Class: Aptible::Auth::Token

Inherits:
Resource
  • Object
show all
Defined in:
lib/aptible/auth/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#namespace, #root_url

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



6
7
8
# File 'lib/aptible/auth/token.rb', line 6

def access_token
  @access_token
end

#expires_atObject

Returns the value of attribute expires_at.



6
7
8
# File 'lib/aptible/auth/token.rb', line 6

def expires_at
  @expires_at
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



6
7
8
# File 'lib/aptible/auth/token.rb', line 6

def refresh_token
  @refresh_token
end

Class Method Details

.create(options) ⇒ Object



8
9
10
11
12
# File 'lib/aptible/auth/token.rb', line 8

def self.create(options)
  token = new
  token.process_options(options)
  token
end

Instance Method Details

#authenticate_client(id, secret, subject, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/aptible/auth/token.rb', line 20

def authenticate_client(id, secret, subject, options = {})
  options[:scope] ||= 'manage'
  response = oauth.assertion.get_token({
    iss: id,
    sub: subject
  }.merge(signing_params_from_secret(secret).merge(options)))
  parse_oauth_response(response)
end

#authenticate_user(email, password, options = {}) ⇒ Object



14
15
16
17
18
# File 'lib/aptible/auth/token.rb', line 14

def authenticate_user(email, password, options = {})
  options[:scope] ||= 'manage'
  response = oauth.password.get_token(email, password, options)
  parse_oauth_response(response)
end

#oauthObject



29
30
31
32
# File 'lib/aptible/auth/token.rb', line 29

def oauth
  options = { site: root_url, token_url: '/tokens' }
  @oauth ||= OAuth2::Client.new(nil, nil, options)
end

#process_options(options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/aptible/auth/token.rb', line 34

def process_options(options)
  if (email = options.delete(:email)) &&
     (password = options.delete(:password))
    authenticate_user(email, password, options)
  elsif (client_id = options.delete(:client_id)) &&
        (client_secret = options.delete(:client_secret)) &&
        (subject = options.delete(:subject))
    authenticate_client(client_id, client_secret, subject, options)
  end
end