module NoBrainer::Simple::OAuth2::AccessToken

Includes all the required API, associations, validations and callbacks

Public Class Methods

create_for(client, resource_owner, scopes = nil) click to toggle source

Create a new AccessToken object

@param client [Object] Client instance @param resource_owner [Object] ResourceOwner instance @param scopes [String] set of scopes

@return [AccessToken] AccessToken object

# File lib/nobrainer_simple_oauth2/mixins/access_token.rb, line 43
def self.create_for(client, resource_owner, scopes = nil)
  create(
    client_id: client.id,
    resource_owner_id: resource_owner.id,
    scopes: scopes
  )
end

Public Instance Methods

expired?() click to toggle source

Indicates whether the object is expired (`#expires_at` present and expiration time has come)

@return [Boolean] true if object expired and false in other case

# File lib/nobrainer_simple_oauth2/mixins/access_token.rb, line 55
def expired?
  expires_at && Time.now.utc > expires_at
end
revoke!(revoked_at = Time.now.utc) click to toggle source

Revokes the object (updates `:revoked_at` attribute setting its value to the specific time)

@param clock [Time] time object

# File lib/nobrainer_simple_oauth2/mixins/access_token.rb, line 71
def revoke!(revoked_at = Time.now.utc)
  update!(revoked_at: revoked_at)
end
revoked?() click to toggle source

Indicates whether the object has been revoked

@return [Boolean] true if revoked, false in other case

# File lib/nobrainer_simple_oauth2/mixins/access_token.rb, line 63
def revoked?
  revoked_at && revoked_at <= Time.now.utc
end
setup_expiration() click to toggle source

Set lifetime for token value during creating a new record

@return clock [Time] time object

# File lib/nobrainer_simple_oauth2/mixins/access_token.rb, line 94
def setup_expiration
  expires_in = ::Simple::OAuth2.config.access_token_lifetime.to_i
  self.expires_at = Time.now.utc + expires_in if expires_at.nil? && !expires_in.nil?
end
to_bearer_token() click to toggle source

Exposes token object to Bearer token

@return [Hash] bearer token instance

# File lib/nobrainer_simple_oauth2/mixins/access_token.rb, line 79
def to_bearer_token
  {
    access_token: token,
    expires_in: expires_at && ::Simple::OAuth2.config.access_token_lifetime.to_i,
    refresh_token: refresh_token,
    scope: scopes
  }
end