Class: OAuth2::AccessToken
- Inherits:
-
Object
- Object
- OAuth2::AccessToken
- Defined in:
- lib/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
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#expires_in ⇒ Object
readonly
Returns the value of attribute expires_in.
-
#expires_latency ⇒ Object
readonly
Returns the value of attribute expires_latency.
-
#options ⇒ Object
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#response ⇒ Object
Returns the value of attribute response.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Class Method Summary collapse
-
.from_hash(client, hash) ⇒ AccessToken
Initializes an AccessToken from a Hash.
-
.from_kvform(client, kvform) ⇒ AccessToken
Initializes an AccessToken from a key/value application/x-www-form-urlencoded string.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Indexer to additional params present in token response.
-
#delete(path, opts = {}, &block) ⇒ Object
Make a DELETE request with the Access Token.
-
#expired? ⇒ Boolean
Whether or not the token is expired.
-
#expires? ⇒ Boolean
Whether or not the token expires.
-
#get(path, opts = {}, &block) ⇒ Object
Make a GET request with the Access Token.
-
#headers ⇒ Object
Get the headers hash (includes Authorization token).
-
#initialize(client, token, opts = {}) ⇒ AccessToken
constructor
Initialize an AccessToken.
-
#patch(path, opts = {}, &block) ⇒ Object
Make a PATCH request with the Access Token.
-
#post(path, opts = {}, &block) ⇒ Object
Make a POST request with the Access Token.
-
#put(path, opts = {}, &block) ⇒ Object
Make a PUT request with the Access Token.
-
#refresh(params = {}, access_token_opts = {}) ⇒ AccessToken
(also: #refresh!)
Refreshes the current Access Token.
-
#request(verb, path, opts = {}, &block) ⇒ Object
Make a request with the Access Token.
-
#to_hash ⇒ Hash
Convert AccessToken to a hash which can be used to rebuild itself with AccessToken.from_hash.
Constructor Details
#initialize(client, token, opts = {}) ⇒ AccessToken
Initialize an AccessToken
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/oauth2/access_token.rb', line 53 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.[:raise_errors] error = Error.new(opts) raise(error) else warn('OAuth2::AccessToken has no token') end end @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
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/oauth2/access_token.rb', line 9 def client @client end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
9 10 11 |
# File 'lib/oauth2/access_token.rb', line 9 def expires_at @expires_at end |
#expires_in ⇒ Object (readonly)
Returns the value of attribute expires_in.
9 10 11 |
# File 'lib/oauth2/access_token.rb', line 9 def expires_in @expires_in end |
#expires_latency ⇒ Object (readonly)
Returns the value of attribute expires_latency.
9 10 11 |
# File 'lib/oauth2/access_token.rb', line 9 def expires_latency @expires_latency end |
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/oauth2/access_token.rb', line 10 def @options end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
9 10 11 |
# File 'lib/oauth2/access_token.rb', line 9 def params @params end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
10 11 12 |
# File 'lib/oauth2/access_token.rb', line 10 def refresh_token @refresh_token end |
#response ⇒ Object
Returns the value of attribute response.
10 11 12 |
# File 'lib/oauth2/access_token.rb', line 10 def response @response end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
9 10 11 |
# File 'lib/oauth2/access_token.rb', line 9 def token @token end |
Class Method Details
.from_hash(client, hash) ⇒ AccessToken
Initializes an AccessToken from a Hash
19 20 21 22 23 24 25 26 27 |
# File 'lib/oauth2/access_token.rb', line 19 def from_hash(client, hash) fresh = hash.dup supported_keys = TOKEN_KEY_LOOKUP & fresh.keys key = supported_keys[0] # Having too many is sus, and may lead to bugs. Having none is fine (e.g. refresh flow doesn't need a token). warn("OAuth2::AccessToken.from_hash: `hash` contained more than one 'token' key (#{supported_keys}); using #{key.inspect}.") if supported_keys.length > 1 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
34 35 36 |
# File 'lib/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
85 86 87 |
# File 'lib/oauth2/access_token.rb', line 85 def [](key) @params[key] end |
#delete(path, opts = {}, &block) ⇒ Object
Make a DELETE request with the Access Token
174 175 176 |
# File 'lib/oauth2/access_token.rb', line 174 def delete(path, opts = {}, &block) request(:delete, path, opts, &block) end |
#expired? ⇒ Boolean
Whether or not the token is expired
99 100 101 |
# File 'lib/oauth2/access_token.rb', line 99 def expired? expires? && (expires_at <= Time.now.to_i) end |
#expires? ⇒ Boolean
Whether or not the token expires
92 93 94 |
# File 'lib/oauth2/access_token.rb', line 92 def expires? !!@expires_at end |
#get(path, opts = {}, &block) ⇒ Object
Make a GET request with the Access Token
146 147 148 |
# File 'lib/oauth2/access_token.rb', line 146 def get(path, opts = {}, &block) request(:get, path, opts, &block) end |
#headers ⇒ Object
Get the headers hash (includes Authorization token)
179 180 181 |
# File 'lib/oauth2/access_token.rb', line 179 def headers {'Authorization' => [:header_format] % token} end |
#patch(path, opts = {}, &block) ⇒ Object
Make a PATCH request with the Access Token
167 168 169 |
# File 'lib/oauth2/access_token.rb', line 167 def patch(path, opts = {}, &block) request(:patch, path, opts, &block) end |
#post(path, opts = {}, &block) ⇒ Object
Make a POST request with the Access Token
153 154 155 |
# File 'lib/oauth2/access_token.rb', line 153 def post(path, opts = {}, &block) request(:post, path, opts, &block) end |
#put(path, opts = {}, &block) ⇒ Object
Make a PUT request with the Access Token
160 161 162 |
# File 'lib/oauth2/access_token.rb', line 160 def put(path, opts = {}, &block) request(:put, path, opts, &block) end |
#refresh(params = {}, access_token_opts = {}) ⇒ AccessToken Also known as: refresh!
options should be carried over to the new AccessToken
Refreshes the current Access Token
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/oauth2/access_token.rb', line 107 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. = 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
138 139 140 141 |
# File 'lib/oauth2/access_token.rb', line 138 def request(verb, path, opts = {}, &block) configure_authentication!(opts) @client.request(verb, path, opts, &block) end |
#to_hash ⇒ Hash
Convert AccessToken to a hash which can be used to rebuild itself with AccessToken.from_hash
128 129 130 |
# File 'lib/oauth2/access_token.rb', line 128 def to_hash params.merge(access_token: token, refresh_token: refresh_token, expires_at: expires_at) end |