omniauth-mit-oauth2 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +178 -0
- data/Rakefile +6 -0
- data/lib/omniauth/mit_oauth2/version.rb +5 -0
- data/lib/omniauth/mit_oauth2.rb +1 -0
- data/lib/omniauth/strategies/mit_oauth2.rb +34 -0
- data/lib/omniauth-mit-oauth2.rb +1 -0
- data/omniauth-mit-oauth2.gemspec +25 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3db752ef555c3cd5fca32ffdc4d3d010e7c07f6e
|
4
|
+
data.tar.gz: 25dba93846917189125f28dca41537ea59bdb7fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b33cfc0a214b90ae5c1e125e372ddfb8ffba3067380800a23a9080be9bc4f539ce8e2fa358816704d1df1ba4dcc846ac7d79971f9692ce5e0bd0625d108f7c6f
|
7
|
+
data.tar.gz: 87253db41e8c22ceacd6dfa731cb8a40dc0627a7bf5f9b3f24eebba24757502c704293fdd7ff3c4516b70de34c22ad82391667e02c5d0e47387c1de3b360c7de
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 MIT Libraries
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,178 @@
|
|
1
|
+
# MIT OpenID Connect OmniAuth Strategy
|
2
|
+
|
3
|
+
This gem provides an OmniAuth strategy for authenticating users through [MIT OpenID Connect](https://oidc.mit.edu/).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'omniauth-mit-oauth2'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install omniauth-mit-oauth2
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
This can be used by configuring OmniAuth in `config/initializers/omniauth.rb` (if using Devise, see instructions below instead):
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Rails.application.config.middleware.use OmniAuth::Builder do
|
27
|
+
provider :mit_oauth2, "MIT_OAUTH2_API_KEY", "MIT_OAUTH2_API_SECRET", {
|
28
|
+
scope: "openid,name,email"
|
29
|
+
}
|
30
|
+
```
|
31
|
+
|
32
|
+
Replace `MIT_OAUTH2_API_KEY` and `MIT_OAUTH2_API_SECRET` with the values obtained from registering your service through MIT OIDC.
|
33
|
+
|
34
|
+
## Devise
|
35
|
+
|
36
|
+
The following instructions provide an example of how this could be used with Devise to add MIT authentication to your site.
|
37
|
+
|
38
|
+
### Registering Your Client
|
39
|
+
|
40
|
+
The first thing you will need to do is register your client with MIT OIDC. Once you have created the client, under the `Main` tab, find the `Redirect URI(s)` field. You need to add the callback URL `https://example.com/users/auth/mit_oauth2/callback` here replacing `example.com` with wherever your application will be deployed.
|
41
|
+
|
42
|
+
### Configuring Your App
|
43
|
+
|
44
|
+
Make sure Devise and this gem are included in your Gemfile:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
gem 'devise'
|
48
|
+
gem 'omniauth-mit-oauth2'
|
49
|
+
```
|
50
|
+
|
51
|
+
Install the gems:
|
52
|
+
|
53
|
+
```
|
54
|
+
bundle install
|
55
|
+
```
|
56
|
+
|
57
|
+
Create the user model:
|
58
|
+
|
59
|
+
```
|
60
|
+
rails generate devise:install
|
61
|
+
rails generate devise User
|
62
|
+
```
|
63
|
+
|
64
|
+
We don't want to provide account registration since users will just be using their MIT account to log in. Modify `app/models/user.rb` to only use the `:omniauthable` module, and add a method to create the user from the OAuth token:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class User < ActiveRecord::Base
|
68
|
+
devise :omniauthable, :omniauth_providers => [:mit_oauth2]
|
69
|
+
|
70
|
+
def self.from_omniauth(auth)
|
71
|
+
where(uid: auth.uid).first_or_create do |user|
|
72
|
+
user.email = auth.info.email
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
Next edit the migration created by devise:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class DeviseCreateUsers < ActiveRecord::Migration
|
82
|
+
def change
|
83
|
+
create_table(:users) do |t|
|
84
|
+
t.string :email
|
85
|
+
t.string :uid, null: false
|
86
|
+
|
87
|
+
t.timestamps null: false
|
88
|
+
end
|
89
|
+
add_index :users, :uid, unique: true
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
Run the migration:
|
95
|
+
|
96
|
+
```
|
97
|
+
rake db:migrate
|
98
|
+
```
|
99
|
+
|
100
|
+
Configure OmniAuth to use our provider in `config/initializers/devise.rb`:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
Devise.setup do |config|
|
104
|
+
# ...
|
105
|
+
config.omniauth :mit_oauth2, "MIT_OAUTH2_API_KEY", "MIT_OAUTH2_API_SECRET", {
|
106
|
+
scope: "openid,email,profile"
|
107
|
+
}
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
Replace `MIT_OAUTH2_API_KEY` and `MIT_OAUTH2_API_SECRET` with the values obtained by registering your site.
|
112
|
+
|
113
|
+
Now we need to set up the routes:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
Rails.application.routes.draw do
|
117
|
+
devise_for :users, :controllers => {
|
118
|
+
:omniauth_callbacks => 'users/omniauth_callbacks'
|
119
|
+
}
|
120
|
+
|
121
|
+
devise_scope :user do
|
122
|
+
get 'sign_in', to: 'devise/sessions#new', as: :new_user_session
|
123
|
+
delete 'sign_out', to: 'devise/sessions#destroy', as: :destroy_user_session
|
124
|
+
end
|
125
|
+
# ...
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
Next create a new controller for the OAuth callback in `app/controllers/users/omniauth_callbacks_controller.rb`:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
133
|
+
def mit_oauth2
|
134
|
+
@user = User.from_omniauth(request.env['omniauth.auth'])
|
135
|
+
sign_in_and_redirect @user, event: :authentication
|
136
|
+
end
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
140
|
+
Since we are not using `:database_authenticatable` we need to define a helper method to be used in case of authentication failures. Add this to `app/controllers/application_controller.rb`:
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
class ApplicationController < ActionController::Base
|
144
|
+
# ...
|
145
|
+
def new_session_path(scope)
|
146
|
+
new_user_session_path
|
147
|
+
end
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
151
|
+
Finally, we need to add the necessary views. A sign in link can be generated by using the following:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
<%= link_to("Sign in", user_omniauth_authorize_path(:mit_oauth2)) %>
|
155
|
+
```
|
156
|
+
|
157
|
+
Depending on your application, you might want to put this in a nav bar along with a sign out link. For example:
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
<% if user_signed_in? %>
|
161
|
+
<%= link_to("Sign out", destroy_user_session_path, method: :delete) %>
|
162
|
+
<% else %>
|
163
|
+
<%= link_to("Sign in", user_omniauth_authorize_path(:mit_oauth2)) %>
|
164
|
+
<% end %>
|
165
|
+
```
|
166
|
+
|
167
|
+
You should also create a view to handle cases where authentication has failed, for example, if the user has not allowed the required scopes. This should go in `app/views/devise/sessions/new.html.erb`.
|
168
|
+
|
169
|
+
|
170
|
+
## Contributing
|
171
|
+
|
172
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/MITLibraries/omniauth-mit-oauth2.
|
173
|
+
|
174
|
+
|
175
|
+
## License
|
176
|
+
|
177
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
178
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "omniauth/strategies/mit_oauth2"
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'omniauth/strategies/oauth2'
|
2
|
+
|
3
|
+
module OmniAuth
|
4
|
+
module Strategies
|
5
|
+
class MITOAuth2 < OmniAuth::Strategies::OAuth2
|
6
|
+
option :name, 'mit_oauth2'
|
7
|
+
|
8
|
+
option :client_options, {
|
9
|
+
site: "https://oidc.mit.edu",
|
10
|
+
authorize_url: '/authorize',
|
11
|
+
token_url: '/token'
|
12
|
+
}
|
13
|
+
|
14
|
+
uid { raw_info['sub'] }
|
15
|
+
|
16
|
+
info do
|
17
|
+
{
|
18
|
+
name: raw_info['name'],
|
19
|
+
email: raw_info['email']
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
extra do
|
24
|
+
{ raw_info: raw_info }
|
25
|
+
end
|
26
|
+
|
27
|
+
def raw_info
|
28
|
+
@raw_info ||= access_token.get('/userinfo').parsed
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
OmniAuth.config.add_camelization('mit_oauth2', 'MITOAuth2')
|
@@ -0,0 +1 @@
|
|
1
|
+
require "omniauth/mit_oauth2"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'omniauth/mit_oauth2/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "omniauth-mit-oauth2"
|
8
|
+
spec.version = OmniAuth::MITOAuth2::VERSION
|
9
|
+
spec.authors = ["Mike Graves"]
|
10
|
+
spec.email = ["mgraves@mit.edu"]
|
11
|
+
|
12
|
+
spec.summary = %q{OmniAuth strategy for MIT OIDC}
|
13
|
+
spec.description = %q{OmniAuth strategy for MIT OIDC}
|
14
|
+
spec.homepage = "https://github.com/MITLibraries/omniauth-mit-oauth2"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency 'omniauth-oauth2', '~> 1.1'
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omniauth-mit-oauth2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Graves
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: omniauth-oauth2
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: OmniAuth strategy for MIT OIDC
|
70
|
+
email:
|
71
|
+
- mgraves@mit.edu
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/omniauth-mit-oauth2.rb
|
84
|
+
- lib/omniauth/mit_oauth2.rb
|
85
|
+
- lib/omniauth/mit_oauth2/version.rb
|
86
|
+
- lib/omniauth/strategies/mit_oauth2.rb
|
87
|
+
- omniauth-mit-oauth2.gemspec
|
88
|
+
homepage: https://github.com/MITLibraries/omniauth-mit-oauth2
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.5
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: OmniAuth strategy for MIT OIDC
|
112
|
+
test_files: []
|