Method: OAuth2::AccessToken#initialize

Defined in:
lib/oauth2/access_token.rb

#initialize(client, token, opts = {}) ⇒ AccessToken

Note:

For “soon-to-expire”/“clock-skew” functionality see the ‘:expires_latency` option.

Note:

If no token is provided, the AccessToken will be considered invalid. This is to prevent the possibility of a token being accidentally created with no token value. If you want to create an AccessToken with no token value, you can pass in an empty string or nil for the token value. If you want to create an AccessToken with no token value and no refresh token, you can pass in an empty string or nil for the token value and nil for the refresh token, and ‘raise_errors: false`.

Initialize an AccessToken

Examples:

Verb-dependent Hash mode

# Send token in query for GET, in header for POST/DELETE, in body for PUT/PATCH
OAuth2::AccessToken.new(client, token, mode: {get: :query, post: :header, delete: :header, put: :body, patch: :body})

Parameters:

  • client (Client)

    the OAuth2::Client instance

  • token (String)

    the Access Token value (optional, may not be used in refresh flows)

  • opts (Hash) (defaults to: {})

    the options to create the Access Token with

Options Hash (opts):

  • :refresh_token (String) — default: nil

    the refresh_token value

  • :expires_in (FixNum, String) — default: nil

    the number of seconds in which the AccessToken will expire

  • :expires_at (FixNum, String) — default: nil

    the epoch time in seconds in which AccessToken will expire

  • :expires_latency (FixNum, String) — default: nil

    the number of seconds by which AccessToken validity will be reduced to offset latency, @version 2.0+

  • :mode (Symbol, Hash, or callable) — default: :header

    the transmission mode of the Access Token parameter value: either one of :header, :body or :query; or a Hash with verb symbols as keys mapping to one of these symbols (e.g., ‘:query, post: :header, delete: :header`); or a callable that accepts a request-verb parameter and returns one of these three symbols.

  • :header_format (String) — default: 'Bearer %s'

    the string format to use for the Authorization header

  • :param_name (String) — default: 'access_token'

    the parameter name to use for transmission of the Access Token value in :body or :query transmission mode

  • :token_name (String) — default: nil

    the name of the response parameter that identifies the access token When nil one of TOKEN_KEY_LOOKUP will be used



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/oauth2/access_token.rb', line 148

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]
      raise Error.new({
        error: "OAuth2::AccessToken has no token",
        error_description: "Options are: #{opts.inspect}",
      })
    elsif !OAuth2.config.silence_no_tokens_warning
      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_in.zero?
  @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",
  }
  @options[:token_name] = opts.delete(:token_name) if opts.key?(:token_name)

  @params = opts
end