revolut-connect 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9267f44243fdcd4eb8506475f0766c8696e86c1ca1eff5d015b0023faed2ea10
4
- data.tar.gz: b310c700d99c101e6f923a2066264a15b0b143d769b963d92df5e626c6021188
3
+ metadata.gz: eaa3d08b4482201ea41200d2968900654790c24c29aaa7d2b9f2d55708711343
4
+ data.tar.gz: ac2db034b2f14493f620052056cb2691b57162289f242ed6fa16bf03d84a26a9
5
5
  SHA512:
6
- metadata.gz: 3d3bf7d3d056bcf68e3199fd0d9f6264ba81301ae5dc9c503aa83d46f1de2172d74cb58fbded6c3c116479c6438ebf9b14488f76a7a2ddf8ab6173c4e870ad01
7
- data.tar.gz: 6130b8b132106023f1f8aac3e924829e443119c4ec2b104403b9a2b38751142bd269997e103c3d635e72f32229f69b96c2490c0e03ca5197f2ac824bed003690
6
+ metadata.gz: 8014cbb4379beb6eccd20b0a8dc7c67e3746638eb1ad0435b522d998aa12f87ced1672e2b33627b266bff390c3ab3ebd240543179f29861c6b5e94e627cd1ba3
7
+ data.tar.gz: f1c2c09b9fc86fb9319befb7fe9e97bb55b823f1f8a4dca8731f0414365da4b9a0ee0a3abd6d9612a93924920c80e3c75e19caebdc4d30ebb1240a2f95a8f97a
data/Gemfile CHANGED
@@ -15,6 +15,7 @@ group :development, :test do
15
15
  gem "pry-stack_explorer", "~> 0.6.1"
16
16
  gem "dotenv"
17
17
  gem "standard", "~> 1.3"
18
+ gem "timecop"
18
19
  end
19
20
 
20
21
  group :test do
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- revolut-connect (0.1.0)
4
+ revolut-connect (0.1.1)
5
5
  faraday (>= 1)
6
6
  faraday-retry (>= 1)
7
7
  jwt (>= 1)
@@ -114,6 +114,7 @@ GEM
114
114
  standard-performance (1.3.1)
115
115
  lint_roller (~> 1.1)
116
116
  rubocop-performance (~> 1.20.2)
117
+ timecop (0.9.8)
117
118
  unicode-display_width (2.5.0)
118
119
  uri (0.13.0)
119
120
  webmock (3.23.0)
@@ -137,6 +138,7 @@ DEPENDENCIES
137
138
  simplecov
138
139
  simplecov-cobertura
139
140
  standard (~> 1.3)
141
+ timecop
140
142
  webmock (~> 3.23.0)
141
143
 
142
144
  BUNDLED WITH
data/README.md CHANGED
@@ -1,12 +1,13 @@
1
1
  # Revolut Connect
2
2
 
3
- <a href="https://codecov.io/github/moraki-finance/revolut-connect" >
4
- <img src="https://codecov.io/github/moraki-finance/revolut-connect/graph/badge.svg?token=SKTT14JJGV"/>
5
- </a>
6
3
 
7
- [![Tests](https://github.com/moraki-finance/revolut-connect/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/moraki-finance/revolut-connect/actions/workflows/main.yml)
4
+ | Tests | Coverage |
5
+ |:-:|:-:|
6
+ | [![Tests](https://github.com/moraki-finance/revolut-connect/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/moraki-finance/revolut-connect/actions/workflows/main.yml) | [![Codecov Coverage](https://codecov.io/github/moraki-finance/revolut-connect/graph/badge.svg?token=SKTT14JJGV)](https://codecov.io/github/moraki-finance/revolut-connect) |
8
7
 
9
- A lightweight API connector for Revolut. Revolut docs: <https://developer.revolut.com/>
8
+ A lightweight API client for Revolut featuring authentication, permission scopes, token expiration and automatic renewal.
9
+
10
+ Revolut docs: <https://developer.revolut.com/>
10
11
 
11
12
  _:warning: The extracted API objects don't do input parameters validations. It's a simple faraday wrapper that allows you to send as many inputs as you want. The Revolut API might fail when passing a wrong set of parameters._
12
13
 
@@ -131,6 +132,15 @@ Revolut.configure do |config|
131
132
  # Default: example.com
132
133
  config.iss = ENV["REVOLUT_ISS"]
133
134
 
135
+ # Optional: JWT token duration. After this duration, the token will be renewed (for security reasons, in case of token leakage)
136
+ # Default: 120 seconds (2 minutes).
137
+ config.token_duration = ENV["REVOLUT_TOKEN_DURATION"]
138
+
139
+ # Optional: Revolut authorization scope. You can restrict which features the app will have access to by passing different scopes.
140
+ # More info in https://developer.revolut.com/docs/business/business-api
141
+ # Default: nil
142
+ config.scope = ENV["REVOLUT_SCOPE"]
143
+
134
144
  # Optional: Timeout of the underlying faraday requests.
135
145
  # Default: 120
136
146
  config.request_timeout = 120
@@ -6,6 +6,8 @@ module Revolut
6
6
  client_id
7
7
  signing_key
8
8
  iss
9
+ token_duration
10
+ scope
9
11
  authorize_redirect_uri
10
12
  base_uri
11
13
  environment
data/lib/revolut/http.rb CHANGED
@@ -103,7 +103,7 @@ module Revolut
103
103
  iss:,
104
104
  sub: client_id,
105
105
  aud: "https://revolut.com",
106
- exp: Time.now.to_i + 120 # Expires in 2 minutes
106
+ exp: Time.now.to_i + token_duration
107
107
  }
108
108
 
109
109
  header = {
@@ -1,17 +1,22 @@
1
1
  # Helper middleware only intended to be used in the console.
2
2
  # The idea is to have a fast extraction of the API error message from the response body.
3
- class CatchError < Faraday::Middleware
4
- def on_complete(env)
5
- raise_error_middleware.on_complete(env)
6
- rescue Faraday::Error => e
7
- raise e, JSON.parse(e.response[:body])["message"]
8
- end
9
3
 
10
- private
4
+ module Revolut
5
+ module Middleware
6
+ class CatchError < Faraday::Middleware
7
+ def on_complete(env)
8
+ raise_error_middleware.on_complete(env)
9
+ rescue Faraday::Error => e
10
+ raise e, JSON.parse(e.response[:body])["message"]
11
+ end
12
+
13
+ private
11
14
 
12
- def raise_error_middleware
13
- @raise_error_middleware ||= Faraday::Response::RaiseError.new
15
+ def raise_error_middleware
16
+ @raise_error_middleware ||= Faraday::Response::RaiseError.new
17
+ end
18
+ end
14
19
  end
15
20
  end
16
21
 
17
- Faraday::Response.register_middleware catch_error: CatchError
22
+ Faraday::Response.register_middleware catch_error: Revolut::Middleware::CatchError
@@ -2,6 +2,8 @@ require "jwt"
2
2
 
3
3
  module Revolut
4
4
  class Auth < Resource
5
+ shallow
6
+
5
7
  class NotAuthorizedError < StandardError
6
8
  def initialize
7
9
  super(
@@ -21,7 +23,11 @@ module Revolut
21
23
  #
22
24
  # @return [String] The authorization URL.
23
25
  def authorize_url
24
- "#{authorize_base_uri}?client_id=#{Revolut.config.client_id}&redirect_uri=#{Revolut.config.authorize_redirect_uri}&response_type=code#authorise"
26
+ URI::HTTPS.build(
27
+ host: authorize_uri_host,
28
+ path: "/app-confirm",
29
+ query: "client_id=#{Revolut.config.client_id}&redirect_uri=#{Revolut.config.authorize_redirect_uri}&response_type=code" + (Revolut.config.scope ? "&scope=#{Revolut.config.scope}" : "")
30
+ ).to_s + "#authorise"
25
31
  end
26
32
 
27
33
  # Exchanges the authorization code for an access token.
@@ -90,6 +96,15 @@ module Revolut
90
96
  expires_at && Time.now.to_i >= @expires_at
91
97
  end
92
98
 
99
+ # Checks if the user is authenticated.
100
+ #
101
+ # Returns:
102
+ # - true if the access token is present and not expired
103
+ # - false otherwise
104
+ def authenticated?
105
+ !@access_token.nil? && !expired?
106
+ end
107
+
93
108
  # Loads authentication information from environment variable REVOLUT_AUTH_JSON.
94
109
  #
95
110
  # If the access token is not already set and the environment variable REVOLUT_AUTH_JSON is present,
@@ -107,12 +122,18 @@ module Revolut
107
122
  load(JSON.parse(env_json))
108
123
  end
109
124
 
125
+ # Clears the authentication information.
126
+ def clear
127
+ @access_token = nil
128
+ @token_type = nil
129
+ @expires_at = nil
130
+ @refresh_token = nil
131
+ end
132
+
110
133
  private
111
134
 
112
- def authorize_base_uri
113
- Revolut.sandbox? ?
114
- "https://sandbox-business.revolut.com/app-confirm" :
115
- "https://business.revolut.com/app-confirm"
135
+ def authorize_uri_host
136
+ Revolut.sandbox? ? "sandbox-business.revolut.com" : "business.revolut.com"
116
137
  end
117
138
  end
118
139
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Revolut
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/revolut.rb CHANGED
@@ -21,13 +21,14 @@ module Revolut
21
21
  class NotImplementedError < Error; end
22
22
 
23
23
  class Configuration
24
- attr_accessor :request_timeout, :global_headers, :environment
24
+ attr_accessor :request_timeout, :global_headers, :environment, :token_duration, :scope
25
25
  attr_writer :client_id, :signing_key, :iss, :authorize_redirect_uri
26
26
  attr_reader :base_uri
27
27
 
28
28
  DEFAULT_BASE_URI = "https://sandbox-b2b.revolut.com/api/1.0/"
29
29
  DEFAULT_ENVIRONMENT = "sandbox"
30
30
  DEFAULT_REQUEST_TIMEOUT = 120
31
+ DEFAULT_TOKEN_DURATION = 120 # 2 minutes
31
32
 
32
33
  def initialize
33
34
  @request_timeout = DEFAULT_REQUEST_TIMEOUT
@@ -36,6 +37,8 @@ module Revolut
36
37
  @signing_key = ENV["REVOLUT_SIGNING_KEY"]&.gsub("\\n", "\n")
37
38
  @iss = ENV.fetch("REVOLUT_ISS", "example.com")
38
39
  @authorize_redirect_uri = ENV["REVOLUT_AUTHORIZE_REDIRECT_URI"]
40
+ @token_duration = ENV.fetch("REVOLUT_TOKEN_DURATION", DEFAULT_TOKEN_DURATION)
41
+ @scope = ENV["REVOLUT_SCOPE"]
39
42
  @environment = ENV.fetch("REVOLUT_ENVIRONMENT", DEFAULT_ENVIRONMENT).to_sym
40
43
  @base_uri = (environment == :sandbox) ? "https://sandbox-b2b.revolut.com/api/1.0/" : "https://b2b.revolut.com/api/1.0/"
41
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: revolut-connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Mochetti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-01 00:00:00.000000000 Z
11
+ date: 2024-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt