Module: Schwab::Authentication

Includes:
Util
Included in:
Client
Defined in:
lib/schwab/authentication.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

response_success?

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



8
9
10
# File 'lib/schwab/authentication.rb', line 8

def access_token
  @access_token
end

#access_token_expires_atObject (readonly)

Returns the value of attribute access_token_expires_at.



8
9
10
# File 'lib/schwab/authentication.rb', line 8

def access_token_expires_at
  @access_token_expires_at
end

#authorization_codeObject (readonly)

Returns the value of attribute authorization_code.



8
9
10
# File 'lib/schwab/authentication.rb', line 8

def authorization_code
  @authorization_code
end

#client_idObject (readonly)

Returns the value of attribute client_id.



8
9
10
# File 'lib/schwab/authentication.rb', line 8

def client_id
  @client_id
end

#redirect_uriObject (readonly)

Returns the value of attribute redirect_uri.



8
9
10
# File 'lib/schwab/authentication.rb', line 8

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



8
9
10
# File 'lib/schwab/authentication.rb', line 8

def refresh_token
  @refresh_token
end

#refresh_token_expires_atObject (readonly)

Returns the value of attribute refresh_token_expires_at.



8
9
10
# File 'lib/schwab/authentication.rb', line 8

def refresh_token_expires_at
  @refresh_token_expires_at
end

#secretObject (readonly)

Returns the value of attribute secret.



8
9
10
# File 'lib/schwab/authentication.rb', line 8

def secret
  @secret
end

Instance Method Details

#authorization_header_tokenObject



11
12
13
14
# File 'lib/schwab/authentication.rb', line 11

def authorization_header_token
  client_id_and_secret = "#{client_id}:#{secret}"
  Base64.strict_encode64(client_id_and_secret)
end

#default_headersObject



16
17
18
19
20
21
# File 'lib/schwab/authentication.rb', line 16

def default_headers
  {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': "Basic #{authorization_header_token}"
  }
end

#refresh_access_tokenObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/schwab/authentication.rb', line 56

def refresh_access_token
  options = {
    body: {
      grant_type: 'refresh_token',
      refresh_token: refresh_token,
    },
    headers: default_headers
  }

  response = HTTParty.post(
    'https://api.schwabapi.com/v1/oauth/token',
    options
  )

  update_tokens(response)
  response
end

#request_access_token(authorization_grant_code) ⇒ Object

Return value looks like:

{"expires_in"=>1800,
 "token_type"=>"Bearer",
 "scope"=>"api",
 "refresh_token"=>"LGGun...",
 "access_token"=>"I0.b2F...@",
 "id_token"=>"eyJ0eXA..."}


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/schwab/authentication.rb', line 30

def request_access_token(authorization_grant_code)
  # headers = { 'Content-Type': 'application/x-www-form-urlencoded' } # turns out didn't need this
  options = {
    body: {
      'grant_type': 'authorization_code',
      'code': authorization_grant_code,
      'redirect_uri': redirect_uri
    },
    headers: default_headers
  }

  response = HTTParty.post(
    'https://api.schwabapi.com/v1/oauth/token',
    options
  )

  unless response_success?(response)
    raise Schwab::APIError.new(
      "Unable to retrieve access tokens from API - #{response.code} - #{response.body}"
    )
  end

  update_tokens(response)
  response
end