Sha256: d1675182c6b0f6ee1c2440bfbf36d43da6847a481b699d448bd09b4ee2673ece

Contents?: true

Size: 1.5 KB

Versions: 14

Compression:

Stored size: 1.5 KB

Contents

module Doorkeeper
  module OAuth
    class RefreshTokenRequest
      include Doorkeeper::Validations

      validate :token,  :error => :invalid_request
      validate :client, :error => :invalid_client

      attr_accessor :server, :refresh_token, :client, :access_token

      # TODO: refresh token can receive scope as parameters
      def initialize(server, refresh_token, client)
        @server        = server
        @refresh_token = refresh_token
        @client        = client
      end

      def authorize
        validate
        @response = if valid?
          revoke_and_create_access_token
          OAuth::TokenResponse.new access_token
        else
          OAuth::ErrorResponse.from_request(self)
        end
      end

      def valid?
        self.error.nil?
      end

    private

      def revoke_and_create_access_token
        refresh_token.revoke
        create_access_token
      end

      def create_access_token
        @access_token = Doorkeeper::AccessToken.create!({
          :application_id    => refresh_token.application_id,
          :resource_owner_id => refresh_token.resource_owner_id,
          :scopes            => refresh_token.scopes_string,
          :expires_in        => server.access_token_expires_in,
          :use_refresh_token => true
        })
      end

      def validate_token
        refresh_token.present? && !refresh_token.revoked?
      end

      def validate_client
        client.present? && refresh_token.application_id == client.id
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
doorkeeper-0.7.4 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.7.3 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.7.2 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.7.1 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.7.0 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.6.7 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.6.6 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.6.5 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.6.4 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.6.3 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.6.2 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.6.1 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.6.0 lib/doorkeeper/oauth/refresh_token_request.rb
doorkeeper-0.6.0.rc1 lib/doorkeeper/oauth/refresh_token_request.rb