Sha256: 85a5f6cccde738ec7722e5500e3b189447853f7f4c1eb41a814119de80f75726

Contents?: true

Size: 894 Bytes

Versions: 8

Compression:

Stored size: 894 Bytes

Contents

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

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

    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

    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
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
quiz_api_client-3.2.0 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-3.1.0 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-3.0.0 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-2.9.0 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-2.8.0 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-2.7.0 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-2.5.2 lib/quiz_api_client/services/jwt_service.rb
quiz_api_client-2.5.1 lib/quiz_api_client/services/jwt_service.rb