Module: Doorkeeper::AccessTokenMixin::ClassMethods

Defined in:
lib/doorkeeper/models/access_token_mixin.rb

Instance Method Summary collapse

Instance Method Details

#by_refresh_token(refresh_token) ⇒ Object


37
38
39
# File 'lib/doorkeeper/models/access_token_mixin.rb', line 37

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

#by_token(token) ⇒ Object


33
34
35
# File 'lib/doorkeeper/models/access_token_mixin.rb', line 33

def by_token(token)
  where(token: token).limit(1).to_a.first
end

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


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/doorkeeper/models/access_token_mixin.rb', line 69

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) ⇒ Object


85
86
87
88
89
90
91
92
93
# File 'lib/doorkeeper/models/access_token_mixin.rb', line 85

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

#matching_token_for(application, resource_owner_or_id, scopes) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/doorkeeper/models/access_token_mixin.rb', line 48

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


41
42
43
44
45
46
# File 'lib/doorkeeper/models/access_token_mixin.rb', line 41

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

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

Returns:

  • (Boolean)

60
61
62
63
64
65
66
67
# File 'lib/doorkeeper/models/access_token_mixin.rb', line 60

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