Class: OAuth2::AccessToken

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initalize an AccessToken

Parameters:

  • client (Client)

    the OAuth2::Client instance

  • token (String)

    the Access Token value

  • 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 time (epoch time in seconds or iso8601-formatted date) when the token will expire

  • :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
59
60
61
62
63
# File 'lib/oauth2/access_token.rb', line 41

def initialize(client, token, opts = {})
  @client = client
  @token = token.to_s
  [:refresh_token, :expires_in, :expires_at].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 &&= begin
                    Time.iso8601(@expires_at)
                  rescue ArgumentError, TypeError
                    begin
                      Time.at(Integer(@expires_at))
                    rescue ArgumentError
                      nil
                    end
                  end unless @expires_at.is_a?(Time)
  @expires_at ||= Time.at(Time.now.to_i + @expires_in) if @expires_in
  @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.



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

def client
  @client
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



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

def expires_at
  @expires_at
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



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

def expires_in
  @expires_in
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/oauth2/access_token.rb', line 6

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



6
7
8
# File 'lib/oauth2/access_token.rb', line 6

def refresh_token
  @refresh_token
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

Class Method Details

.from_hash(client, hash) ⇒ AccessToken

Initializes an AccessToken from a Hash

Parameters:

  • the (Client)

    OAuth2::Client instance

  • a (Hash)

    hash of AccessToken property values

Returns:



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

def from_hash(client, hash)
  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

Parameters:

  • client (Client)

    the OAuth2::Client instance

  • kvform (String)

    the application/x-www-form-urlencoded string

Returns:



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

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



68
69
70
# File 'lib/oauth2/access_token.rb', line 68

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

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

Make a DELETE request with the Access Token

See Also:



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

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

#expired?Boolean

Whether or not the token is expired

Returns:

  • (Boolean)


82
83
84
# File 'lib/oauth2/access_token.rb', line 82

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

#expires?Boolean

Whether or not the token expires

Returns:

  • (Boolean)


75
76
77
# File 'lib/oauth2/access_token.rb', line 75

def expires?
  !!@expires_at
end

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

Make a GET request with the Access Token

See Also:



123
124
125
# File 'lib/oauth2/access_token.rb', line 123

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

#headersObject

Get the headers hash (includes Authorization token)



156
157
158
# File 'lib/oauth2/access_token.rb', line 156

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

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

Make a PATCH request with the Access Token

See Also:



144
145
146
# File 'lib/oauth2/access_token.rb', line 144

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:



130
131
132
# File 'lib/oauth2/access_token.rb', line 130

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:



137
138
139
# File 'lib/oauth2/access_token.rb', line 137

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

#refresh!(params = {}) ⇒ AccessToken

Note:

options should be carried over to the new AccessToken

Refreshes the current Access Token

Returns:



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/oauth2/access_token.rb', line 90

def refresh!(params = {})
  fail('A refresh_token is not available') unless refresh_token
  params.merge!(:client_id      => @client.id,
                :client_secret  => @client.secret,
                :grant_type     => 'refresh_token',
                :refresh_token  => refresh_token)
  new_token = @client.get_token(params)
  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

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



115
116
117
118
# File 'lib/oauth2/access_token.rb', line 115

def request(verb, path, opts = {}, &block)
  self.token = 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



105
106
107
# File 'lib/oauth2/access_token.rb', line 105

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