Class: QuizApiClient::Services::JwtService

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/quiz_api_client/services/jwt_service.rb

Constant Summary collapse

HASHING_ALGORITHM =
'HS512'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ JwtService

Returns a new instance of JwtService.



10
11
12
# File 'lib/quiz_api_client/services/jwt_service.rb', line 10

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/quiz_api_client/services/jwt_service.rb', line 7

def config
  @config
end

Instance Method Details

#grant_permission(scope:, exp: nil, uuid: nil, **additional_fields) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/quiz_api_client/services/jwt_service.rb', line 14

def grant_permission(scope:, exp: nil, uuid: nil, **additional_fields)
  payload = {
    host: URI.parse("#{protocol}://#{host}").host,
    consumer_key: consumer_key,
    scope: scope,
    exp: exp || Time.now.utc.to_i + 60,
    **additional_fields
  }

  if uuid
    payload[:user] ||= {}
    payload[:user][:uuid] = uuid
  end

  JWT.encode(payload, config.shared_secret, HASHING_ALGORITHM)
end