Sha256: 26857a00fe503a4e17de8229bb2f3b7427f629785ca7855c9a256c51ec82889f

Contents?: true

Size: 1.79 KB

Versions: 11

Compression:

Stored size: 1.79 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

11 entries across 11 versions & 1 rubygems

Version Path
gds-sso-19.0.0 lib/gds-sso/failure_app.rb
gds-sso-18.1.0 lib/gds-sso/failure_app.rb
gds-sso-18.0.0 lib/gds-sso/failure_app.rb
gds-sso-17.1.1 lib/gds-sso/failure_app.rb
gds-sso-17.1.0 lib/gds-sso/failure_app.rb
gds-sso-17.0.0 lib/gds-sso/failure_app.rb
gds-sso-16.1.0 lib/gds-sso/failure_app.rb
gds-sso-16.0.2 lib/gds-sso/failure_app.rb
gds-sso-16.0.1 lib/gds-sso/failure_app.rb
gds-sso-16.0.0 lib/gds-sso/failure_app.rb
gds-sso-15.1.0 lib/gds-sso/failure_app.rb