Class: Revolut::Auth

Inherits:
Resource show all
Defined in:
lib/revolut/resources/auth.rb

Defined Under Namespace

Classes: NotAuthorizedError

Class Attribute Summary collapse

Class Method Summary collapse

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_tokenString

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.

Returns:

  • (String)

    The access token.

Raises:



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_atDateTime

Returns the expiration date and time of the authentication token.

Returns:

  • (DateTime)

    The expiration date and time.



86
87
88
# File 'lib/revolut/resources/auth.rb', line 86

def expires_at
  Time.at(@expires_at).utc.to_datetime
end

.refresh_tokenObject

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_typeObject

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

Returns:

  • (Boolean)


104
105
106
# File 'lib/revolut/resources/auth.rb', line 104

def authenticated?
  !@access_token.nil? && !expired?
end

.authorize_urlString

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.

Returns:

  • (String)

    The authorization URL.



25
26
27
28
29
30
31
# File 'lib/revolut/resources/auth.rb', line 25

def authorize_url
  URI::HTTPS.build(
    host: authorize_uri_host,
    path: "/app-confirm",
    query: "client_id=#{Revolut.config.client_id}&redirect_uri=#{Revolut.config.authorize_redirect_uri}&response_type=code" + (Revolut.config.scope ? "&scope=#{Revolut.config.scope}" : "")
  ).to_s + "#authorise"
end

.clearObject

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.

Parameters:

  • authorization_code (String)

    The authorization code to retrieve the access token.

Returns:

  • (Auth)

    The newly created Revolut::Auth object.



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

Returns:

  • (Boolean)


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.

Parameters:

  • auth_json (Hash)

    The JSON object containing authentication data.



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_envvoid

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.

Parameters:

  • force (Boolean) (defaults to: false)

    Whether to force refresh the access token.

Returns:

  • (Revolut::Resources::Auth)

    The refreshed authentication object.



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