Class: Core::Models::OAuth::Authorization

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/core/models/oauth/authorization.rb

Overview

An OAuth authorization is granted by a user to an application to access its personal data. The application then transforms it into an access token to be able to send it with further requests, so that we know the user has authorized the application to access its data.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#accountArkaaan::Account

Returns the account granting the authorization to access its data to the application.

Returns:

  • (Arkaaan::Account)

    the account granting the authorization to access its data to the application.



24
# File 'lib/core/models/oauth/authorization.rb', line 24

belongs_to :account, class_name: 'Core::Models::Account', inverse_of: :authorizations

#applicationCore::Models::OAuth::Application

Returns the application asking to access account’s data.

Returns:



27
# File 'lib/core/models/oauth/authorization.rb', line 27

belongs_to :application, class_name: 'Core::Models::OAuth::Application', inverse_of: :authorizations

#codeString

Returns the value corresponding to the authentication code in the RFC of OAuth2.0, kep for historic purpose.

Returns:

  • (String)

    the value corresponding to the authentication code in the RFC of OAuth2.0, kep for historic purpose.



17
# File 'lib/core/models/oauth/authorization.rb', line 17

field :code, type: String, default: ->{ SecureRandom.hex }

#expirationInteger

Returns the time, in seconds, after which the authorization is declared expired.

Returns:

  • (Integer)

    the time, in seconds, after which the authorization is declared expired.



20
# File 'lib/core/models/oauth/authorization.rb', line 20

field :expiration, type: Integer, default: 60

#tokenCore::Models::OAuth::AccessToken

Returns the access token used further in the application process to access private data of the account.

Returns:



30
# File 'lib/core/models/oauth/authorization.rb', line 30

has_many :tokens, class_name: 'Core::Models::OAuth::AccessToken', inverse_of: :authorization

Instance Method Details

#expired?Boolean

Checks if the current date is inferior to the creation date + expiration period

Returns:

  • (Boolean)

    TRUE if the authorization is expired, FALSE otherwise.



41
42
43
# File 'lib/core/models/oauth/authorization.rb', line 41

def expired?
  created_at.to_time.to_i + expiration < Time.now.to_i
end

#used?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/core/models/oauth/authorization.rb', line 45

def used?
  tokens.count > 0
end