Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ rc.authorize
account = rc.get('/restapi/v2/accounts/~').body
```

Call `authorize` again when the access token expires. Automatic refresh applies
only to tokens issued with a refresh token.
The SDK repeats the client credentials exchange when an API request needs a new
access token. Call `rc.refresh` to renew the token explicitly, or set
`rc.auto_refresh = true` before authorization to schedule renewal in the
background. If you save the token for reuse, keep its `grant_type` and `expires_at`
fields so the SDK can renew it after loading it again.

## Samples

Expand Down Expand Up @@ -128,9 +131,8 @@ Above will be translated to

### Token Refresh

Access token expires. You need to call `rc.refresh()` before it expires. If you
want the SDK to do auto refresh please `rc.auto_refresh = true` before
authorization.
Call `rc.refresh` to renew an access token explicitly. Set `rc.auto_refresh = true`
before authorization to schedule renewal in the background.

### Load pre-existing token

Expand Down Expand Up @@ -291,6 +293,13 @@ prints the account response as formatted JSON.
bundle exec ruby api_test/client_credentials_smoke.rb
```

Add `--test-renewal` to simulate expiry and verify that a second account lookup
automatically renews the token and returns the same account.

```sh
bundle exec ruby api_test/client_credentials_smoke.rb --test-renewal
```

**JWT**

Also set `RINGCENTRAL_JWT_TOKEN` to the JWT for the test extension.
Expand All @@ -299,6 +308,13 @@ Also set `RINGCENTRAL_JWT_TOKEN` to the JWT for the test extension.
bundle exec ruby api_test/jwt_smoke.rb
```

Add `--test-renewal` to exchange the refresh token and verify that the renewed
access token can retrieve the same account.

```sh
bundle exec ruby api_test/jwt_smoke.rb --test-renewal
```

**Authorization code**

Register `http://localhost:5000/oauth2callback` as an app redirect URI, or set
Expand All @@ -311,6 +327,12 @@ the code using PKCE.
bundle exec ruby api_test/auth_code_smoke.rb
```

Add `--test-renewal` to verify the refresh-token exchange after browser approval.

```sh
bundle exec ruby api_test/auth_code_smoke.rb --test-renewal
```

### Continuous integration

Normal push/PR CI and release-tag validation run the complete live suite on Ruby
Expand Down
2 changes: 1 addition & 1 deletion api_test/auth_code_smoke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def wait_for_authorization_code(client, callback, redirect_uri, verifier)
callback = loopback_callback(redirect_uri)
abort "Set RINGCENTRAL_REDIRECT_URI to a registered HTTP loopback URL with a port, such as #{DEFAULT_REDIRECT_URI}." unless callback

OAuthSmoke.run('Authorization code', server, client_id, client_secret) do |client|
OAuthSmoke.run('Authorization code', server, client_id, client_secret, test_renewal: ARGV.include?('--test-renewal')) do |client|
verifier = SecureRandom.urlsafe_base64(32)
code = wait_for_authorization_code(client, callback, redirect_uri, verifier)
client.authorize(auth_code: code, redirect_uri: redirect_uri, verifier: verifier)
Expand Down
2 changes: 1 addition & 1 deletion api_test/client_credentials_smoke.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require_relative 'oauth_smoke'

OAuthSmoke.run('Client credentials', *OAuthSmoke.load_config) do |client|
OAuthSmoke.run('Client credentials', *OAuthSmoke.load_config, test_renewal: ARGV.include?('--test-renewal')) do |client|
client.authorize
end
2 changes: 1 addition & 1 deletion api_test/jwt_smoke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

server, client_id, client_secret, jwt = OAuthSmoke.load_config('RINGCENTRAL_JWT_TOKEN')

OAuthSmoke.run('JWT', server, client_id, client_secret) do |client|
OAuthSmoke.run('JWT', server, client_id, client_secret, test_renewal: ARGV.include?('--test-renewal')) do |client|
client.authorize(jwt: jwt)
end
48 changes: 39 additions & 9 deletions api_test/oauth_smoke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,57 @@ def self.load_config(*extra_required)
ENV.values_at(*required)
end

def self.run(flow, server, client_id, client_secret)
def self.run(flow, server, client_id, client_secret, test_renewal: false)
operation = "#{flow} authorization"
client = RingCentral.new(client_id, client_secret, server)
yield client
verify_access_token(client, operation)

operation = 'Account lookup'
status, account = lookup_account(client)
puts "#{flow} authorization succeeded. Account lookup returned HTTP #{status} with an account ID."
puts JSON.pretty_generate(account)

if test_renewal
operation = "#{flow} token renewal"
previous_token = client.token
if previous_token['grant_type'] == 'client_credentials'
previous_token = previous_token.merge('expires_at' => Time.now.to_f - 1)
client.token = previous_token
else
abort "#{operation} requires a refresh token from authorization." if previous_token['refresh_token'].to_s.empty?

client.refresh
verify_access_token(client, operation)
end

status, renewed_account = lookup_account(client)
if client.token.equal?(previous_token) || renewed_account['id'] != account['id']
abort "#{operation} did not renew the token and return the same account."
end
puts "#{operation} succeeded. Account lookup returned HTTP #{status} with the same account ID."
puts JSON.pretty_generate(renewed_account)
end
rescue StandardError => error
status = error.message[/\AHTTP status (\d{3}):/, 1]
detail = status ? "HTTP #{status}" : error.class.name
abort "#{operation} failed (#{detail})."
end

def self.verify_access_token(client, operation)
access_token = client.token['access_token'] if client.token.is_a?(Hash)
unless access_token.is_a?(String) && !access_token.empty?
abort "#{operation} did not return an access token."
end
end

operation = 'Account lookup'
def self.lookup_account(client)
response = client.get('/restapi/v2/accounts/~')
account = response.body
unless (200..299).cover?(response.status) && account.is_a?(Hash) && !account['id'].to_s.strip.empty?
abort "Account lookup returned HTTP #{response.status} without a successful account response."
end

puts "#{flow} authorization succeeded. Account lookup returned HTTP #{response.status} with an account ID."
puts JSON.pretty_generate(account)
rescue StandardError => error
status = error.message[/\AHTTP status (\d{3}):/, 1]
detail = status ? "HTTP #{status}" : error.class.name
abort "#{operation} failed (#{detail})."
[response.status, account]
end
private_class_method :verify_access_token, :lookup_account
end
Loading