Class: OAuth2::AccessToken

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

Direct Known Subclasses

MACToken

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initalize an AccessToken

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

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/oauth2/access_token.rb', line 41

def initialize(client, token, opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
  @client = client
  @token = token.to_s
  opts = opts.dup
  [:refresh_token, :expires_in, :expires_at, :expires_latency].each do |arg|
    instance_variable_set("@#{arg}", opts.delete(arg) || opts.delete(arg.to_s))
  end
  @expires_in ||= opts.delete('expires')
  @expires_in &&= @expires_in.to_i
  @expires_at &&= @expires_at.to_i
  @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



3
4
5
# File 'lib/oauth2/access_token.rb', line 3

def client
  @client
end

#expires_atObject (readonly)

Returns the value of attribute expires_at



3
4
5
# File 'lib/oauth2/access_token.rb', line 3

def expires_at
  @expires_at
end

#expires_inObject (readonly)

Returns the value of attribute expires_in



3
4
5
# File 'lib/oauth2/access_token.rb', line 3

def expires_in
  @expires_in
end

#expires_latencyObject (readonly)

Returns the value of attribute expires_latency



3
4
5
# File 'lib/oauth2/access_token.rb', line 3

def expires_latency
  @expires_latency
end

#optionsObject

Returns the value of attribute options



4
5
6
# File 'lib/oauth2/access_token.rb', line 4

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params



3
4
5
# File 'lib/oauth2/access_token.rb', line 3

def params
  @params
end

#refresh_tokenObject

Returns the value of attribute refresh_token



4
5
6
# File 'lib/oauth2/access_token.rb', line 4

def refresh_token
  @refresh_token
end

#responseObject

Returns the value of attribute response



4
5
6
# File 'lib/oauth2/access_token.rb', line 4

def response
  @response
end

#tokenObject (readonly)

Returns the value of attribute token



3
4
5
# File 'lib/oauth2/access_token.rb', line 3

def token
  @token
end

Class Method Details

.from_hash(client, hash) ⇒ AccessToken

Initializes an AccessToken from a Hash



12
13
14
15
# File 'lib/oauth2/access_token.rb', line 12

def from_hash(client, hash)
  hash = hash.dup
  new(client, hash.delete('access_token') || hash.delete(:access_token), hash)
end

.from_kvform(client, kvform) ⇒ AccessToken

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



22
23
24
# File 'lib/oauth2/access_token.rb', line 22

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



63
64
65
# File 'lib/oauth2/access_token.rb', line 63

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

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

Make a DELETE request with the Access Token

See Also:



147
148
149
# File 'lib/oauth2/access_token.rb', line 147

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

#expired?Boolean

Whether or not the token is expired



77
78
79
# File 'lib/oauth2/access_token.rb', line 77

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

#expires?Boolean

Whether or not the token expires



70
71
72
# File 'lib/oauth2/access_token.rb', line 70

def expires?
  !!@expires_at
end

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

Make a GET request with the Access Token

See Also:



119
120
121
# File 'lib/oauth2/access_token.rb', line 119

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

#headersObject

Get the headers hash (includes Authorization token)



152
153
154
# File 'lib/oauth2/access_token.rb', line 152

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

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

Make a PATCH request with the Access Token

See Also:



140
141
142
# File 'lib/oauth2/access_token.rb', line 140

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:



126
127
128
# File 'lib/oauth2/access_token.rb', line 126

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:



133
134
135
# File 'lib/oauth2/access_token.rb', line 133

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

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

Note:

options should be carried over to the new AccessToken

Refreshes the current Access Token



85
86
87
88
89
90
91
92
93
# File 'lib/oauth2/access_token.rb', line 85

def refresh(params = {}, access_token_opts = {}, access_token_class = self.class)
  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, access_token_class)
  new_token.options = options
  new_token.refresh_token = refresh_token unless new_token.refresh_token
  new_token
end

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

Make a request with the Access Token

See Also:



111
112
113
114
# File 'lib/oauth2/access_token.rb', line 111

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



101
102
103
# File 'lib/oauth2/access_token.rb', line 101

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