Module: DoorkeeperMongodb::Mixins::MongoMapper::AccessTokenMixin::ClassMethods

Defined in:
lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb

Instance Method Summary collapse

Instance Method Details

#by_refresh_token(refresh_token) ⇒ Doorkeeper::AccessToken?

Returns an instance of the Doorkeeper::AccessToken with specific token value.

Parameters:

  • refresh_token (#to_s)

    refresh token value (any object that responds to ‘#to_s`)

Returns:



54
55
56
# File 'lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb', line 54

def by_refresh_token(refresh_token)
  where(refresh_token: refresh_token.to_s).first
end

#by_token(token) ⇒ Doorkeeper::AccessToken?

Returns an instance of the Doorkeeper::AccessToken with specific token value.

Parameters:

  • token (#to_s)

    token value (any object that responds to ‘#to_s`)

Returns:



40
41
42
# File 'lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb', line 40

def by_token(token)
  where(token: token.to_s).first
end

#find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token) ⇒ Doorkeeper::AccessToken

Looking for not expired AccessToken record with a matching set of scopes that belongs to specific Application and Resource Owner. If it doesn’t exists - then creates it.

Parameters:

  • application (Doorkeeper::Application)

    Application instance

  • resource_owner_id (ActiveRecord::Base, Integer)

    Resource Owner model instance or it’s ID

  • scopes (#to_s)

    set of scopes (any object that responds to ‘#to_s`)

  • expires_in (Integer)

    token lifetime in seconds

  • use_refresh_token (Boolean)

    whether to use the refresh token

Returns:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb', line 137

def find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token)
  if Doorkeeper.configuration.reuse_access_token
    access_token = matching_token_for(application, resource_owner_id, scopes)
    if access_token && !access_token.expired?
      return access_token
    end
  end

  create!(
    application_id: application.try(:id),
    resource_owner_id: resource_owner_id,
    scopes: scopes.to_s,
    expires_in: expires_in,
    use_refresh_token: use_refresh_token
  )
end

#last_authorized_token_for(application_id, resource_owner_id) ⇒ Doorkeeper::AccessToken?

Looking for not revoked Access Token record that belongs to specific Application and Resource Owner.

Parameters:

  • application_id (Integer)

    ID of the Application model instance

  • resource_owner_id (Integer)

    ID of the Resource Owner model instance

Returns:



165
166
167
168
169
170
# File 'lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb', line 165

def last_authorized_token_for(application_id, resource_owner_id)
  send(order_method, created_at_desc).
    where(application_id: application_id,
          resource_owner_id: resource_owner_id,
          revoked_at: nil).first
end

#matching_token_for(application, resource_owner_or_id, scopes) ⇒ Doorkeeper::AccessToken?

Looking for not expired Access Token with a matching set of scopes that belongs to specific Application and Resource Owner.

Parameters:

  • application (Doorkeeper::Application)

    Application instance

  • resource_owner_or_id (ActiveRecord::Base, Integer)

    Resource Owner model instance or it’s ID

  • scopes (String, Doorkeeper::OAuth::Scopes)

    set of scopes

Returns:



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb', line 86

def matching_token_for(application, resource_owner_or_id, scopes)
  resource_owner_id = if resource_owner_or_id.respond_to?(:to_key)
                        resource_owner_or_id.id
                      else
                        resource_owner_or_id
                      end
  token = last_authorized_token_for(application.try(:id), resource_owner_id)
  if token && scopes_match?(token.scopes, scopes, application.try(:scopes))
    token
  end
end

#revoke_all_for(application_id, resource_owner) ⇒ Object

Revokes AccessToken records that have not been revoked and associated with the specific Application and Resource Owner.

Parameters:

  • application_id (Integer)

    ID of the Application

  • resource_owner (ActiveRecord::Base)

    instance of the Resource Owner model



66
67
68
69
70
71
# File 'lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb', line 66

def revoke_all_for(application_id, resource_owner)
  where(application_id: application_id,
        resource_owner_id: resource_owner.id,
        revoked_at: nil).
    each(&:revoke)
end

#scopes_match?(token_scopes, param_scopes, app_scopes) ⇒ Boolean

Checks whether the token scopes match the scopes from the parameters or Application scopes (if present).

Parameters:

  • token_scopes (#to_s)

    set of scopes (any object that responds to ‘#to_s`)

  • param_scopes (String)

    scopes from params

  • app_scopes (String)

    Application scopes

Returns:

  • (Boolean)

    true if all scopes and blank or matches and false in other cases



111
112
113
114
115
116
117
118
# File 'lib/doorkeeper-mongodb/mixins/mongo_mapper/access_token_mixin.rb', line 111

def scopes_match?(token_scopes, param_scopes, app_scopes)
  (!token_scopes.present? && !param_scopes.present?) ||
    Doorkeeper::OAuth::Helpers::ScopeChecker.match?(
      token_scopes.to_s,
      param_scopes,
      app_scopes
    )
end