Skip to content

Commit

Permalink
Remove GDS SSO Gem
Browse files Browse the repository at this point in the history
Remove the Gem, add bits missing from the gem itself.
  • Loading branch information
Tom Iles committed Nov 16, 2023
1 parent 1b88166 commit b978709
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 187 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ gem "pg", "~> 1.5"
gem "puma", "~> 6.4.0"

# Used for handling authentication
gem "gds-sso"
gem "omniauth"
gem "omniauth-auth0"
gem "warden"
Expand Down
12 changes: 0 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,6 @@ GEM
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
gds-sso (18.1.0)
oauth2 (~> 2.0)
omniauth (~> 2.1)
omniauth-oauth2 (~> 1.8)
plek (>= 4, < 6)
rails (>= 6)
warden (~> 1.2)
warden-oauth2 (~> 0.0.1)
globalid (1.2.1)
activesupport (>= 6.1)
govuk-components (4.1.1)
Expand Down Expand Up @@ -322,7 +314,6 @@ GEM
ast (~> 2.4.1)
racc
pg (1.5.4)
plek (5.0.0)
psych (5.1.1.1)
stringio
public_suffix (5.0.3)
Expand Down Expand Up @@ -493,8 +484,6 @@ GEM
zeitwerk (~> 2.2)
warden (1.2.9)
rack (>= 2.0.9)
warden-oauth2 (0.0.1)
warden
webmock (3.19.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
Expand Down Expand Up @@ -529,7 +518,6 @@ DEPENDENCIES
dfe-autocomplete!
factory_bot_rails
faker
gds-sso
govuk-components (~> 4.1.1)
govuk-forms-markdown!
govuk_design_system_formbuilder (~> 4.1.1)
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/authentication_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def sign_out
end
end

def failure
render "authentications/failure", layout: "application"
end

private

def attempted_path
Expand Down
13 changes: 12 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class User < ApplicationRecord
include GDS::SSO::User
has_paper_trail only: %i[role organisation_id has_access]

class UserAuthenticationException < StandardError; end
Expand Down Expand Up @@ -85,6 +84,18 @@ def role_changed_to_editor?
role_changed_to_editor
end

def clear_remotely_signed_out!
# rubocop:disable Rails/SkipsModelValidations
update_attribute(:remotely_signed_out, false)
# rubocop:enable Rails/SkipsModelValidations
end

def set_remotely_signed_out!
# rubocop:disable Rails/SkipsModelValidations
update_attribute(:remotely_signed_out, true)
# rubocop:enable Rails/SkipsModelValidations
end

private

def requires_name?
Expand Down
12 changes: 2 additions & 10 deletions app/service/navigation_items_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def support_navigation_item
def profile_navigation_item
return nil if user.name.blank?

NavigationItem.new(text: user.name, href: user_profile_url, active: false)
NavigationItem.new(text: user.name, href: nil, active: false)
end

def signout_navigation_item
Expand All @@ -70,19 +70,11 @@ def user_provider
end

def signout_url
if user_provider == :gds
gds_sign_out_path
elsif %i[auth0 mock_gds_sso].include? user_provider
if %i[auth0 mock_gds_sso].include? user_provider
sign_out_path
end
end

def user_profile_url
if user_provider == :gds
GDS::SSO::Config.oauth_root_url
end
end

def should_show_user_profile_link?
Pundit.policy(user, :user).can_manage_user?
end
Expand Down
87 changes: 74 additions & 13 deletions config/initializers/authentication.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,73 @@
Rails.application.config.before_initialize do
# Configure OmniAuth authentication middleware
# add Auth0 provider
Rails.application.config.app_middleware.use(
OmniAuth::Strategies::Auth0,
require "warden"

OmniAuth.config.logger = Rails.logger

Warden::Manager.after_authentication do |user, _auth, _opts|
# We've successfully signed in.
# If they were remotely signed out, clear the flag as they're no longer suspended
user.clear_remotely_signed_out!
end

Warden::Manager.serialize_into_session do |user|
if user.respond_to?(:uid) && user.uid
[user.uid, Time.zone.now.utc.iso8601]
end
end

Warden::Manager.serialize_from_session do |(uid, auth_timestamp)|
# This will reject old sessions that don't have a previous login timestamp
if auth_timestamp.is_a?(String)
begin
auth_timestamp = Time.zone.parse(auth_timestamp)
rescue ArgumentError
auth_timestamp = nil
end
end

if auth_timestamp && ((auth_timestamp + Settings.auth_valid_for) > Time.zone.now.utc)
User.where(uid:, remotely_signed_out: false).first
end
end

Rails.application.config.app_middleware.use Warden::Manager do |warden|
warden.default_strategies(Settings.auth_provider.to_sym, :gds_bearer_token)
warden.failure_app = AuthenticationController
end

# Rails.application.config.before_initialize do
# Configure OmniAuth authentication middleware
# add Auth0 provider
# Rails.application.config.app_middleware.use(
# OmniAuth::Strategies::Auth0,
# Settings.auth0.client_id,
# Settings.auth0.client_secret,
# Settings.auth0.domain,
# callback_path: "/auth/auth0/callback",
# authorize_params: {
# scope: "openid email",
# connection: "email", # default to using the passwordless flow
# },
# )

# config.app_middleware.use ::OmniAuth::Builder do
# next if GDS::SSO::Config.api_only

# provider :gds, GDS::SSO::Config.oauth_id, GDS::SSO::Config.oauth_secret,
# client_options: {
# site: GDS::SSO::Config.oauth_root_url,
# authorize_url: "#{GDS::SSO::Config.oauth_root_url}/oauth/authorize",
# token_url: "#{GDS::SSO::Config.oauth_root_url}/oauth/access_token",
# connection_opts: {
# headers: {
# user_agent: "gds-sso/#{GDS::SSO::VERSION} (#{ENV['GOVUK_APP_NAME']})",
# },
# },
# }
# end

Rails.application.config.middleware.use OmniAuth::Builder do
provider(
:auth0,
Settings.auth0.client_id,
Settings.auth0.client_secret,
Settings.auth0.domain,
Expand All @@ -12,13 +77,9 @@
connection: "email", # default to using the passwordless flow
},
)
end

# Configure Warden session management middleware
# swap out the Warden::Manager installed by `gds-sso` gem
Rails.application.config.app_middleware.swap Warden::Manager, Warden::Manager do |warden|
warden.default_strategies(Settings.auth_provider.to_sym, :gds_bearer_token)
warden.failure_app = AuthenticationController
end
OmniAuth.config.allowed_request_methods = %i[post get]

GDS::SSO::Config.auth_valid_for = Settings.auth_valid_for
end
# GDS::SSO::Config.auth_valid_for = Settings.auth_valid_for
# end
6 changes: 6 additions & 0 deletions config/initializers/warden/strategies/basic_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ def authenticate!
custom! [@status, headers, [@message]]
end
end

private

def logger
Rails.logger || env["rack.logger"]
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

get "/sign-up" => "authentication#sign_up", as: :sign_up
get "/sign-out" => "authentication#sign_out", as: :sign_out
get "/auth/failure" => "authentication#failure"

scope "auth/:provider" do
get "/callback" => "authentication#callback_from_omniauth"
Expand Down
4 changes: 4 additions & 0 deletions lib/warden/strategies/omniauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ def authenticate!
def prep_user(auth_hash)
raise NotImplementedError
end

def logger
Rails.logger || env["rack.logger"]
end
end
end
119 changes: 0 additions & 119 deletions spec/integration/gds_sso_spec.rb

This file was deleted.

3 changes: 0 additions & 3 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require "gds-sso/lint/user_spec"
require "rails_helper"

describe User, type: :model do
Expand All @@ -21,8 +20,6 @@
end
end

it_behaves_like "a gds-sso user class"

describe "role" do
it "is invalid if blank" do
user.role = nil
Expand Down
Loading

0 comments on commit b978709

Please sign in to comment.