Class: Revolut::Auth
Defined Under Namespace
Classes: NotAuthorizedError
Class Attribute Summary collapse
-
.access_token ⇒ String
Returns the access token too access the Revolut API.
-
.expires_at ⇒ DateTime
Returns the expiration date and time of the authentication token.
-
.refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
.token_type ⇒ Object
Returns the value of attribute token_type.
Class Method Summary collapse
-
.authenticated? ⇒ Boolean
Checks if the user is authenticated.
-
.authorize_url ⇒ String
Generates the authorization URL for the Revolut API.
-
.clear ⇒ Object
Clears the authentication information.
-
.exchange(authorization_code:) ⇒ Auth
Exchanges the authorization code for an access token.
-
.expired? ⇒ Boolean
Checks if the access_token has expired.
-
.load(auth_json) ⇒ void
Loads authentication data from a JSON object.
-
.load_from_env ⇒ void
Loads authentication information from the configuration auth_json.
-
.refresh(force: false) ⇒ Revolut::Resources::Auth
Refreshes the access token if it has expired or if force is set to true.
Methods inherited from Resource
coerce_with, create, delete, http_client, #initialize, list, retrieve, skip_coertion_for, #to_json, to_proc, update
Constructor Details
This class inherits a constructor from Revolut::Resource
Class Attribute Details
.access_token ⇒ String
Returns the access token too access the Revolut API. Raises Revolut::Auth::NotAuthorizedError if the access token is not set. Refreshes the access token if it has expired.
59 60 61 62 63 64 65 66 67 |
# File 'lib/revolut/resources/auth.rb', line 59 def access_token # If there's no token set in the authorization class, it means that we're trying to # access the API without having gone through the authorization process. raise Revolut::Auth::NotAuthorizedError if @access_token.nil? refresh if expired? @access_token end |
.expires_at ⇒ DateTime
Returns the expiration date and time of the authentication token.
86 87 88 |
# File 'lib/revolut/resources/auth.rb', line 86 def expires_at Time.at(@expires_at).utc.to_datetime end |
.refresh_token ⇒ Object
Returns the value of attribute refresh_token.
17 18 19 |
# File 'lib/revolut/resources/auth.rb', line 17 def refresh_token @refresh_token end |
.token_type ⇒ Object
Returns the value of attribute token_type.
17 18 19 |
# File 'lib/revolut/resources/auth.rb', line 17 def token_type @token_type end |
Class Method Details
.authenticated? ⇒ Boolean
Checks if the user is authenticated.
Returns:
-
true if the access token is present and not expired
-
false otherwise
104 105 106 |
# File 'lib/revolut/resources/auth.rb', line 104 def authenticated? !@access_token.nil? && !expired? end |
.authorize_url ⇒ String
Generates the authorization URL for the Revolut API. Use this URI to redirect the user to Revolut’s authorization page to authorize your app to access her business account.
25 26 27 28 29 30 31 |
# File 'lib/revolut/resources/auth.rb', line 25 def URI::HTTPS.build( host: , path: "/app-confirm", query: "client_id=#{Revolut.config.client_id}&redirect_uri=#{Revolut.config.}&response_type=code" + (Revolut.config.scope ? "&scope=#{Revolut.config.scope}" : "") ).to_s + "#authorise" end |
.clear ⇒ Object
Clears the authentication information.
126 127 128 129 130 131 |
# File 'lib/revolut/resources/auth.rb', line 126 def clear @access_token = nil @token_type = nil @expires_at = nil @refresh_token = nil end |
.exchange(authorization_code:) ⇒ Auth
Exchanges the authorization code for an access token.
37 38 39 40 41 |
# File 'lib/revolut/resources/auth.rb', line 37 def exchange(authorization_code:) auth_json = http_client.get_access_token(authorization_code:).body load(auth_json) new(auth_json) end |
.expired? ⇒ Boolean
Checks if the access_token has expired.
Returns:
-
true if the access_token has expired
-
false otherwise
95 96 97 |
# File 'lib/revolut/resources/auth.rb', line 95 def expired? expires_at && Time.now.to_i >= @expires_at end |
.load(auth_json) ⇒ void
This method returns an undefined value.
Loads authentication data from a JSON object.
47 48 49 50 51 52 |
# File 'lib/revolut/resources/auth.rb', line 47 def load(auth_json) @access_token = auth_json["access_token"] @token_type = auth_json["token_type"] @expires_at = Time.now.to_i + auth_json["expires_in"] @refresh_token = auth_json["refresh_token"] end |
.load_from_env ⇒ void
This method returns an undefined value.
Loads authentication information from the configuration auth_json.
If the access token is not already set and auth_json config is present, this method loads the JSON data from the config variable and calls the load method to set the authentication information.
Example:
auth.load_from_env
117 118 119 120 121 122 123 |
# File 'lib/revolut/resources/auth.rb', line 117 def load_from_env env_json = Revolut.config.auth_json return unless @access_token.nil? && env_json load(JSON.parse(env_json)) end |
.refresh(force: false) ⇒ Revolut::Resources::Auth
Refreshes the access token if it has expired or if force is set to true.
73 74 75 76 77 78 79 80 81 |
# File 'lib/revolut/resources/auth.rb', line 73 def refresh(force: false) return unless expired? || force new(http_client.refresh_access_token(refresh_token:).body).tap do |refreshed_auth| @access_token = refreshed_auth.access_token @token_type = refreshed_auth.token_type @expires_at = Time.now.to_i + refreshed_auth.expires_in end end |