Module: Brightbox::Config::AuthenticationTokens
- Included in:
- BBConfig
- Defined in:
- lib/brightbox-cli/config/authentication_tokens.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
- #access_token_filename ⇒ Object
- #oauth_token ⇒ Object
-
#reauthenticate ⇒ Object
We have been told our tokens are bad so we need to correct that.
- #refresh_token_filename ⇒ Object
-
#renew_tokens(options = {}) ⇒ Object
This attempts to renew access (and refresh) tokens for the current configuration based on the current connection.
- #update_stored_tokens(new_access_token, new_refresh_token = nil) ⇒ Object
Instance Attribute Details
#access_token ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/brightbox-cli/config/authentication_tokens.rb', line 11 def access_token if defined?(@access_token) && !@access_token.nil? return @access_token end @access_token = if File.exist?(access_token_filename) cached_access_token end end |
#refresh_token ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/brightbox-cli/config/authentication_tokens.rb', line 26 def refresh_token if defined?(@refresh_token) && !@refresh_token.nil? return @refresh_token end @refresh_token = if File.exist?(refresh_token_filename) cached_refresh_token end end |
Instance Method Details
#access_token_filename ⇒ Object
6 7 8 9 |
# File 'lib/brightbox-cli/config/authentication_tokens.rb', line 6 def access_token_filename file_name = "#{base_token_name}.oauth_token" @access_token_filename ||= File.join(config_directory, file_name) end |
#oauth_token ⇒ Object
37 38 39 40 |
# File 'lib/brightbox-cli/config/authentication_tokens.rb', line 37 def oauth_token debug "WARN: oauth_token is deprecated, use access_token instead" access_token end |
#reauthenticate ⇒ Object
We have been told our tokens are bad so we need to correct that
44 45 46 47 48 49 50 51 |
# File 'lib/brightbox-cli/config/authentication_tokens.rb', line 44 def reauthenticate # Don't hold on to the current access token it's worthless flush_access_token! renew_tokens false # Skip GLI error handling end |
#refresh_token_filename ⇒ Object
21 22 23 24 |
# File 'lib/brightbox-cli/config/authentication_tokens.rb', line 21 def refresh_token_filename file_name = "#{base_token_name}.refresh_token" @refresh_token_filename ||= File.join(config_directory, file_name) end |
#renew_tokens(options = {}) ⇒ Object
Note:
Brightbox.config and Api.conn are actually two different worlds and should be merged (so a configuration holds the current connection)
This attempts to renew access (and refresh) tokens for the current configuration based on the current connection.
59 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/brightbox-cli/config/authentication_tokens.rb', line 59 def renew_tokens( = {}) # This monster is basically a worse version of automatically getting the # best grant strategy from our fog service itself. # # If/when the fog connection is correctly initialised all of this can go # because passing all the config and all the cached tokens lets fog do # it's thing rather than us trying to micromanage it. # # The only problem is wanting to send a password but prompting halfway # through the process. # password = [:password] if [:password] one_time_password = [:one_time_password] if [:one_time_password] # To prevent refreshing tokens for the wrong client (using client_name # is pretty random) we set it specially if [:client_name] self.client_name = [:client_name] end begin if using_application? if refresh_token begin service = update_tokens_with_refresh_token rescue Fog::Brightbox::OAuth2::TwoFactorMissingError, Excon::Errors::BadRequest, Excon::Errors::Unauthorized service = update_tokens_with_user_credentials end else begin service = update_tokens_with_user_credentials(password: password, one_time_password: one_time_password) rescue Fog::Brightbox::OAuth2::TwoFactorMissingError, Excon::Errors::BadRequest, Excon::Errors::Unauthorized service = update_tokens_with_user_credentials(password: password) end end else service = update_tokens_with_client_credentials end new_access_token = service.access_token new_refresh_token = service.refresh_token update_stored_tokens(new_access_token, new_refresh_token) rescue Excon::Errors::BadRequest, Excon::Errors::Unauthorized error "ERROR: Unable to reauthenticate!" ensure debug_tokens end end |
#update_stored_tokens(new_access_token, new_refresh_token = nil) ⇒ Object
108 109 110 111 112 |
# File 'lib/brightbox-cli/config/authentication_tokens.rb', line 108 def update_stored_tokens(new_access_token, new_refresh_token = nil) save_access_token(new_access_token) save_refresh_token(new_refresh_token) unless new_refresh_token.nil? debug_tokens end |