Class: SchwabRb::Auth::TokenManager

Inherits:
Object
  • Object
show all
Defined in:
lib/schwab_rb/auth/token_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, timestamp, api_key: nil, database: nil) ⇒ TokenManager

Returns a new instance of TokenManager.



51
52
53
54
55
56
# File 'lib/schwab_rb/auth/token_manager.rb', line 51

def initialize(token, timestamp, api_key: nil, database: nil)
  @token = token
  @timestamp = timestamp
  @api_key = api_key
  @database = database
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



58
59
60
# File 'lib/schwab_rb/auth/token_manager.rb', line 58

def api_key
  @api_key
end

#databaseObject (readonly)

Returns the value of attribute database.



58
59
60
# File 'lib/schwab_rb/auth/token_manager.rb', line 58

def database
  @database
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



58
59
60
# File 'lib/schwab_rb/auth/token_manager.rb', line 58

def timestamp
  @timestamp
end

#tokenObject (readonly)

Returns the value of attribute token.



58
59
60
# File 'lib/schwab_rb/auth/token_manager.rb', line 58

def token
  @token
end

Class Method Details

.from_database(api_key, database: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/schwab_rb/auth/token_manager.rb', line 12

def from_database(api_key, database: nil)
  database ||= default_database
  token_data = database.load_token(api_key)
  return nil unless token_data

  token = SchwabRb::Auth::Token.new(
    token: token_data["token"]["access_token"],
    expires_in: token_data["token"]["expires_in"],
    token_type: token_data["token"]["token_type"],
    scope: token_data["token"]["scope"],
    refresh_token: token_data["token"]["refresh_token"],
    id_token: token_data["token"]["id_token"],
    expires_at: token_data["token"]["expires_at"]
  )

  TokenManager.new(token, token_data["timestamp"], api_key: api_key, database: database)
end

.from_oauth2_token(oauth2_token, timestamp, api_key:, database: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/schwab_rb/auth/token_manager.rb', line 30

def from_oauth2_token(oauth2_token, timestamp, api_key:, database: nil)
  token = SchwabRb::Auth::Token.new(
    token: oauth2_token.token,
    expires_in: oauth2_token.expires_in,
    token_type: oauth2_token.params["token_type"] || "Bearer",
    scope: oauth2_token.params["scope"],
    refresh_token: oauth2_token.refresh_token,
    id_token: oauth2_token.params["id_token"],
    expires_at: oauth2_token.expires_at
  )

  TokenManager.new(token, timestamp, api_key: api_key, database: database)
end

Instance Method Details

#refresh_token(client) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/schwab_rb/auth/token_manager.rb', line 60

def refresh_token(client)
  new_token = client.session.refresh!

  @token = SchwabRb::Auth::Token.new(
    token: new_token.token,
    expires_in: new_token.expires_in,
    token_type: new_token.params["token_type"] || "Bearer",
    scope: new_token.params["scope"],
    refresh_token: new_token.refresh_token,
    id_token: new_token.params["id_token"],
    expires_at: new_token.expires_at
  )
  @timestamp = Time.now.to_i

  save

  oauth = OAuth2::Client.new(
    client.api_key,
    client.app_secret,
    site: SchwabRb::Constants::SCHWAB_BASE_URL,
    token_url: "/v1/oauth/token"
  )

  OAuth2::AccessToken.new(
    oauth,
    token.token,
    refresh_token: token.refresh_token,
    expires_at: token.expires_at
  )
end

#saveObject



91
92
93
# File 'lib/schwab_rb/auth/token_manager.rb', line 91

def save
  effective_database.save_token(api_key, to_h)
end

#to_hObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/schwab_rb/auth/token_manager.rb', line 99

def to_h
  {
    timestamp: timestamp,
    token: {
      expires_in: token.expires_in,
      token_type: token.token_type,
      scope: token.scope,
      refresh_token: token.refresh_token,
      access_token: token.token,
      id_token: token.id_token,
      expires_at: token.expires_at
    }
  }
end

#token_ageObject



95
96
97
# File 'lib/schwab_rb/auth/token_manager.rb', line 95

def token_age
  Time.now.to_i - timestamp
end