Skip to content

Commit 3592d20

Browse files
committed
First commit
0 parents  commit 3592d20

File tree

15 files changed

+422
-0
lines changed

15 files changed

+422
-0
lines changed

.github/workflows/ruby.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Ruby
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
ruby-version: ['2.4', '2.5', '2.6', '2.7', '3.0', '3.1', '3.2', 'truffleruby-head']
15+
16+
steps:
17+
- uses: actions/checkout@v3
18+
- name: Set up Ruby ${{ matrix.ruby-version }}
19+
uses: ruby/setup-ruby@v1
20+
with:
21+
ruby-version: ${{ matrix.ruby-version }}
22+
bundler-cache: true
23+
- name: Build and test with Rake
24+
run: bundle exec rake

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
/pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--format progress

Gemfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in omniauth-github.gemspec
4+
gemspec
5+
6+
group :development, :test do
7+
gem 'rake'
8+
end

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Jarrett Lusso
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# OmniAuth LinkedIn
2+
3+
![Ruby](https://github.com/omniauth/omniauth-github/workflows/Ruby/badge.svg?branch=master)
4+
[![Gem](https://img.shields.io/gem/v/omniauth-linkedin-openid)](https://rubygems.org/gems/omniauth-linkedin-openid)
5+
6+
This is the a OmniAuth strategy for authenticating to LinkedIn using OpenID. To
7+
use it, you'll need to register an application on the
8+
[LinkedIn Apps Page](https://www.linkedin.com/developers/apps) to get your
9+
Client ID and Client Secret. Additionally, you'll need to request access to the
10+
"Sign In with LinkedIn using OpenID Connect" product.
11+
12+
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.
13+
14+
## Installation
15+
16+
```ruby
17+
gem 'omniauth-linkedin-openid'
18+
```
19+
20+
## Usage
21+
22+
```ruby
23+
use OmniAuth::Builder do
24+
provider :linkedin, ENV['LINKEDIN_CLIENT_ID'], ENV['LINKEDIN_CLIENT_SECRET']
25+
end
26+
```
27+
28+
## Authenticating Members
29+
30+
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.
31+
32+
The following scopes are requested by default:
33+
34+
'openid profile email'
35+
36+
Here is an example of how you can configure the `scope` option:
37+
38+
```ruby
39+
provider :linkedin, ENV['LINKEDIN_CLIENT_ID'], ENV['LINKEDIN_CLIENT_SECRET'],
40+
scope: 'openid profile email'
41+
```
42+
43+
## Profile Fields
44+
45+
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:
46+
47+
```ruby
48+
%w(id full-name first-name last-name picture-url email-address)
49+
```
50+
51+
Here is an example of how you can configure the `fields` option:
52+
53+
```ruby
54+
provider :linkedin, ENV['LINKEDIN_CLIENT_ID'], ENV['LINKEDIN_CLIENT_SECRET'],
55+
fields: %w(id full-name email-address)
56+
```
57+
58+
To see a complete list of available fields, read the LinkedIn [Profile Fields](https://learn.microsoft.com/en-us/linkedin/shared/references/fields) documentation.
59+
60+
## Development
61+
62+
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.
63+
64+
## Contributing
65+
66+
Bug reports and pull requests are welcome on GitHub at https://github.com/jclusso/omniauth-linkedin-openid.
67+
68+
## License
69+
70+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
71+
72+
## Credits
73+
74+
Thanks to [@decioferreira](https://github.com/decioferreira) for making [omniauth-linkedin-oauth2](https://github.com/decioferreira/omniauth-linkedin-oauth2) which this was based on.

Rakefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'bundler/gem_tasks'
2+
require 'rspec/core/rake_task'
3+
4+
RSpec::Core::RakeTask.new
5+
6+
desc 'Run specs'
7+
task :default => :spec

bin/console

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "bundler/setup"
5+
require "omniauth-linkedin-openid"
6+
7+
# You can add fixtures and/or initialization code here to make experimenting
8+
# with your gem easier. You can also use a different console, if you like.
9+
10+
# (If you use this, don't forget to add pry to your Gemfile!)
11+
# require "pry"
12+
# Pry.start
13+
14+
require "irb"
15+
IRB.start(__FILE__)

bin/setup

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
set -vx
5+
6+
bundle install
7+
8+
# Do any other automated setup that you need to do here

lib/omniauth-linkedin-openid.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require "omniauth-linkedin-openid/version"
2+
require 'omniauth/strategies/linkedin'
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module OmniAuth
2+
module LinkedInOpenID
3+
VERSION = "1.0.0"
4+
end
5+
end

lib/omniauth/strategies/linkedin.rb

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
require 'omniauth-oauth2'
2+
3+
module OmniAuth
4+
module Strategies
5+
class LinkedIn < OmniAuth::Strategies::OAuth2
6+
option :name, 'linkedin'
7+
8+
option :client_options, {
9+
:site => 'https://api.linkedin.com',
10+
:authorize_url => 'https://www.linkedin.com/oauth/v2/authorization?response_type=code',
11+
:token_url => 'https://www.linkedin.com/oauth/v2/accessToken'
12+
}
13+
14+
option :scope, 'openid profile email'
15+
option :fields, %w(
16+
id full-name first-name last-name picture-url email-address
17+
)
18+
option :redirect_url
19+
20+
uid do
21+
raw_info['sub']
22+
end
23+
24+
info do
25+
{
26+
email: raw_info['email'],
27+
first_name: raw_info['given_name'],
28+
last_name: raw_info['family_name'],
29+
picture_url: raw_info['picture']
30+
}
31+
end
32+
33+
extra do
34+
{ 'raw_info' => raw_info }
35+
end
36+
37+
def callback_url
38+
return options.redirect_url if options.redirect_url
39+
40+
full_host + script_name + callback_path
41+
end
42+
43+
alias :oauth2_access_token :access_token
44+
45+
def access_token
46+
::OAuth2::AccessToken.new(client, oauth2_access_token.token, {
47+
expires_in: oauth2_access_token.expires_in,
48+
expires_at: oauth2_access_token.expires_at,
49+
refresh_token: oauth2_access_token.refresh_token
50+
})
51+
end
52+
53+
def raw_info
54+
@raw_info ||= access_token.get(profile_endpoint).parsed
55+
end
56+
57+
private
58+
59+
def fields_mapping
60+
# 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
61+
{
62+
'id' => 'sub',
63+
'full-name' => 'name',
64+
'first-name' => 'given_name',
65+
'last-name' => 'family_name',
66+
'picture-url' => 'picture'
67+
}
68+
end
69+
70+
def fields
71+
options.fields.each.with_object([]) do |field, result|
72+
result << fields_mapping[field] if fields_mapping.has_key? field
73+
end
74+
end
75+
76+
def profile_endpoint
77+
'/v2/userinfo'
78+
end
79+
80+
def token_params
81+
super.tap do |params|
82+
params.client_secret = options.client_secret
83+
end
84+
end
85+
end
86+
end
87+
end
88+
89+
OmniAuth.config.add_camelization 'linkedin', 'LinkedIn'

omniauth-linkedin-openid.gemspec

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- encoding: utf-8 -*-
2+
require File.expand_path('../lib/omniauth-linkedin-openid/version', __FILE__)
3+
4+
Gem::Specification.new do |gem|
5+
gem.authors = 'Jarrett Lusso'
6+
gem.email = '[email protected]'
7+
gem.description = 'OmniAuth strategy for LinkedIn using OpenID.'
8+
gem.summary = 'OmniAuth strategy for LinkedIn using OpenID.'
9+
gem.homepage = 'https://github.com/jclusso/omniauth-linkedin-openid'
10+
gem.license = 'MIT'
11+
12+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13+
gem.files = `git ls-files`.split("\n")
14+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15+
gem.name = 'omniauth-linkedin-openid'
16+
gem.require_paths = ['lib']
17+
gem.version = OmniAuth::LinkedInOpenID::VERSION
18+
19+
gem.metadata = {
20+
"bug_tracker_uri" => "https://github.com/jclusso/omniauth-linkedin-openid/issues",
21+
"documentation_uri" => "https://github.com/jclusso/omniauth-linkedin-openid/README.md",
22+
"source_code_uri" => "https://github.com/jclusso/omniauth-linkedin-openid"
23+
}
24+
25+
gem.add_dependency 'omniauth', '~> 2.0'
26+
gem.add_dependency 'omniauth-oauth2', '~> 1.8'
27+
gem.add_development_dependency 'rspec', '~> 3.5'
28+
gem.add_development_dependency 'rack-test'
29+
gem.add_development_dependency 'webmock'
30+
end

0 commit comments

Comments
 (0)