Sha256: c356e358ad63e27e6cf9c2384140adea0d1cab691efb5847904d3e9446fbb6e1

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'devise/strategies/base'

module Devise
  module Strategies
    class Oauth2Providable < Base
      def valid?
        @req = Rack::OAuth2::Server::Resource::Bearer::Request.new(env)
        @req.oauth2?
      end
      def authenticate!
        token = AccessToken.valid.find_by_token access_token
        resource = token ? token.user : nil
        if validate(resource)
          success! resource
        elsif !halted?
          fail(:invalid_token)
        end
      end

      private
      def access_token
        tokens = [@req.access_token_in_header, @req.access_token_in_payload].compact
        raise 'invalid request: access token exists in header and payload' if tokens.size > 1
        tokens.first
      end
      # Simply invokes valid_for_authentication? with the given block and deal with the result.
      def validate(resource, &block)
        result = resource && resource.valid_for_authentication?(&block)

        case result
        when String, Symbol
          fail!(result)
          false
        when TrueClass
          true
        else
          result
        end
      end
    end
  end
end

Warden::Strategies.add(:oauth2_providable, Devise::Strategies::Oauth2Providable)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
devise_oauth2_providable-0.2.2 lib/devise_oauth2_providable/strategy.rb