Skip to content

Commit 0b7449b

Browse files
authored
Add Management API calls for session API endpoints #613 (#616)
2 parents 2d58da0 + b254e07 commit 0b7449b

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

lib/auth0/api/v2.rb

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
require 'auth0/api/v2/resource_servers'
2525
require 'auth0/api/v2/guardian'
2626
require 'auth0/api/v2/attack_protection'
27+
require 'auth0/api/v2/sessions'
2728

2829
module Auth0
2930
module Api
@@ -55,6 +56,7 @@ module V2
5556
include Auth0::Api::V2::Tenants
5657
include Auth0::Api::V2::Tickets
5758
include Auth0::Api::V2::AttackProtection
59+
include Auth0::Api::V2::Sessions
5860
end
5961
end
6062
end

lib/auth0/api/v2/sessions.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
module Auth0
4+
module Api
5+
module V2
6+
# Methods to use the Session endpoints
7+
module Sessions
8+
# Retrieve session information by id
9+
# @see https://auth0.com/docs/api/management/v2/sessions/get-session
10+
# @param id [string] The id of the session to retrieve.
11+
def session(session_id)
12+
raise Auth0::InvalidParameter, 'Must supply a valid session_id' if session_id.to_s.empty?
13+
14+
get "#{sessions_path}/#{session_id}"
15+
end
16+
17+
# Deletes a session by id
18+
# @see https://auth0.com/docs/api/management/v2/sessions/delete-session
19+
# @param id [string] The id of the session to delete.
20+
def delete_session(session_id)
21+
raise Auth0::InvalidParameter, 'Must supply a valid session_id' if session_id.to_s.empty?
22+
23+
delete "#{sessions_path}/#{session_id}"
24+
end
25+
26+
# Revokes a session by ID and all associated refresh tokens
27+
# @see https://auth0.com/docs/api/management/v2/sessions/revoke-session
28+
# @param id [string] The ID of the session to revoke
29+
def revoke_session(session_id)
30+
raise Auth0::InvalidParameter, 'Must supply a valid session_id' if session_id.to_s.empty?
31+
32+
post "#{sessions_path}/#{session_id}/revoke"
33+
end
34+
35+
private
36+
37+
def sessions_path
38+
@sessions_path ||= '/api/v2/sessions'
39+
end
40+
end
41+
end
42+
end
43+
end
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe Auth0::Api::V2::Sessions do
6+
before :all do
7+
dummy_instance = DummyClass.new
8+
dummy_instance.extend(Auth0::Api::V2::Sessions)
9+
@instance = dummy_instance
10+
end
11+
12+
context '.session' do
13+
it 'is expected to respond to a session method' do
14+
expect(@instance).to respond_to(:session)
15+
end
16+
17+
it 'is expected to GET a session' do
18+
expect(@instance).to receive(:get).with(
19+
'/api/v2/sessions/SESSION_ID'
20+
)
21+
22+
expect do
23+
@instance.session('SESSION_ID')
24+
end.not_to raise_error
25+
end
26+
27+
it 'is expected to raise an exception when the session ID is empty' do
28+
expect { @instance.session(nil) }.to raise_error('Must supply a valid session_id')
29+
end
30+
end
31+
32+
context '.delete_session' do
33+
it 'is expected to respond to a delete_session method' do
34+
expect(@instance).to respond_to(:delete_session)
35+
end
36+
37+
it 'is expected to DELETE a session' do
38+
expect(@instance).to receive(:delete).with(
39+
'/api/v2/sessions/SESSION_ID'
40+
)
41+
42+
expect do
43+
@instance.delete_session('SESSION_ID')
44+
end.not_to raise_error
45+
end
46+
47+
it 'is expected to raise an exception when the session ID is empty' do
48+
expect { @instance.delete_session(nil) }.to raise_error('Must supply a valid session_id')
49+
end
50+
end
51+
52+
context '.revoke_session' do
53+
it 'is expected to respond to a revoke_session method' do
54+
expect(@instance).to respond_to(:revoke_session)
55+
end
56+
57+
it 'is expected to POST to /api/v2/sessions/{id}/revoke' do
58+
expect(@instance).to receive(:post).with(
59+
'/api/v2/sessions/SESSION_ID/revoke'
60+
)
61+
62+
expect do
63+
@instance.revoke_session('SESSION_ID')
64+
end.not_to raise_error
65+
end
66+
67+
it 'is expected to raise an exception when the session ID is empty' do
68+
expect { @instance.revoke_session(nil) }.to raise_error('Must supply a valid session_id')
69+
end
70+
end
71+
end

0 commit comments

Comments
 (0)