Skip to content

Commit 17b7fc4

Browse files
l15narpit-jn
authored andcommitted
Add Management API calls for session
1 parent a802592 commit 17b7fc4

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

lib/auth0/api/v2.rb

Lines changed: 2 additions & 0 deletions
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

Lines changed: 34 additions & 0 deletions
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 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+
private
27+
28+
def sessions_path
29+
@sessions_path ||= '/api/v2/sessions'
30+
end
31+
end
32+
end
33+
end
34+
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
describe Auth0::Api::V2::Sessions do
5+
before :all do
6+
dummy_instance = DummyClass.new
7+
dummy_instance.extend(Auth0::Api::V2::Sessions)
8+
@instance = dummy_instance
9+
end
10+
context '.session' do
11+
it 'is expected to respond to a session method' do
12+
expect(@instance).to respond_to(:session)
13+
end
14+
15+
it 'is expected to GET a session' do
16+
expect(@instance).to receive(:get).with(
17+
'/api/v2/sessions/SESSION_ID'
18+
)
19+
20+
expect do
21+
@instance.session('SESSION_ID')
22+
end.not_to raise_error
23+
end
24+
25+
it 'is expected to raise an exception when the session ID is empty' do
26+
expect { @instance.session(nil) }.to raise_error('Must supply a valid session_id')
27+
end
28+
end
29+
context '.delete_session' do
30+
it 'is expected to respond to a delete_session method' do
31+
expect(@instance).to respond_to(:delete_session)
32+
end
33+
34+
it 'is expected to DELETE a session' do
35+
expect(@instance).to receive(:delete).with(
36+
'/api/v2/sessions/SESSION_ID'
37+
)
38+
39+
expect do
40+
@instance.delete_session('SESSION_ID')
41+
end.not_to raise_error
42+
end
43+
44+
it 'is expected to raise an exception when the session ID is empty' do
45+
expect { @instance.delete_session(nil) }.to raise_error('Must supply a valid session_id')
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)