Method: Rack::OAuth2::Server::AccessToken.get_token_for

Defined in:
lib/rack/oauth2/models/access_token.rb

.get_token_for(identity, client, scope, expires = nil) ⇒ Object

Get an access token (create new one if necessary).

You can set optional expiration in seconds. If zero or nil, token never expires.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rack/oauth2/models/access_token.rb', line 21

def get_token_for(identity, client, scope, expires = nil)
  raise ArgumentError, "Identity must be String or Integer" unless String === identity || Integer === identity
  scope = Utils.normalize_scope(scope) & client.scope # Only allowed scope

  token = collection.find_one({
    :$or=>[{:expires_at=>nil}, {:expires_at=>{:$gt=>Time.now.to_i}}],
    :identity=>identity, :scope=>scope,
    :client_id=>client.id, :revoked=>nil})

  unless token
    return create_token_for(client, scope, identity, expires)
  end
  Server.new_instance self, token
end