Sha256: 2371a125308ca1aae3cff335019e0036a04df4bcbc8468d8e5666842def11bd8

Contents?: true

Size: 1.8 KB

Versions: 9

Compression:

Stored size: 1.8 KB

Contents

require "action_controller/metal"
require 'rails'

# Failure application that will be called every time :warden is thrown from
# any strategy or hook.
module GDS
  module SSO
    class FailureApp < ActionController::Metal
      include ActionController::UrlFor
      include ActionController::Redirecting
      include AbstractController::Rendering
      include ActionController::Rendering
      include ActionController::Renderers
      use_renderers :json

      include Rails.application.routes.url_helpers

      def self.call(env)
        if GDS::SSO::ApiAccess.api_call?(env)
          action(:api_invalid_token).call(env)
        elsif GDS::SSO::Config.api_only?
          action(:api_missing_token).call(env)
        else
          action(:redirect).call(env)
        end
      end

      def redirect
        store_location!
        redirect_to '/auth/gds'
      end

      def api_invalid_token
        api_unauthorized('Bearer token does not appear to be valid', 'invalid_token')
      end

      def api_missing_token
        api_unauthorized('No bearer token was provided', 'invalid_request')
      end

      # Stores requested uri to redirect the user after signing in. We cannot use
      # scoped session provided by warden here, since the user is not authenticated
      # yet, but we still need to store the uri based on scope, so different scopes
      # would never use the same uri to redirect.

      # TOTALLY NOT DOING THE SCOPE THING. PROBABLY SHOULD.
      def store_location!
        session["return_to"] = request.env['warden.options'][:attempted_path] if request.get?
      end

      private

      def api_unauthorized(message, bearer_error)
        headers['WWW-Authenticate'] = %(Bearer error="#{bearer_error}")
        render json: { message: message }, status: :unauthorized
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gds-sso-15.0.0 lib/gds-sso/failure_app.rb
gds-sso-14.3.0 lib/gds-sso/failure_app.rb
gds-sso-14.2.0 lib/gds-sso/failure_app.rb
gds-sso-14.1.1 lib/gds-sso/failure_app.rb
gds-sso-14.1.0 lib/gds-sso/failure_app.rb
gds-sso-14.0.0 lib/gds-sso/failure_app.rb
gds-sso-13.6.0 lib/gds-sso/failure_app.rb
gds-sso-13.5.1 lib/gds-sso/failure_app.rb
gds-sso-13.5.0 lib/gds-sso/failure_app.rb