Class: Core::Services::Tokens

Inherits:
Base
  • Object
show all
Includes:
Singleton
Defined in:
lib/core/services/tokens.rb

Overview

Service handling every operations concerning access tokens. This should mainly be used in the authentication backend as we should be the only ones to manage tokens.

Author:

Instance Method Summary collapse

Methods inherited from Base

#bad_request_err, #forbidden_err, #require_parameters, #unknown_err

Instance Method Details

#create_from_authorization(client_id: nil, client_secret: nil, authorization_code: nil, **_ignored) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/core/services/tokens.rb', line 11

def create_from_authorization(client_id: nil, client_secret: nil, authorization_code: nil, **_ignored)
  authorization = Core.svc.authorizations.get_by_credentials(
    client_id: client_id,
    client_secret: client_secret,
    authorization_code: authorization_code
  )
  raise forbidden_err(field: 'authorization_code', error: 'used') if authorization.used?

  created = Core::Models::OAuth::AccessToken.create(authorization: authorization)
  Core::Decorators::Token.new(created)
end

#create_from_token(client_id: nil, client_secret: nil, token: nil, **_ignored) ⇒ Object

Refreshes the token for the next request the client wants to issue by re-creating it from the previous token to add it to the tokens chain.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/core/services/tokens.rb', line 26

def create_from_token(client_id: nil, client_secret: nil, token: nil, **_ignored)
  token = get_by_value(token: token)
  authorization = Core.svc.authorizations.get_by_credentials(
    client_id: client_id,
    client_secret: client_secret,
    authorization_code: token.authorization.code
  )
  raise forbidden_err(field: 'token', error: 'used') unless token.generated.nil?

  created = Core::Models::OAuth::AccessToken.create(
    generator: token,
    authorization: authorization
  )
  Core::Decorators::Token.new(created)
end

#get_by_value(token: nil, **_ignored) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/core/services/tokens.rb', line 42

def get_by_value(token: nil, **_ignored)
  require_parameters token: token
  token = Core::Models::OAuth::AccessToken.find_by(value: token)
  raise unknown_err(field: 'token') if token.nil?

  Core::Decorators::Token.new(token)
end