Sha256: 8e0372be7f6c1722b45d8a1f42faf64251a2bbdb830f6b3325f2c6642bd1e371

Contents?: true

Size: 773 Bytes

Versions: 6

Compression:

Stored size: 773 Bytes

Contents

module QuizApiClient::Services
  class JwtService
    HASHING_ALGORITHM = 'HS512'.freeze

    attr_reader :consumer_key, :host, :shared_secret, :protocol

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

    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
      }
      payload[:user] = { uuid: uuid } if uuid

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
quiz_api_client-2.3.1 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-2.3.0 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-2.2.0 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-2.1.0 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-2.0.0 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-1.1.0 lib/quiz_api_client/services/jwt_service.rb