-
Notifications
You must be signed in to change notification settings - Fork 0
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 #67 from MITLibraries/tco-29-add-devise
Implement authentication
- Loading branch information
Showing
18 changed files
with
536 additions
and
5 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
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,7 @@ | ||
class ApplicationController < ActionController::Base | ||
helper Mitlibraries::Theme::Engine.helpers | ||
|
||
def new_session_path(_scope) | ||
root_path | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Handles authentication response from Omniauth. See | ||
# [the Devise docs](https://www.rubydoc.info/gems/devise_token_auth/DeviseTokenAuth/OmniauthCallbacksController) for | ||
# additional information about this controller. | ||
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController | ||
include FakeAuthConfig | ||
|
||
def openid_connect | ||
@user = User.from_omniauth(request.env['omniauth.auth']) | ||
sign_in_and_redirect @user, event: :authentication | ||
flash[:notice] = "Welcome, #{@user.email}!" | ||
end | ||
|
||
# Developer authentication is used in local dev and PR builds. | ||
def developer | ||
raise 'Invalid Authentication' unless FakeAuthConfig.fake_auth_enabled? | ||
|
||
@user = User.from_omniauth(request.env['omniauth.auth']) | ||
sign_in_and_redirect @user, event: :authentication | ||
flash[:notice] = "Welcome, #{@user.email}!" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module FakeAuthConfig | ||
# Used in an initializer to determine if the application is configured and allowed to use fake authentication. | ||
def self.fake_auth_enabled? | ||
fake_auth_env? && app_name_pattern_match? | ||
end | ||
|
||
# Default to fake auth in development unless FAKE_AUTH_ENABLED=false. This allows rake tasks to run without loading | ||
# ENV. | ||
private_class_method def self.fake_auth_env? | ||
if Rails.env.development? && ENV['FAKE_AUTH_ENABLED'].nil? | ||
true | ||
else | ||
ENV['FAKE_AUTH_ENABLED'] == 'true' | ||
end | ||
end | ||
|
||
# Check if the app is a PR build. This assures that fake auth is never enabled in staging or prod. | ||
private_class_method def self.app_name_pattern_match? | ||
return true if Rails.env.development? | ||
|
||
review_app_pattern = /^tacos-api-pipeline-pr-\d+$/ | ||
review_app_pattern.match(ENV.fetch('HEROKU_APP_NAME', nil)).present? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# == Schema Information | ||
# | ||
# Table name: users | ||
# | ||
# id :integer not null, primary key | ||
# uid :string not null | ||
# email :string not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class User < ApplicationRecord | ||
include FakeAuthConfig | ||
|
||
if FakeAuthConfig.fake_auth_enabled? | ||
devise :omniauthable, omniauth_providers: [:developer] | ||
else | ||
devise :omniauthable, omniauth_providers: [:openid_connect] | ||
end | ||
|
||
validates :uid, presence: true | ||
validates :email, presence: true | ||
|
||
# Creating a user from Omniauth first requires us to determine whether fake_auth is enabled. If so, we need to | ||
# traverse the response hash differently than with OIDC, as developer mode returns metadata in a different structure. | ||
# @param auth Hash The authentication response from Omniauth. | ||
def self.from_omniauth(auth) | ||
if FakeAuthConfig.fake_auth_enabled? | ||
uid = auth.uid | ||
email = auth.info.email | ||
else | ||
uid = auth.extra.raw_info.preferred_username | ||
email = auth.extra.raw_info.email | ||
end | ||
|
||
User.where(uid: uid).first_or_create do |user| | ||
user.uid = uid | ||
user.email = email | ||
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
Oops, something went wrong.