Sha256: ad46ea5fddba89e87da20f96b4880908eddf45d2a49bcfb5803557ecb55ed91f

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

require 'json'
require 'authenticated_client'

module SoarAuthenticationToken
  class RemoteTokenGenerator
    def initialize(configuration)
      @configuration = configuration
      validate_configuration
    end

    def inject_store_provider(store_provider)
      #ignore the store provider since this generator does not use a store
    end

    def generate(authenticated_identifier:, flow_identifier: nil)
      client = authenticated_client(authenticated_identifier,flow_identifier)
      validate_and_extract_token_from_response(client.request)
    end

    private

    def authenticated_client(authenticated_identifier,flow_identifier)
      client = AuthenticatedClient::Client.new
      client.url = @configuration['generator-url']
      client.token = @configuration['generator-client-auth-token']
      client.verb = :post
      client.parameters = {'flow_identifier' => flow_identifier}
      client.body = { 'authenticated_identifier' => authenticated_identifier }
      client
    end

    def validate_and_extract_token_from_response(response)
      raise "Failure generating token with token generation service. Code #{response.code}" if '200' != response.code
      body = JSON.parse(response.body)
      raise 'Failure generating token by token service' if 'success' != body['status']
      raise 'Token service did not provide token' if body['data'].nil? or body['data']['token'].nil?
      body['data']['token']
    end

    def validate_configuration
      raise "'generator-url' must be configured" if @configuration['generator-url'].nil?
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
soar_authentication_token-5.0.2 lib/soar_authentication_token/providers/remote_token_generator.rb
soar_authentication_token-5.0.1 lib/soar_authentication_token/providers/remote_token_generator.rb
soar_authentication_token-5.0.0 lib/soar_authentication_token/providers/remote_token_generator.rb
soar_authentication_token-4.0.1 lib/soar_authentication_token/providers/remote_token_generator.rb
soar_authentication_token-4.0.0 lib/soar_authentication_token/providers/remote_token_generator.rb