Skip to content

Commit fe37f4d

Browse files
authored
Add Refresh Token endpoints for the Auth0 Management API #614 (#623)
2 parents 0b7449b + 676caf7 commit fe37f4d

File tree

5 files changed

+175
-1
lines changed

5 files changed

+175
-1
lines changed

lib/auth0/api/v2.rb

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
require 'auth0/api/v2/jobs'
1212
require 'auth0/api/v2/prompts'
1313
require 'auth0/api/v2/organizations'
14+
require 'auth0/api/v2/refresh_tokens'
1415
require 'auth0/api/v2/rules'
1516
require 'auth0/api/v2/roles'
1617
require 'auth0/api/v2/stats'
@@ -46,6 +47,7 @@ module V2
4647
include Auth0::Api::V2::LogStreams
4748
include Auth0::Api::V2::Prompts
4849
include Auth0::Api::V2::Organizations
50+
include Auth0::Api::V2::RefreshTokens
4951
include Auth0::Api::V2::Rules
5052
include Auth0::Api::V2::Roles
5153
include Auth0::Api::V2::Stats

lib/auth0/api/v2/refresh_tokens.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
module Auth0
4+
module Api
5+
module V2
6+
# Methods to use the Refresh Token endpoints
7+
module RefreshTokens
8+
# Retrieve refresh token information.
9+
# @see https://auth0.com/docs/api/management/v2/refresh-tokens/get-refresh-token
10+
# @param id [string] The id of the refresh token to retrieve
11+
def refresh_token(id)
12+
raise Auth0::InvalidParameter, 'Must supply a valid id' if id.to_s.empty?
13+
14+
get "#{resource_path}/#{id}"
15+
end
16+
17+
# Delete a refresh token by its ID.
18+
# @see https://auth0.com/docs/api/management/v2/refresh-tokens/delete-refresh-token
19+
# @param id [string] The id of the refresh token to delete
20+
def delete_refresh_token(id)
21+
raise Auth0::InvalidParameter, 'Must supply a valid id' if id.to_s.empty?
22+
23+
delete "#{resource_path}/#{id}"
24+
end
25+
26+
private
27+
28+
def resource_path
29+
@resource_path ||= '/api/v2/refresh-tokens'
30+
end
31+
end
32+
end
33+
end
34+
end

lib/auth0/api/v2/users.rb

+33-1
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,45 @@ def user_sessions(user_id)
465465
get "#{users_path}/#{user_id}/sessions"
466466
end
467467

468+
# Retrieve details for a user's refresh tokens.
469+
# @see https://auth0.com/docs/api/management/v2/users/get-refresh-tokens-for-user
470+
#
471+
# @param use_id [String] The user ID
472+
# @param options [hash] A hash of options for getting permissions
473+
# * :take [Integer] Number of results per page. Defaults to 50.
474+
# * :from [String] Optional token ID from which to start selection (exclusive).
475+
# * :include_totals [boolean] Return results inside an object that contains the total result count (true)
476+
# or as a direct array of results (false, default)
477+
#
478+
# @return [json] Returns refresh tokens for the given user_id.
479+
def user_refresh_tokens(user_id, options = {})
480+
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
481+
482+
request_params = {
483+
take: options.fetch(:take, nil),
484+
from: options.fetch(:from, nil),
485+
include_totals: options.fetch(:include_totals, nil)
486+
}
487+
488+
get "#{users_path}/#{user_id}/refresh-tokens", request_params
489+
end
490+
491+
# Delete all refresh tokens for a user.
492+
#
493+
# @param user_id [String] ID of the user to get remove refresh tokens for
494+
# @see https://auth0.com/docs/api/management/v2/users/delete-refresh-tokens-for-user
495+
def delete_user_refresh_tokens(user_id)
496+
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
497+
498+
delete "#{users_path}/#{user_id}/refresh-tokens"
499+
end
500+
468501
private
469502

470503
# Users API path
471504
def users_path
472505
@users_path ||= '/api/v2/users'
473506
end
474-
475507
end
476508
end
477509
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe Auth0::Api::V2::RefreshTokens do
6+
before :all do
7+
dummy_instance = DummyClass.new
8+
dummy_instance.extend(Auth0::Api::V2::RefreshTokens)
9+
@instance = dummy_instance
10+
end
11+
12+
describe '.refresh_token' do
13+
it 'is expected to respond to a refresh_token method' do
14+
expect(@instance).to respond_to(:refresh_token)
15+
end
16+
17+
it 'is expected to GET a refresh_token' do
18+
expect(@instance).to receive(:get).with(
19+
'/api/v2/refresh-tokens/REFRESH_TOKEN_ID'
20+
)
21+
22+
expect do
23+
@instance.refresh_token('REFRESH_TOKEN_ID')
24+
end.not_to raise_error
25+
end
26+
27+
it 'is expected to raise an exception when the id is empty' do
28+
expect { @instance.refresh_token(nil) }.to raise_error('Must supply a valid id')
29+
end
30+
end
31+
32+
describe '.delete_refresh_token' do
33+
it 'is expected to respond to a delete_refresh_token method' do
34+
expect(@instance).to respond_to(:delete_refresh_token)
35+
end
36+
37+
it 'is expected to DELETE a refresh_token' do
38+
expect(@instance).to receive(:delete).with(
39+
'/api/v2/refresh-tokens/REFRESH_TOKEN_ID'
40+
)
41+
42+
expect do
43+
@instance.delete_refresh_token('REFRESH_TOKEN_ID')
44+
end.not_to raise_error
45+
end
46+
47+
it 'is expected to raise an exception when the id is empty' do
48+
expect { @instance.delete_refresh_token(nil) }.to raise_error('Must supply a valid id')
49+
end
50+
end
51+
end

spec/lib/auth0/api/v2/users_spec.rb

+55
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,62 @@
843843
expect do
844844
@instance.user_sessions('USER_ID')
845845
end.not_to raise_error
846+
end
847+
end
846848

849+
context '.user_refresh_tokens' do
850+
it 'is expected to respond to a user_refresh_tokens method' do
851+
expect(@instance).to respond_to(:user_refresh_tokens)
852+
end
853+
854+
it 'is expected to raise an exception when the user ID is empty' do
855+
expect { @instance.user_refresh_tokens(nil) }.to raise_exception(Auth0::MissingUserId)
856+
end
857+
858+
it 'is expected to get user refresh tokens' do
859+
expect(@instance).to receive(:get).with(
860+
'/api/v2/users/USER_ID/refresh-tokens', {
861+
from: nil,
862+
take: nil,
863+
include_totals: nil
864+
}
865+
)
866+
expect do
867+
@instance.user_refresh_tokens('USER_ID')
868+
end.not_to raise_error
869+
end
870+
871+
it 'is expected to get user refresh tokens with custom parameters' do
872+
expect(@instance).to receive(:get).with(
873+
'/api/v2/users/USER_ID/refresh-tokens', {
874+
from: 'TOKEN_ID',
875+
take: 10,
876+
include_totals: true
877+
}
878+
)
879+
expect do
880+
@instance.user_refresh_tokens('USER_ID', from: 'TOKEN_ID', take: 10, include_totals: true)
881+
end.not_to raise_error
882+
end
883+
end
884+
885+
context '.delete_user_refresh_tokens' do
886+
it 'is expected to respond to delete_user_refresh_tokens' do
887+
expect(@instance).to respond_to(:delete_user_refresh_tokens)
888+
end
889+
890+
it 'is expected to raise an exception for a missing user ID' do
891+
expect { @instance.delete_user_refresh_tokens(nil) }.to raise_exception(Auth0::MissingUserId)
892+
end
893+
894+
it 'is expected to call the endpoint' do
895+
expect(@instance).to receive(:delete).with(
896+
'/api/v2/users/USER_ID/refresh-tokens'
897+
)
898+
899+
expect do
900+
@instance.delete_user_refresh_tokens 'USER_ID'
901+
end.to_not raise_error
847902
end
848903
end
849904
end

0 commit comments

Comments
 (0)