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, shared_secret:, host:, protocol: 'https') ⇒ JwtService

Returns a new instance of JwtService.



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

def initialize(consumer_key: nil, shared_secret:, host:, protocol: 'https')
  @consumer_key = consumer_key
  @host = host
  @shared_secret = shared_secret
  @protocol = protocol
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, resource_id: nil) ⇒ Object



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

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

    # FIXME: This is currently required on quiz_web
    user: {
      full_name: 'Fake Full Name',
      given_name: 'Fake',
      email: '[email protected]',
      id: 1
    }
  }
  payload[:resource_id] = resource_id if resource_id

  JWT.encode(payload, shared_secret, HASHING_ALGORITHM)
end