Sha256: b142f414dc84a5c24b7d4c21970edf81b8687da1f5da1501f9d4e923e4ed6251

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

module Rack
  module OAuth2
    module Server
      class Token < Abstract::Handler
        attr_accessor :grant_type, :optional_authentication

        def call(env)
          request = Request.new(env)
          request.profile.new(@app, @realm, &@authenticator).call(env).finish
        rescue Error => e
          e.finish
        end

        class Request < Abstract::Request
          attr_accessor :client_id, :client_secret, :code, :redirect_uri, :scope

          def initialize(env)
            super
            @client_id     = params['client_id']
            @client_secret = params['client_secret']
            @scope         = Array(params['scope'].to_s.split(' '))
          end

          def required_params
            [:grant_type, :client_id]
          end

          def profile(allow_no_profile = false)
            case params['grant_type']
            when 'authorization_code'
              AuthorizationCode
            when 'password'
              Password
            when 'assertion'
              Assertion
            when 'refresh_token'
              RefreshToken
            else
              raise BadRequest.new(:unsupported_grant_type, "'#{params['invalid_grant']}' isn't supported.")
            end
          end
        end

        class Response < Abstract::Response
          attr_accessor :access_token, :expires_in, :refresh_token, :scope

          def finish
            response = {:access_token => access_token}
            response[:expires_in] = expires_in if expires_in
            response[:refresh_token] = refresh_token if refresh_token
            response[:scope] = Array(scope).join(' ') if scope
            [200, {'Content-Type' => "application/json"}, response.to_json]
          end
        end

      end
    end
  end
end

require 'rack/oauth2/server/token/authorization_code'
require 'rack/oauth2/server/token/password'
require 'rack/oauth2/server/token/assertion'
require 'rack/oauth2/server/token/refresh_token'

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-oauth2-0.0.0 lib/rack/oauth2/server/token.rb