Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sessions and refresh tokens to Users Management API #661

Merged
merged 7 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 16 additions & 0 deletions src/main/java/com/auth0/client/mgmt/ManagementAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,22 @@ public KeysEntity keys() {
return new KeysEntity(client, baseUrl, tokenProvider);
}

/**
* Getter for the Prompts Entity
* @return the Prompts Entity
*/
jimmyjames marked this conversation as resolved.
Show resolved Hide resolved
public RefreshTokensEntity refreshTokens() {
return new RefreshTokensEntity(client, baseUrl, tokenProvider);
}

/**
* Getter for the Prompts Entity
* @return the Prompts Entity
*/
public SessionsEntity sessions() {
return new SessionsEntity(client, baseUrl, tokenProvider);
}

/**
* Builder for {@link ManagementAPI} API client instances.
*/
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/com/auth0/client/mgmt/RefreshTokensEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.auth0.client.mgmt;

import com.auth0.json.mgmt.refreshtokens.RefreshToken;
import com.auth0.net.BaseRequest;
import com.auth0.net.Request;
import com.auth0.net.VoidRequest;
import com.auth0.net.client.Auth0HttpClient;
import com.auth0.net.client.HttpMethod;
import com.auth0.utils.Asserts;
import com.fasterxml.jackson.core.type.TypeReference;
import okhttp3.HttpUrl;

/**
* 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>
* <p>
* This class is not thread-safe.
* @see ManagementAPI
*/
@SuppressWarnings("WeakerAccess")
public class RefreshTokensEntity extends BaseManagementEntity{

RefreshTokensEntity(Auth0HttpClient client, HttpUrl baseUrl, TokenProvider tokenProvider) {
super(client, baseUrl, tokenProvider);
}

/**
* Request the refresh token for a given refresh token ID.
* A token with scope {@code read:refresh_tokens} is needed.
* 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>
* @param refreshTokenId the refresh token ID.
* @return a Request to execute.
*/
public Request<RefreshToken> get(String refreshTokenId){
Asserts.assertNotNull(refreshTokenId, "refresh token ID");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/refresh-tokens")
.addPathSegment(refreshTokenId)
.build()
.toString();

return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference<RefreshToken>() {
});
}

/**
* Delete the refresh token for a given refresh token ID.
* * A token with scope {@code delete:refresh_tokens} is needed.
* 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>
* @param refreshTokenId the refresh token ID.
* @return a Request to execute.
*/
public Request<Void> delete(String refreshTokenId){
Asserts.assertNotNull(refreshTokenId, "refresh token ID");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/refresh-tokens")
.addPathSegment(refreshTokenId)
.build()
.toString();

return new VoidRequest(client, tokenProvider, url, HttpMethod.DELETE);
}
}
67 changes: 67 additions & 0 deletions src/main/java/com/auth0/client/mgmt/SessionsEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.auth0.client.mgmt;

import com.auth0.json.mgmt.sessions.Session;
import com.auth0.net.BaseRequest;
import com.auth0.net.Request;
import com.auth0.net.VoidRequest;
import com.auth0.net.client.Auth0HttpClient;
import com.auth0.net.client.HttpMethod;
import com.auth0.utils.Asserts;
import com.fasterxml.jackson.core.type.TypeReference;
import okhttp3.HttpUrl;


/**
* Class that provides an implementation of the Sessions methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Sessions
* <p>
* This class is not thread-safe.
* @see ManagementAPI
*/
@SuppressWarnings("WeakerAccess")
public class SessionsEntity extends BaseManagementEntity{

SessionsEntity(Auth0HttpClient client, HttpUrl baseUrl, TokenProvider tokenProvider) {
super(client, baseUrl, tokenProvider);
}

/**
* Request the session for a given session ID.
* A token with scope {@code read:sessions} is needed.
* See <a href="https://auth0.com/docs/api/management/v2/sessions/get-session">https://auth0.com/docs/api/management/v2/sessions/get-session</a>
* @param sessionId the session ID.
* @return a Request to execute.
*/
public Request<Session> get(String sessionId){
Asserts.assertNotNull(sessionId, "session ID");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/sessions")
.addPathSegment(sessionId)
.build()
.toString();

return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference<Session>() {
});
}

/**
* Delete the session for a given session ID.
* A token with scope {@code delete:sessions} is needed.
* See <a href="https://auth0.com/docs/api/management/v2/sessions/delete-session">https://auth0.com/docs/api/management/v2/sessions/delete-session</a>
* @param sessionId the session ID.
* @return a Request to execute.
*/
public Request<Void> delete(String sessionId){
Asserts.assertNotNull(sessionId, "session ID");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/sessions")
.addPathSegment(sessionId)
.build()
.toString();

return new VoidRequest(client, tokenProvider, url, HttpMethod.DELETE);
}
}
99 changes: 99 additions & 0 deletions src/main/java/com/auth0/client/mgmt/UsersEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.auth0.json.mgmt.users.RecoveryCode;
import com.auth0.json.mgmt.users.User;
import com.auth0.json.mgmt.users.UsersPage;
import com.auth0.json.mgmt.refreshtokens.RefreshTokensPage;
import com.auth0.json.mgmt.sessions.SessionsPage;
import com.auth0.net.EmptyBodyRequest;
import com.auth0.net.BaseRequest;
import com.auth0.net.Request;
Expand Down Expand Up @@ -787,6 +789,103 @@ public Request<AuthenticationMethod> updateAuthenticationMethodById(String userI
return request;
}

/**
* Get refresh tokens for a user
* A token with {@code read:refresh_tokens} is needed.
* 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>
*
* @param userId the role id
* @param filter an optional pagination filter
* @return a Request to execute
*/
public Request<RefreshTokensPage> listRefreshTokens(String userId, PageFilter filter) {
Asserts.assertNotNull(userId, "user ID");
HttpUrl.Builder builder = baseUrl
.newBuilder()
.addPathSegments("api/v2/users")
.addPathSegment(userId)
.addPathSegment("refresh-tokens");
if (filter != null) {
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
}
}
String url = builder.build().toString();
return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference<RefreshTokensPage>() {
});
}

/**
* Delete all refresh tokens for a user.
* A token with scope {@code delete:refresh_tokens} is needed.
* 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>
*
* @param userId the user to delete the refresh tokens for
* @return a Request to execute.
*/
public Request<Void> deleteRefreshTokens(String userId) {
Asserts.assertNotNull(userId, "user ID");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/users")
.addPathSegment(userId)
.addPathSegment("refresh-tokens")
.build()
.toString();

return new VoidRequest(this.client, tokenProvider, url, HttpMethod.DELETE);
}


/**
* Get sessions for user
* A token with {@code read:sessions} is needed.
* 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>
*
* @param userId the role id
* @param filter an optional pagination filter
* @return a Request to execute
*/
public Request<SessionsPage> listSessions(String userId, PageFilter filter) {
Asserts.assertNotNull(userId, "user ID");
HttpUrl.Builder builder = baseUrl
.newBuilder()
.addPathSegments("api/v2/users")
.addPathSegment(userId)
.addPathSegment("sessions");
if (filter != null) {
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue()));
}
}
String url = builder.build().toString();
return new BaseRequest<>(client, tokenProvider, url, HttpMethod.GET, new TypeReference<SessionsPage>() {
});
}

/**
* Delete sessions for user
* A token with scope {@code delete:sessions} is needed.
* 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>
*
* @param userId the user to delete the sessions for
* @return a Request to execute.
*/
public Request<Void> deleteSessions(String userId) {
Asserts.assertNotNull(userId, "user ID");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/users")
.addPathSegment(userId)
.addPathSegment("sessions")
.build()
.toString();

return new VoidRequest(this.client, tokenProvider, url, HttpMethod.DELETE);
}

private static void encodeAndAddQueryParam(HttpUrl.Builder builder, BaseFilter filter) {
if (filter != null) {
for (Map.Entry<String, Object> e : filter.getAsMap().entrySet()) {
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/com/auth0/json/mgmt/refreshtokens/Device.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.auth0.json.mgmt.refreshtokens;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Device {
@JsonProperty("initial_ip")
private String initialIp;
@JsonProperty("initial_asn")
private String initialAsn;
@JsonProperty("initial_user_agent")
private String initialUserAgent;
@JsonProperty("last_ip")
private String lastIp;
@JsonProperty("last_asn")
private String lastAsn;
@JsonProperty("last_user_agent")
private String lastUserAgent;

/**
* @return First IP address associated with this session
*/
public String getInitialIp() {
return initialIp;
}

/**
* @return First autonomous system number associated with this session
*/
public String getInitialAsn() {
return initialAsn;
}

/**
* @return First user agent associated with this session
*/
public String getInitialUserAgent() {
return initialUserAgent;
}

/**
* @return Last IP address from which this user logged in
*/
public String getLastIp() {
return lastIp;
}

/**
* @return Last autonomous system number from which this user logged in
*/
public String getLastAsn() {
return lastAsn;
}

/**
* @return Last user agent of the device from which this user logged in
*/
public String getLastUserAgent() {
return lastUserAgent;
}
}
Loading
Loading