Sha256: d73969ed787c5531f1037095e5c72d10776de4eb5959f4ca4d9bfcbb7cac4e1d

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

# app/middleware/sso/authorization_grant_maker.rb
# Middleware that catches outgoing Doorkeeper authorization grants

module Sso
  class AuthorizationGrantMarker

    def initialize(app)
      @app = app
    end

    def call(env)
      @env = env
      @response = @app.call @env

      return response unless outgoing_grant_token

      if passport_id
        Rails.logger.debug { %{Detected outgoing "Authorization Grant Token" #{outgoing_grant_token.inspect} of the "Authorization Code Grant" flow. Augmenting Passport #{passport_id.inspect} with it.} }
        registration = ::Passports.register_authorization_grant passport_id: passport_id, token: outgoing_grant_token

        if registration.failure?
          Rails.logger.warn { "The passport could not be augmented. Destroying warden session." }
          warden.logout
        end
      end

      response
    end

    def request
      ::ActionDispatch::Request.new @env
    end

    def response
      @response
    end

    def code
      response.first
    end

    def session
      request.session
    end

    def warden
      request.env['warden']
    end

    def passport_id
      session['passport_id']
    end

    def location_header
      unless code == 302
        #logger.debug { "Uninteresting response, because it is not a redirect" }
        return
      end

      response.second['Location']
    end

    def redirect_uri
      unless location_header
        #logger.debug { "Uninteresting response, because there is no Location header" }
        return
      end

      ::URI.parse location_header
    end

    def redirect_uri_params
      return unless redirect_uri
      ::Rack::Utils.parse_query redirect_uri.query
    end

    def outgoing_grant_token
      unless redirect_uri_params && redirect_uri_params['code']
        #logger.debug { "Uninteresting response, because there is no code parameter sent" }
        return
      end

      redirect_uri_params['code']
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
doorkeeper_sso-0.0.4 app/middleware/sso/authorization_grant_marker.rb