Sha256: 9efcd70c948176ff8741a7aac9e54ce81682dbb56e8db82398de95fb8eb08caa

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'soar_xt'
require 'jwt'

module SoarAuthenticationToken
  class TokenValidator
    DEFAULT_CONFIGURATION = {
      'mode' => 'local',
      'public_key' => '',
      'url' => ''
    } unless defined? DEFAULT_CONFIGURATION; DEFAULT_CONFIGURATION.freeze

    def initialize(configuration)
      @configuration = merge_with_default_configuration(configuration)
      validate_configuration
      @public_key = OpenSSL::PKey::EC.new(@configuration['public_key'])
      @public_key.private_key = nil
    end

    def validate(authentication_token)
      return validate_locally(authentication_token) if 'local' == @configuration['mode']
      return validate_remotely(authentication_token)
    end

    private

    def validate_locally(authentication_token)
      decoded_token_payload = decode(authentication_token)
      return [false, nil] if expired?(decoded_token_payload[0]['issue_time'])
      [true, decoded_token_payload[0]['authenticated_identifier']]
    rescue JWT::VerificationError, JWT::DecodeError
      [false, nil]
    end

    def validate_remotely(authentication_token)
      [true, 'uuid']
    end

    def validate_configuration

    end

    def merge_with_default_configuration(configuration)
      Hash.deep_merge(DEFAULT_CONFIGURATION,configuration)
    end

    def decode(authentication_token)
      JWT.decode(authentication_token, @public_key, true, { :algorithm => 'ES512' })
    end

    def expired?(issue_time)
      (Time.parse(issue_time) + 604800) < Time.now #TODO make this configurable
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
soar_authentication_token-0.0.2 lib/soar_authentication_token/token_validator.rb