Sha256: 08fe9bf42f9dfc689c3012956d92c4912e82270e00352e4ba1811bf40be07919

Contents?: true

Size: 906 Bytes

Versions: 3

Compression:

Stored size: 906 Bytes

Contents

require 'access_token_agent/missing_access_token'
require 'access_token_agent/missing_token_type'
require 'access_token_agent/unsupported_token_type_error'

module AccessTokenAgent
  class Token
    attr_reader :value, :expires_at

    EXPIRY_MARGIN = 60 # seconds

    def initialize(auth_response)
      validate_response(auth_response)

      @value = auth_response['access_token']
      @expires_at = Time.now + auth_response['expires_in']
    end

    def valid?
      @expires_at - EXPIRY_MARGIN > Time.now
    end

    private

    def validate_response(auth_response)
      raise MissingTokenType if auth_response['token_type'].nil?
      unless auth_response['token_type'].downcase == 'bearer'
        raise UnsupportedTokenTypeError, auth_response['token_type']
      end

      token = auth_response['access_token']
      raise MissingAccessToken if token.nil? || token.empty?
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
access_token_agent-4.0.0 lib/access_token_agent/token.rb
access_token_agent-3.5.0 lib/access_token_agent/token.rb
access_token_agent-3.4.0 lib/access_token_agent/token.rb