Sha256: 5f172047513980292d230a6123158112c690feacdc39d99c61445fcc8fd36f97

Contents?: true

Size: 1.67 KB

Versions: 13

Compression:

Stored size: 1.67 KB

Contents

module Doorkeeper
  module OAuth
    class Token
      module Methods
        def from_access_token_param(request)
          request.parameters[:access_token]
        end

        def from_bearer_param(request)
          request.parameters[:bearer_token]
        end

        def from_bearer_authorization(request)
          pattern = /^Bearer /i
          header  = request.authorization
          token_from_header(header, pattern) if match?(header, pattern)
        end

        def from_basic_authorization(request)
          pattern = /^Basic /i
          header  = request.authorization
          token_from_basic_header(header, pattern) if match?(header, pattern)
        end

        private

        def token_from_basic_header(header, pattern)
          encoded_header = token_from_header(header, pattern)
          token, _ = decode_basic_credentials(encoded_header)
          token
        end

        def decode_basic_credentials(encoded_header)
          Base64.decode64(encoded_header).split(/:/, 2)
        end

        def token_from_header(header, pattern)
          header.gsub pattern, ''
        end

        def match?(header, pattern)
          header && header.match(pattern)
        end
      end

      extend Methods

      def self.from_request(request, *methods)
        methods.inject(nil) do |credentials, method|
          method = self.method(method) if method.is_a?(Symbol)
          credentials = method.call(request)
          break credentials unless credentials.blank?
        end
      end

      def self.authenticate(request, *methods)
        if token = from_request(request, *methods)
          AccessToken.by_token(token)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
doorkeeper-4.0.0.rc2 lib/doorkeeper/oauth/token.rb
doorkeeper-4.0.0.rc1 lib/doorkeeper/oauth/token.rb
doorkeeper-3.1.0 lib/doorkeeper/oauth/token.rb
doorkeeper-3.0.1 lib/doorkeeper/oauth/token.rb
doorkeeper-2.2.2 lib/doorkeeper/oauth/token.rb
doorkeeper-3.0.0 lib/doorkeeper/oauth/token.rb
doorkeeper-3.0.0.rc2 lib/doorkeeper/oauth/token.rb
doorkeeper-3.0.0.rc1 lib/doorkeeper/oauth/token.rb
doorkeeper-2.2.1 lib/doorkeeper/oauth/token.rb
doorkeeper-2.2.0 lib/doorkeeper/oauth/token.rb
doorkeeper-2.1.4 lib/doorkeeper/oauth/token.rb
doorkeeper-2.1.3 lib/doorkeeper/oauth/token.rb
doorkeeper-2.1.2 lib/doorkeeper/oauth/token.rb