class OAuth2::AccessToken

rubocop:disable Metrics/ClassLength

def [](key)

Parameters:
  • key (String) -- entry key to Hash
def [](key)
  @params[key]
end

def configure_authentication!(opts)

def configure_authentication!(opts)
  case options[:mode]
  when :header
    opts[:headers] ||= {}
    opts[:headers].merge!(headers)
  when :query
    opts[:params] ||= {}
    opts[:params][options[:param_name]] = token
  when :body
    opts[:body] ||= {}
    if opts[:body].is_a?(Hash)
      opts[:body][options[:param_name]] = token
    else
      opts[:body] += "&#{options[:param_name]}=#{token}"
    end
    # @todo support for multi-part (file uploads)
  else
    raise("invalid :mode option of #{options[:mode]}")
  end
end

def convert_expires_at(expires_at)

def convert_expires_at(expires_at)
  Time.iso8601(expires_at.to_s).to_i
rescue ArgumentError
  expires_at.to_i
end

def delete(path, opts = {}, &block)

Other tags:
    See: AccessToken#request -
def delete(path, opts = {}, &block)
  request(:delete, path, opts, &block)
end

def expired?

Returns:
  • (Boolean) -
def expired?
  expires? && (expires_at <= Time.now.to_i)
end

def expires?

Returns:
  • (Boolean) -
def expires?
  !!@expires_at
end

def extra_tokens_warning(supported_keys, key)

Having too many is sus, and may lead to bugs. Having none is fine (e.g. refresh flow doesn't need a token).
def extra_tokens_warning(supported_keys, key)
  return if OAuth2.config.silence_extra_tokens_warning
  return if supported_keys.length <= 1
  warn("OAuth2::AccessToken.from_hash: `hash` contained more than one 'token' key (#{supported_keys}); using #{key.inspect}.")
end

def from_hash(client, hash)

Returns:
  • (AccessToken) - the initialized AccessToken

Options Hash: (**hash)
  • 'access_token', (String, Symbol) -- 'id_token', 'token', :access_token, :id_token, or :token the access token

Parameters:
  • hash (Hash) -- a hash of AccessToken property values
  • client (Client) -- the OAuth2::Client instance
def from_hash(client, hash)
  fresh = hash.dup
  supported_keys = TOKEN_KEY_LOOKUP & fresh.keys
  key = supported_keys[0]
  extra_tokens_warning(supported_keys, key)
  token = fresh.delete(key)
  new(client, token, fresh)
end

def from_kvform(client, kvform)

Returns:
  • (AccessToken) - the initialized AccessToken

Parameters:
  • kvform (String) -- the application/x-www-form-urlencoded string
  • client (Client) -- the OAuth2::Client instance
def from_kvform(client, kvform)
  from_hash(client, Rack::Utils.parse_query(kvform))
end

def get(path, opts = {}, &block)

Other tags:
    See: AccessToken#request -
def get(path, opts = {}, &block)
  request(:get, path, opts, &block)
end

def headers

Get the headers hash (includes Authorization token)
def headers
  {'Authorization' => options[:header_format] % token}
end

def initialize(client, token, opts = {})

Options Hash: (**opts)
  • :param_name (String) -- the parameter name to use for transmission of the
  • :header_format (String) -- the string format to use for the Authorization header
  • :mode (Symbol) -- the transmission mode of the Access Token parameter value
  • :expires_latency (FixNum, String) -- the number of seconds by which AccessToken validity will be reduced to offset latency, @version 2.0+
  • :expires_at (FixNum, String) -- the epoch time in seconds in which AccessToken will expire
  • :expires_in (FixNum, String) -- the number of seconds in which the AccessToken will expire
  • :refresh_token (String) -- the refresh_token value

Parameters:
  • opts (Hash) -- the options to create the Access Token with
  • token (String) -- the Access Token value (optional, may not be used in refresh flows)
  • client (Client) -- the OAuth2::Client instance
def initialize(client, token, opts = {})
  @client = client
  @token = token.to_s
  opts = opts.dup
  %i[refresh_token expires_in expires_at expires_latency].each do |arg|
    instance_variable_set("@#{arg}", opts.delete(arg) || opts.delete(arg.to_s))
  end
  no_tokens = (@token.nil? || @token.empty?) && (@refresh_token.nil? || @refresh_token.empty?)
  if no_tokens
    if @client.options[:raise_errors]
      error = Error.new(opts)
      raise(error)
    else
      warn('OAuth2::AccessToken has no token')
    end
  end
  # @option opts [Fixnum, String] :expires is deprecated
  @expires_in ||= opts.delete('expires')
  @expires_in &&= @expires_in.to_i
  @expires_at &&= convert_expires_at(@expires_at)
  @expires_latency &&= @expires_latency.to_i
  @expires_at ||= Time.now.to_i + @expires_in if @expires_in
  @expires_at -= @expires_latency if @expires_latency
  @options = {mode: opts.delete(:mode) || :header,
              header_format: opts.delete(:header_format) || 'Bearer %s',
              param_name: opts.delete(:param_name) || 'access_token'}
  @params = opts
end

def patch(path, opts = {}, &block)

Other tags:
    See: AccessToken#request -
def patch(path, opts = {}, &block)
  request(:patch, path, opts, &block)
end

def post(path, opts = {}, &block)

Other tags:
    See: AccessToken#request -
def post(path, opts = {}, &block)
  request(:post, path, opts, &block)
end

def put(path, opts = {}, &block)

Other tags:
    See: AccessToken#request -
def put(path, opts = {}, &block)
  request(:put, path, opts, &block)
end

def refresh(params = {}, access_token_opts = {})

Other tags:
    Note: - options should be carried over to the new AccessToken

Returns:
  • (AccessToken) - a new AccessToken
def refresh(params = {}, access_token_opts = {})
  raise('A refresh_token is not available') unless refresh_token
  params[:grant_type] = 'refresh_token'
  params[:refresh_token] = refresh_token
  new_token = @client.get_token(params, access_token_opts)
  new_token.options = options
  if new_token.refresh_token
    # Keep it, if there is one
  else
    new_token.refresh_token = refresh_token
  end
  new_token
end

def request(verb, path, opts = {}, &block)

Other tags:
    See: Client#request -

Parameters:
  • opts (Hash) -- the options to make the request with
  • path (String) -- the HTTP URL path of the request
  • verb (Symbol) -- the HTTP request method
def request(verb, path, opts = {}, &block)
  configure_authentication!(opts)
  @client.request(verb, path, opts, &block)
end

def to_hash

Returns:
  • (Hash) - a hash of AccessToken property values
def to_hash
  params.merge(access_token: token, refresh_token: refresh_token, expires_at: expires_at)
end