Class: BungieSdk::TokenManager
- Inherits:
-
Object
- Object
- BungieSdk::TokenManager
- Extended by:
- T::Sig
- Includes:
- Singleton
- Defined in:
- lib/bungie_sdk/token_manager.rb
Overview
OAuth2 token manager singleton class for the BungieSdk
Constant Summary collapse
- AUTH_URL =
'/en/Oauth/Authorize'.freeze
- TOKEN_URL =
'/Platform/App/Oauth/Token'.freeze
- BASE_URI =
'https://www.bungie.net'.freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #initialized? ⇒ Boolean
- #load_token(filepath) ⇒ Object
- #load_token_data ⇒ Object
- #oauth_client ⇒ Object
- #refresh_token ⇒ Object
- #save_token_data(auth_token) ⇒ Object
- #setup_token(user_token, token_path, api_key, client_id, client_secret, redirect_uri) ⇒ Object
- #web_auth ⇒ Object
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/bungie_sdk/token_manager.rb', line 9 def api_key @api_key end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
9 10 11 |
# File 'lib/bungie_sdk/token_manager.rb', line 9 def token @token end |
Instance Method Details
#initialized? ⇒ Boolean
49 50 51 |
# File 'lib/bungie_sdk/token_manager.rb', line 49 def initialized? !@token.nil? end |
#load_token(filepath) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/bungie_sdk/token_manager.rb', line 57 def load_token(filepath) if @token.nil? @token = if File.exist?(filepath) load_token_data else web_auth end end if @token.expired? @token = @token.refresh! save_token_data(@token) end @token end |
#load_token_data ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/bungie_sdk/token_manager.rb', line 98 def load_token_data JSON.parse(File.read(@token_path)).yield_self do |token_data| OAuth2::AccessToken.new( oauth_client, token_data['token'], refresh_token: token_data['refresh_token'], expires_at: token_data['expires_at'] ) end end |
#oauth_client ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/bungie_sdk/token_manager.rb', line 111 def oauth_client OAuth2::Client.new( @client_id, @client_secret, site: BASE_URI, authorize_url: AUTH_URL, token_url: TOKEN_URL ) end |
#refresh_token ⇒ Object
92 93 94 |
# File 'lib/bungie_sdk/token_manager.rb', line 92 def refresh_token @token = @token.refresh! end |
#save_token_data(auth_token) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/bungie_sdk/token_manager.rb', line 124 def save_token_data(auth_token) if @token_path File.write( @token_path, JSON.generate({ 'token' => auth_token.token, 'refresh_token' => auth_token.refresh_token, 'expires_at' => auth_token.expires_at }) ) end end |
#setup_token(user_token, token_path, api_key, client_id, client_secret, redirect_uri) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bungie_sdk/token_manager.rb', line 26 def setup_token(user_token, token_path, api_key, client_id, client_secret, redirect_uri) @token_path = token_path @api_key = api_key @client_id = client_id @client_secret = client_secret @redirect_uri = redirect_uri if !@token_path.nil? load_token(@token_path) elsif !user_token.nil? @token = user_token else web_auth end end |
#web_auth ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bungie_sdk/token_manager.rb', line 76 def web_auth client = oauth_client auth_url = client.auth_code.(redirect_uri: @redirect_uri) puts auth_url Launchy.open(auth_url) rescue nil puts 'Please go to this url, accept the authorization request, '\ 'and copy the code parameter from the url into this program:' code = gets.chomp auth_token = client.auth_code.get_token(code) save_token_data(auth_token) @token = auth_token end |