Class: Doorkeeper::AccessToken

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
Models::Mongoid::Version
Includes:
Models::Accessible, Models::Expirable, Models::Mongoid::Scopes, Models::Revocable, Models::Scopes, OAuth::Helpers, MongoMapper::Document, Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/doorkeeper/models/access_token.rb,
lib/doorkeeper/models/mongoid2/access_token.rb,
lib/doorkeeper/models/mongoid3_4/access_token.rb,
lib/doorkeeper/models/mongo_mapper/access_token.rb,
lib/doorkeeper/models/active_record/access_token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Mongoid::Version

mongoid3?, mongoid4?

Methods included from Models::Mongoid::Scopes

included

Methods included from Models::Scopes

included

Methods included from Models::Accessible

#accessible?

Methods included from Models::Revocable

#revoke, #revoked?

Methods included from Models::Expirable

#expired?, #expires_in_seconds

Instance Attribute Details

#use_refresh_tokenObject

Returns the value of attribute use_refresh_token.



17
18
19
# File 'lib/doorkeeper/models/access_token.rb', line 17

def use_refresh_token
  @use_refresh_token
end

Class Method Details

.authenticate(token) ⇒ Object



28
29
30
# File 'lib/doorkeeper/models/access_token.rb', line 28

def self.authenticate(token)
  where(token: token).first
end

.by_refresh_token(refresh_token) ⇒ Object



32
33
34
# File 'lib/doorkeeper/models/access_token.rb', line 32

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

.create_indexesObject



43
44
45
46
# File 'lib/doorkeeper/models/mongo_mapper/access_token.rb', line 43

def self.create_indexes
  ensure_index :token, unique: true
  ensure_index [[:refresh_token, 1]], unique: true, sparse: true
end

.find_or_create_for(application, resource_owner_id, scopes, expires_in, use_refresh_token) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/doorkeeper/models/access_token.rb', line 53

def self.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

.lastObject



19
20
21
# File 'lib/doorkeeper/models/mongo_mapper/access_token.rb', line 19

def self.last
  self.sort(:created_at).last
end

.matching_token_for(application, resource_owner_or_id, scopes) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/doorkeeper/models/access_token.rb', line 43

def self.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)
  token if token && ScopeChecker.matches?(token.scopes, scopes)
end

.revoke_all_for(application_id, resource_owner) ⇒ Object



36
37
38
39
40
41
# File 'lib/doorkeeper/models/access_token.rb', line 36

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

Instance Method Details

#acceptable?(scopes) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/doorkeeper/models/access_token.rb', line 92

def acceptable?(scopes)
  accessible? && includes_scope?(scopes)
end

#as_json(options = {}) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/doorkeeper/models/access_token.rb', line 77

def as_json(options = {})
  {
    resource_owner_id: resource_owner_id,
    scopes: scopes,
    expires_in_seconds: expires_in_seconds,
    application: { uid: application.try(:uid) }
  }
end

#refresh_tokenObject



35
36
37
# File 'lib/doorkeeper/models/mongoid2/access_token.rb', line 35

def refresh_token
  self[:refresh_token]
end

#same_credential?(access_token) ⇒ Boolean

It indicates whether the tokens have the same credential

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/doorkeeper/models/access_token.rb', line 87

def same_credential?(access_token)
  application_id == access_token.application_id &&
    resource_owner_id == access_token.resource_owner_id
end

#scopes=(value) ⇒ Object



15
16
17
# File 'lib/doorkeeper/models/mongo_mapper/access_token.rb', line 15

def scopes=(value)
  write_attribute :scopes, value if value.present?
end

#token_typeObject



69
70
71
# File 'lib/doorkeeper/models/access_token.rb', line 69

def token_type
  'bearer'
end

#use_refresh_token?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/doorkeeper/models/access_token.rb', line 73

def use_refresh_token?
  use_refresh_token
end