Skip to content

Commit 8934e14

Browse files
authored
Add sessions and refresh tokens to Users Management API (#661)
2 parents 4bcff45 + 35dc307 commit 8934e14

26 files changed

Lines changed: 1706 additions & 0 deletions

src/main/java/com/auth0/client/mgmt/ManagementAPI.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,22 @@ public KeysEntity keys() {
364364
return new KeysEntity(client, baseUrl, tokenProvider);
365365
}
366366

367+
/**
368+
* Getter for the RefreshTokens Entity
369+
* @return the RefreshTokens Entity
370+
*/
371+
public RefreshTokensEntity refreshTokens() {
372+
return new RefreshTokensEntity(client, baseUrl, tokenProvider);
373+
}
374+
375+
/**
376+
* Getter for the Sessions Entity
377+
* @return the Sessions Entity
378+
*/
379+
public SessionsEntity sessions() {
380+
return new SessionsEntity(client, baseUrl, tokenProvider);
381+
}
382+
367383
/**
368384
* Builder for {@link ManagementAPI} API client instances.
369385
*/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.auth0.client.mgmt;
2+
3+
import com.auth0.json.mgmt.refreshtokens.RefreshToken;
4+
import com.auth0.net.BaseRequest;
5+
import com.auth0.net.Request;
6+
import com.auth0.net.VoidRequest;
7+
import com.auth0.net.client.Auth0HttpClient;
8+
import com.auth0.net.client.HttpMethod;
9+
import com.auth0.utils.Asserts;
10+
import com.fasterxml.jackson.core.type.TypeReference;
11+
import okhttp3.HttpUrl;
12+
13+
/**
14+
* Class that provides an implementation of the Refresh Tokens methods of the Management API as defined in <a href="https://auth0.com/docs/api/management/v2#!/Refresh_Tokens">https://auth0.com/docs/api/management/v2#!/Refresh_Tokens</a>
15+
* <p>
16+
* This class is not thread-safe.
17+
* @see ManagementAPI
18+
*/
19+
@SuppressWarnings("WeakerAccess")
20+
public class RefreshTokensEntity extends BaseManagementEntity{
21+
22+
RefreshTokensEntity(Auth0HttpClient client, HttpUrl baseUrl, TokenProvider tokenProvider) {
23+
super(client, baseUrl, tokenProvider);
24+
}
25+
26+
/**
27+
* Request the refresh token for a given refresh token ID.
28+
* A token with scope {@code read:refresh_tokens} is needed.
29+
* See <a href="https://auth0.com/docs/api/management/v2/refresh-tokens/get-refresh-token">https://auth0.com/docs/api/management/v2/refresh-tokens/get-refresh-token</a>
30+
* @param refreshTokenId the refresh token ID.
31+
* @return a Request to execute.
32+
*/
33+
public Request<RefreshToken> get(String refreshTokenId){
34+
Asserts.assertNotNull(refreshTokenId, "refresh token ID");
35+
36+
String url = baseUrl
37+
.newBuilder()
38+
.addPathSegments("api/v2/refresh-tokens")
39+
.addPathSegment(refreshTokenId)
40+
.build()
41+
.toString();
42+
43+
return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference<RefreshToken>() {
44+
});
45+
}
46+
47+
/**
48+
* Delete the refresh token for a given refresh token ID.
49+
* * A token with scope {@code delete:refresh_tokens} is needed.
50+
* See <a href="https://auth0.com/docs/api/management/v2/refresh-tokens/delete-refresh-token">https://auth0.com/docs/api/management/v2/refresh-tokens/delete-refresh-token</a>
51+
* @param refreshTokenId the refresh token ID.
52+
* @return a Request to execute.
53+
*/
54+
public Request<Void> delete(String refreshTokenId){
55+
Asserts.assertNotNull(refreshTokenId, "refresh token ID");
56+
57+
String url = baseUrl
58+
.newBuilder()
59+
.addPathSegments("api/v2/refresh-tokens")
60+
.addPathSegment(refreshTokenId)
61+
.build()
62+
.toString();
63+
64+
return new VoidRequest(client, tokenProvider, url, HttpMethod.DELETE);
65+
}
66+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.auth0.client.mgmt;
2+
3+
import com.auth0.json.mgmt.sessions.Session;
4+
import com.auth0.net.BaseRequest;
5+
import com.auth0.net.Request;
6+
import com.auth0.net.VoidRequest;
7+
import com.auth0.net.client.Auth0HttpClient;
8+
import com.auth0.net.client.HttpMethod;
9+
import com.auth0.utils.Asserts;
10+
import com.fasterxml.jackson.core.type.TypeReference;
11+
import okhttp3.HttpUrl;
12+
13+
14+
/**
15+
* Class that provides an implementation of the Sessions methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Sessions
16+
* <p>
17+
* This class is not thread-safe.
18+
* @see ManagementAPI
19+
*/
20+
@SuppressWarnings("WeakerAccess")
21+
public class SessionsEntity extends BaseManagementEntity{
22+
23+
SessionsEntity(Auth0HttpClient client, HttpUrl baseUrl, TokenProvider tokenProvider) {
24+
super(client, baseUrl, tokenProvider);
25+
}
26+
27+
/**
28+
* Request the session for a given session ID.
29+
* A token with scope {@code read:sessions} is needed.
30+
* See <a href="https://auth0.com/docs/api/management/v2/sessions/get-session">https://auth0.com/docs/api/management/v2/sessions/get-session</a>
31+
* @param sessionId the session ID.
32+
* @return a Request to execute.
33+
*/
34+
public Request<Session> get(String sessionId){
35+
Asserts.assertNotNull(sessionId, "session ID");
36+
37+
String url = baseUrl
38+
.newBuilder()
39+
.addPathSegments("api/v2/sessions")
40+
.addPathSegment(sessionId)
41+
.build()
42+
.toString();
43+
44+
return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference<Session>() {
45+
});
46+
}
47+
48+
/**
49+
* Delete the session for a given session ID.
50+
* A token with scope {@code delete:sessions} is needed.
51+
* See <a href="https://auth0.com/docs/api/management/v2/sessions/delete-session">https://auth0.com/docs/api/management/v2/sessions/delete-session</a>
52+
* @param sessionId the session ID.
53+
* @return a Request to execute.
54+
*/
55+
public Request<Void> delete(String sessionId){
56+
Asserts.assertNotNull(sessionId, "session ID");
57+
58+
String url = baseUrl
59+
.newBuilder()
60+
.addPathSegments("api/v2/sessions")
61+
.addPathSegment(sessionId)
62+
.build()
63+
.toString();
64+
65+
return new VoidRequest(client, tokenProvider, url, HttpMethod.DELETE);
66+
}
67+
}

src/main/java/com/auth0/client/mgmt/UsersEntity.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.auth0.json.mgmt.users.RecoveryCode;
1414
import com.auth0.json.mgmt.users.User;
1515
import com.auth0.json.mgmt.users.UsersPage;
16+
import com.auth0.json.mgmt.refreshtokens.RefreshTokensPage;
17+
import com.auth0.json.mgmt.sessions.SessionsPage;
1618
import com.auth0.net.EmptyBodyRequest;
1719
import com.auth0.net.BaseRequest;
1820
import com.auth0.net.Request;
@@ -787,6 +789,103 @@ public Request<AuthenticationMethod> updateAuthenticationMethodById(String userI
787789
return request;
788790
}
789791

792+
/**
793+
* Get refresh tokens for a user
794+
* A token with {@code read:refresh_tokens} is needed.
795+
* See <a href="https://auth0.com/docs/api/management/v2/users/get-refresh-tokens-for-user">https://auth0.com/docs/api/management/v2/users/get-refresh-tokens-for-user</a>
796+
*
797+
* @param userId the role id
798+
* @param filter an optional pagination filter
799+
* @return a Request to execute
800+
*/
801+
public Request<RefreshTokensPage> listRefreshTokens(String userId, PageFilter filter) {
802+
Asserts.assertNotNull(userId, "user ID");
803+
HttpUrl.Builder builder = baseUrl
804+
.newBuilder()
805+
.addPathSegments("api/v2/users")
806+
.addPathSegment(userId)
807+
.addPathSegment("refresh-tokens");
808+
if (filter != null) {
809+
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
810+
builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
811+
}
812+
}
813+
String url = builder.build().toString();
814+
return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference<RefreshTokensPage>() {
815+
});
816+
}
817+
818+
/**
819+
* Delete all refresh tokens for a user.
820+
* A token with scope {@code delete:refresh_tokens} is needed.
821+
* See <a href="https://auth0.com/docs/api/management/v2/users/delete-refresh-tokens-for-user">https://auth0.com/docs/api/management/v2/users/delete-refresh-tokens-for-user</a>
822+
*
823+
* @param userId the user to delete the refresh tokens for
824+
* @return a Request to execute.
825+
*/
826+
public Request<Void> deleteRefreshTokens(String userId) {
827+
Asserts.assertNotNull(userId, "user ID");
828+
829+
String url = baseUrl
830+
.newBuilder()
831+
.addPathSegments("api/v2/users")
832+
.addPathSegment(userId)
833+
.addPathSegment("refresh-tokens")
834+
.build()
835+
.toString();
836+
837+
return new VoidRequest(this.client, tokenProvider, url, HttpMethod.DELETE);
838+
}
839+
840+
841+
/**
842+
* Get sessions for user
843+
* A token with {@code read:sessions} is needed.
844+
* See <a href="https://auth0.com/docs/api/management/v2/users/get-sessions-for-user">https://auth0.com/docs/api/management/v2/users/get-sessions-for-user</a>
845+
*
846+
* @param userId the role id
847+
* @param filter an optional pagination filter
848+
* @return a Request to execute
849+
*/
850+
public Request<SessionsPage> listSessions(String userId, PageFilter filter) {
851+
Asserts.assertNotNull(userId, "user ID");
852+
HttpUrl.Builder builder = baseUrl
853+
.newBuilder()
854+
.addPathSegments("api/v2/users")
855+
.addPathSegment(userId)
856+
.addPathSegment("sessions");
857+
if (filter != null) {
858+
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
859+
builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
860+
}
861+
}
862+
String url = builder.build().toString();
863+
return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference<SessionsPage>() {
864+
});
865+
}
866+
867+
/**
868+
* Delete sessions for user
869+
* A token with scope {@code delete:sessions} is needed.
870+
* See <a href="https://auth0.com/docs/api/management/v2/users/delete-sessions-for-user">https://auth0.com/docs/api/management/v2/users/delete-sessions-for-user</a>
871+
*
872+
* @param userId the user to delete the sessions for
873+
* @return a Request to execute.
874+
*/
875+
public Request<Void> deleteSessions(String userId) {
876+
Asserts.assertNotNull(userId, "user ID");
877+
878+
String url = baseUrl
879+
.newBuilder()
880+
.addPathSegments("api/v2/users")
881+
.addPathSegment(userId)
882+
.addPathSegment("sessions")
883+
.build()
884+
.toString();
885+
886+
return new VoidRequest(this.client, tokenProvider, url, HttpMethod.DELETE);
887+
}
888+
790889
private static void encodeAndAddQueryParam(HttpUrl.Builder builder, BaseFilter filter) {
791890
if (filter != null) {
792891
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.auth0.json.mgmt.refreshtokens;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
@JsonIgnoreProperties(ignoreUnknown = true)
8+
@JsonInclude(JsonInclude.Include.NON_NULL)
9+
public class Device {
10+
@JsonProperty("initial_ip")
11+
private String initialIp;
12+
@JsonProperty("initial_asn")
13+
private String initialAsn;
14+
@JsonProperty("initial_user_agent")
15+
private String initialUserAgent;
16+
@JsonProperty("last_ip")
17+
private String lastIp;
18+
@JsonProperty("last_asn")
19+
private String lastAsn;
20+
@JsonProperty("last_user_agent")
21+
private String lastUserAgent;
22+
23+
/**
24+
* @return First IP address associated with this session
25+
*/
26+
public String getInitialIp() {
27+
return initialIp;
28+
}
29+
30+
/**
31+
* @return First autonomous system number associated with this session
32+
*/
33+
public String getInitialAsn() {
34+
return initialAsn;
35+
}
36+
37+
/**
38+
* @return First user agent associated with this session
39+
*/
40+
public String getInitialUserAgent() {
41+
return initialUserAgent;
42+
}
43+
44+
/**
45+
* @return Last IP address from which this user logged in
46+
*/
47+
public String getLastIp() {
48+
return lastIp;
49+
}
50+
51+
/**
52+
* @return Last autonomous system number from which this user logged in
53+
*/
54+
public String getLastAsn() {
55+
return lastAsn;
56+
}
57+
58+
/**
59+
* @return Last user agent of the device from which this user logged in
60+
*/
61+
public String getLastUserAgent() {
62+
return lastUserAgent;
63+
}
64+
}

0 commit comments

Comments
 (0)