-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from NYULibraries/mcain/shibboleth-omniauth
Integrate Shibboleth SSO
- Loading branch information
Showing
12 changed files
with
165 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ruby 2.7.8 | ||
nodejs 18.10.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//= link_tree ../images | ||
//= link_tree ../javascripts | ||
//= link_tree ../stylesheets | ||
//= link_directory ../stylesheets .css |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# frozen_string_literal: true | ||
|
||
class OmniauthCallbacksController < Devise::OmniauthCallbacksController | ||
before_action :require_valid_omniauth, only: :shibboleth | ||
|
||
def shibboleth | ||
Rails.logger.info("OmniauthCallbacksController#shibboleth: #{omniauth.inspect}") | ||
set_user | ||
if @user.persisted? | ||
log_in | ||
else | ||
redirect_to_login | ||
end | ||
end | ||
|
||
def failure | ||
redirect_to root_path | ||
end | ||
|
||
private | ||
|
||
def redirect_to_login | ||
Rails.logger.error(find_message(:failure, kind: 'NYU Shibboleth', reason: 'User not persisted')) | ||
session['devise.shibboleth_data'] = request.env['omniauth.auth'] | ||
redirect_to root_path | ||
end | ||
|
||
def log_in | ||
sign_in_and_redirect @user, event: :authentication | ||
set_flash_message(:notice, :success, kind: 'NYU Shibboleth') | ||
logger.info(find_message(:success, kind: 'NYU Shibboleth')) | ||
end | ||
|
||
def require_valid_omniauth | ||
head :bad_request unless valid_omniauth? | ||
end | ||
|
||
def valid_omniauth? | ||
omniauth.present? | ||
end | ||
|
||
def omniauth | ||
@omniauth ||= request.env['omniauth.auth'] | ||
end | ||
|
||
def omniauth_provider | ||
@omniauth_provider ||= omniauth.provider | ||
end | ||
|
||
def attributes_from_omniauth | ||
{ | ||
provider: omniauth.provider, | ||
username: omniauth.uid, | ||
email: omniauth_email, | ||
firstname: omniauth_firstname, | ||
lastname: omniauth_lastname | ||
} | ||
end | ||
|
||
def omniauth_email | ||
@omniauth_email ||= omniauth.info.email | ||
end | ||
|
||
def omniauth_firstname | ||
@omniauth_firstname ||= omniauth.info.first_name | ||
end | ||
|
||
def omniauth_lastname | ||
@omniauth_lastname ||= omniauth.info.last_name | ||
end | ||
|
||
def set_user | ||
# Find existing or initialize new user, | ||
# and save new attributes each time | ||
@user = find_user | ||
@user.update_attributes(attributes_from_omniauth) | ||
end | ||
|
||
def find_user | ||
@find_user ||= User.create_from_provider_data(request.env['omniauth.auth']) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module OmniAuth | ||
module Strategies | ||
require 'omniauth-oauth2' | ||
class Shibboleth < OmniAuth::Strategies::OAuth2 | ||
if defined?(::Rails) && ::Rails.env.development? | ||
silence_warnings do | ||
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE | ||
end | ||
end | ||
option :name, :shibboleth | ||
option :authorize_params, { scope: "openid" } | ||
|
||
uid do | ||
raw_info["sub"] | ||
end | ||
|
||
info do | ||
{ | ||
email: raw_info["sub"], | ||
last_name: last_name, | ||
first_name: first_name | ||
} | ||
end | ||
|
||
def raw_info | ||
response = access_token.get("/oauth2/userinfo?schema=openid") | ||
Rails.logger.info("Shibboleth raw_info: #{response.parsed}") | ||
@raw_info ||= response.parsed | ||
end | ||
|
||
# Extract Last Name from identity | ||
def last_name | ||
@last_name ||= raw_info["lastname"] rescue nil | ||
end | ||
|
||
# Extract First Name from identity | ||
def first_name | ||
@first_name ||= raw_info["firstname"] rescue nil | ||
end | ||
|
||
def log(level, message) | ||
Rails.logger.send(level, "(#{name}) #{message}") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
describe 'routes for users' do | ||
describe 'GET /users/auth/nyulibraries' do | ||
subject { get('/users/auth/nyulibraries') } | ||
describe 'GET /auth/shibboleth' do | ||
subject { get('/auth/shibboleth') } | ||
it do | ||
should route_to({ | ||
controller: 'users/omniauth_callbacks', | ||
controller: 'omniauth_callbacks', | ||
action: 'passthru' | ||
}) | ||
end | ||
end | ||
|
||
describe 'POST /users/auth/nyulibraries' do | ||
subject { post('/users/auth/nyulibraries') } | ||
describe 'POST /auth/shibboleth' do | ||
subject { post('/auth/shibboleth') } | ||
it do | ||
should route_to({ | ||
controller: 'users/omniauth_callbacks', | ||
controller: 'omniauth_callbacks', | ||
action: 'passthru' | ||
}) | ||
end | ||
end | ||
|
||
describe 'GET /users/auth/nyulibraries/callback' do | ||
subject { get('/users/auth/nyulibraries/callback') } | ||
it { should route_to({ controller: 'users/omniauth_callbacks', action: 'nyulibraries' }) } | ||
describe 'GET /auth/shibboleth/callback' do | ||
subject { get('/auth/shibboleth/callback') } | ||
it { should route_to({ controller: 'omniauth_callbacks', action: 'shibboleth' }) } | ||
end | ||
|
||
describe 'POST /users/auth/nyulibraries/callback' do | ||
subject { post('/users/auth/nyulibraries/callback') } | ||
it { should route_to({ controller: 'users/omniauth_callbacks', action: 'nyulibraries' }) } | ||
describe 'POST /auth/shibboleth/callback' do | ||
subject { post('/auth/shibboleth/callback') } | ||
it { should route_to({ controller: 'omniauth_callbacks', action: 'shibboleth' }) } | ||
end | ||
end |