Class: OmniAuth::WithingsOauth2::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/omniauth/withings_oauth2/access_token.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

TOKEN_KEYS_STR =
%w[access_token id_token token accessToken idToken].freeze
TOKEN_KEYS_SYM =
%i[access_token id_token token accessToken idToken].freeze
TOKEN_KEY_LOOKUP =
TOKEN_KEYS_STR + TOKEN_KEYS_SYM

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize an AccessToken

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) — default: :header

    the transmission mode of the Access Token parameter value one of :header, :body or :query

  • :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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 63

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 10

def client
  @client
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



10
11
12
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 10

def expires_at
  @expires_at
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



10
11
12
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 10

def expires_in
  @expires_in
end

#expires_latencyObject (readonly)

Returns the value of attribute expires_latency.



10
11
12
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 10

def expires_latency
  @expires_latency
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 11

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 10

def params
  @params
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



11
12
13
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 11

def refresh_token
  @refresh_token
end

#responseObject

Returns the value of attribute response.



11
12
13
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 11

def response
  @response
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 10

def token
  @token
end

Class Method Details

.from_hash(client, hash) ⇒ AccessToken

Initializes an AccessToken from a Hash

Parameters:

  • client (Client)

    the OAuth2::Client instance

  • hash (Hash)

    a hash of AccessToken property values

Options Hash (hash):

  • 'access_token', (String, Symbol)

    ‘id_token’, ‘token’, :access_token, :id_token, or :token the access token

Returns:



20
21
22
23
24
25
26
27
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 20

def from_hash(client, hash)
  fresh = hash["body"].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

.from_kvform(client, kvform) ⇒ AccessToken

Initializes an AccessToken from a key/value application/x-www-form-urlencoded string

Parameters:

  • client (Client)

    the OAuth2::Client instance

  • kvform (String)

    the application/x-www-form-urlencoded string

Returns:



34
35
36
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 34

def from_kvform(client, kvform)
  from_hash(client, Rack::Utils.parse_query(kvform))
end

Instance Method Details

#[](key) ⇒ Object

Indexer to additional params present in token response

Parameters:

  • key (String)

    entry key to Hash



96
97
98
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 96

def [](key)
  @params[key]
end

#delete(path, opts = {}, &block) ⇒ Object

Make a DELETE request with the Access Token

See Also:



185
186
187
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 185

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

#expired?Boolean

Whether or not the token is expired

Returns:

  • (Boolean)


110
111
112
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 110

def expired?
  expires? && (expires_at <= Time.now.to_i)
end

#expires?Boolean

Whether or not the token expires

Returns:

  • (Boolean)


103
104
105
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 103

def expires?
  !!@expires_at
end

#get(path, opts = {}, &block) ⇒ Object

Make a GET request with the Access Token

See Also:



157
158
159
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 157

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

#headersObject

Get the headers hash (includes Authorization token)



190
191
192
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 190

def headers
  {'Authorization' => options[:header_format] % token}
end

#patch(path, opts = {}, &block) ⇒ Object

Make a PATCH request with the Access Token

See Also:



178
179
180
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 178

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

#post(path, opts = {}, &block) ⇒ Object

Make a POST request with the Access Token

See Also:



164
165
166
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 164

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

#put(path, opts = {}, &block) ⇒ Object

Make a PUT request with the Access Token

See Also:



171
172
173
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 171

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

#refresh(params = {}, access_token_opts = {}) ⇒ AccessToken Also known as: refresh!

Note:

options should be carried over to the new AccessToken

Refreshes the current Access Token

Returns:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 118

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

#request(verb, path, opts = {}, &block) ⇒ Object

Make a request with the Access Token

Parameters:

  • verb (Symbol)

    the HTTP request method

  • path (String)

    the HTTP URL path of the request

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

    the options to make the request with @see Client#request



149
150
151
152
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 149

def request(verb, path, opts = {}, &block)
  configure_authentication!(opts)
  @client.request(verb, path, opts, &block)
end

#to_hashHash

Convert AccessToken to a hash which can be used to rebuild itself with AccessToken.from_hash

Returns:

  • (Hash)

    a hash of AccessToken property values



139
140
141
# File 'lib/omniauth/withings_oauth2/access_token.rb', line 139

def to_hash
  params.merge(access_token: token, refresh_token: refresh_token, expires_at: expires_at)
end