Class: QuizApiClient::Services::JwtService

Inherits:
Object
  • Object
show all
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(consumer_key: nil, consumer_request_id: nil, shared_secret:, host:, protocol: 'https') ⇒ JwtService

Returns a new instance of JwtService.



7
8
9
10
11
12
13
# File 'lib/quiz_api_client/services/jwt_service.rb', line 7

def initialize(consumer_key: nil, consumer_request_id: nil, shared_secret:, host:, protocol: 'https')
  @consumer_key = consumer_key
  @host = host
  @shared_secret = shared_secret
  @protocol = protocol
  @consumer_request_id = consumer_request_id
end

Instance Attribute Details

#consumer_keyObject (readonly)

Returns the value of attribute consumer_key.



5
6
7
# File 'lib/quiz_api_client/services/jwt_service.rb', line 5

def consumer_key
  @consumer_key
end

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/quiz_api_client/services/jwt_service.rb', line 5

def host
  @host
end

#protocolObject (readonly)

Returns the value of attribute protocol.



5
6
7
# File 'lib/quiz_api_client/services/jwt_service.rb', line 5

def protocol
  @protocol
end

#shared_secretObject (readonly)

Returns the value of attribute shared_secret.



5
6
7
# File 'lib/quiz_api_client/services/jwt_service.rb', line 5

def shared_secret
  @shared_secret
end

Instance Method Details

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



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

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, shared_secret, HASHING_ALGORITHM)
end