Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jclusso committed Sep 12, 2023
0 parents commit 3592d20
Show file tree
Hide file tree
Showing 15 changed files with 422 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Ruby

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.4', '2.5', '2.6', '2.7', '3.0', '3.1', '3.2', 'truffleruby-head']

steps:
- uses: actions/checkout@v3
- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- name: Build and test with Rake
run: bundle exec rake
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
/pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in omniauth-github.gemspec
gemspec

group :development, :test do
gem 'rake'
end
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Jarrett Lusso

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# OmniAuth LinkedIn

![Ruby](https://github.com/omniauth/omniauth-github/workflows/Ruby/badge.svg?branch=master)
[![Gem](https://img.shields.io/gem/v/omniauth-linkedin-openid)](https://rubygems.org/gems/omniauth-linkedin-openid)

This is the a OmniAuth strategy for authenticating to LinkedIn using OpenID. To
use it, you'll need to register an application on the
[LinkedIn Apps Page](https://www.linkedin.com/developers/apps) to get your
Client ID and Client Secret. Additionally, you'll need to request access to the
"Sign In with LinkedIn using OpenID Connect" product.

For more details, read the [Sign In with LinkedIn using OpenID Connect](https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2) documentation.

## Installation

```ruby
gem 'omniauth-linkedin-openid'
```

## Usage

```ruby
use OmniAuth::Builder do
provider :linkedin, ENV['LINKEDIN_CLIENT_ID'], ENV['LINKEDIN_CLIENT_SECRET']
end
```

## Authenticating Members

With the LinkedIn API, you have the ability to specify which permissions you want users to grant your application. For more details, read the LinkedIn [Authenticating Members](https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2#authenticating-members) documentation.

The following scopes are requested by default:

'openid profile email'

Here is an example of how you can configure the `scope` option:

```ruby
provider :linkedin, ENV['LINKEDIN_CLIENT_ID'], ENV['LINKEDIN_CLIENT_SECRET'],
scope: 'openid profile email'
```

## Profile Fields

When specifying which permissions you want users to grant to your application, you can also specify the array of fields that you want returned in the OmniAuth hash. The following fields are requested by default:

```ruby
%w(id full-name first-name last-name picture-url email-address)
```

Here is an example of how you can configure the `fields` option:

```ruby
provider :linkedin, ENV['LINKEDIN_CLIENT_ID'], ENV['LINKEDIN_CLIENT_SECRET'],
fields: %w(id full-name email-address)
```

To see a complete list of available fields, read the LinkedIn [Profile Fields](https://learn.microsoft.com/en-us/linkedin/shared/references/fields) documentation.

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jclusso/omniauth-linkedin-openid.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## Credits

Thanks to [@decioferreira](https://github.com/decioferreira) for making [omniauth-linkedin-oauth2](https://github.com/decioferreira/omniauth-linkedin-oauth2) which this was based on.
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new

desc 'Run specs'
task :default => :spec
15 changes: 15 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/setup"
require "omniauth-linkedin-openid"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
2 changes: 2 additions & 0 deletions lib/omniauth-linkedin-openid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "omniauth-linkedin-openid/version"
require 'omniauth/strategies/linkedin'
5 changes: 5 additions & 0 deletions lib/omniauth-linkedin-openid/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module OmniAuth
module LinkedInOpenID
VERSION = "1.0.0"
end
end
89 changes: 89 additions & 0 deletions lib/omniauth/strategies/linkedin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
require 'omniauth-oauth2'

module OmniAuth
module Strategies
class LinkedIn < OmniAuth::Strategies::OAuth2
option :name, 'linkedin'

option :client_options, {
:site => 'https://api.linkedin.com',
:authorize_url => 'https://www.linkedin.com/oauth/v2/authorization?response_type=code',
:token_url => 'https://www.linkedin.com/oauth/v2/accessToken'
}

option :scope, 'openid profile email'
option :fields, %w(
id full-name first-name last-name picture-url email-address
)
option :redirect_url

uid do
raw_info['sub']
end

info do
{
email: raw_info['email'],
first_name: raw_info['given_name'],
last_name: raw_info['family_name'],
picture_url: raw_info['picture']
}
end

extra do
{ 'raw_info' => raw_info }
end

def callback_url
return options.redirect_url if options.redirect_url

full_host + script_name + callback_path
end

alias :oauth2_access_token :access_token

def access_token
::OAuth2::AccessToken.new(client, oauth2_access_token.token, {
expires_in: oauth2_access_token.expires_in,
expires_at: oauth2_access_token.expires_at,
refresh_token: oauth2_access_token.refresh_token
})
end

def raw_info
@raw_info ||= access_token.get(profile_endpoint).parsed
end

private

def fields_mapping
# https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/sign-in-with-linkedin-v2?context=linkedin%2Fconsumer%2Fcontext#api-request-to-retreive-member-details
{
'id' => 'sub',
'full-name' => 'name',
'first-name' => 'given_name',
'last-name' => 'family_name',
'picture-url' => 'picture'
}
end

def fields
options.fields.each.with_object([]) do |field, result|
result << fields_mapping[field] if fields_mapping.has_key? field
end
end

def profile_endpoint
'/v2/userinfo'
end

def token_params
super.tap do |params|
params.client_secret = options.client_secret
end
end
end
end
end

OmniAuth.config.add_camelization 'linkedin', 'LinkedIn'
30 changes: 30 additions & 0 deletions omniauth-linkedin-openid.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/omniauth-linkedin-openid/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = 'Jarrett Lusso'
gem.email = '[email protected]'
gem.description = 'OmniAuth strategy for LinkedIn using OpenID.'
gem.summary = 'OmniAuth strategy for LinkedIn using OpenID.'
gem.homepage = 'https://github.com/jclusso/omniauth-linkedin-openid'
gem.license = 'MIT'

gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.name = 'omniauth-linkedin-openid'
gem.require_paths = ['lib']
gem.version = OmniAuth::LinkedInOpenID::VERSION

gem.metadata = {
"bug_tracker_uri" => "https://github.com/jclusso/omniauth-linkedin-openid/issues",
"documentation_uri" => "https://github.com/jclusso/omniauth-linkedin-openid/README.md",
"source_code_uri" => "https://github.com/jclusso/omniauth-linkedin-openid"
}

gem.add_dependency 'omniauth', '~> 2.0'
gem.add_dependency 'omniauth-oauth2', '~> 1.8'
gem.add_development_dependency 'rspec', '~> 3.5'
gem.add_development_dependency 'rack-test'
gem.add_development_dependency 'webmock'
end
Loading

0 comments on commit 3592d20

Please sign in to comment.