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 3 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
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.users.refreshtokens.RefreshTokensPage;
import com.auth0.json.mgmt.users.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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.auth0.client.mgmt.filter;

public class CheckpointPaginationFilter extends BaseFilter {
jimmyjames marked this conversation as resolved.
Show resolved Hide resolved

/**
* Return results inside an object that contains the total result count (true) or as a direct array of results (false, default).
*
* @param includeTotals whether to include or not total result count.
* @return this filter instance
*/
public CheckpointPaginationFilter withTotals(boolean includeTotals) {
parameters.put("include_totals", includeTotals);
return this;
}

/**
* Optional ID from which to start selection (exclusive).
*
* @param from the ID from which to start selection. This can be obtained from the {@code next} field returned from
* a checkpoint-paginated result.
* @return this filter instance.
*/
public CheckpointPaginationFilter withFrom(String from) {
parameters.put("from", from);
return this;
}

/**
* Number of results per page. Defaults to 50.
*
* @param take the amount of entries to retrieve per page.
* @return this filter instance.
*/
public CheckpointPaginationFilter withTake(int take) {
parameters.put("take", take);
return this;
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/auth0/json/mgmt/users/refreshtokens/Device.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.auth0.json.mgmt.users.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.auth0.json.mgmt.users.refreshtokens;

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

import java.util.Date;
import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RefreshToken {
@JsonProperty("id")
private String id;
@JsonProperty("user_id")
private String userId;
@JsonProperty("created_at")
private Date createdAt;
@JsonProperty("idle_expires_at")
private Date idleExpiresAt;
@JsonProperty("expires_at")
private Date expiresAt;
@JsonProperty("device")
private Device device;
@JsonProperty("client_id")
private String clientId;
@JsonProperty("session_id")
private String sessionId;
@JsonProperty("rotating")
private Boolean rotating;
@JsonProperty("resource_servers")
private List<ResourceServer> resourceServers;
@JsonProperty("last_exchanged_at")
private Date lastExchangedAt;

/**
* @return The ID of the refresh token
*/
public String getId() {
return id;
}

/**
* @return ID of the user which can be used when interacting with other APIs.
*/
public String getUserId() {
return userId;
}

/**
* @return The date and time when the refresh token was created
*/
public Date getCreatedAt() {
return createdAt;
}

/**
*
* @return The date and time when the refresh token will expire if idle
*/
public Date getIdleExpiresAt() {
return idleExpiresAt;
}

/**
*
* @return The date and time when the refresh token will expire
*/
public Date getExpiresAt() {
return expiresAt;
}

/**
* @return Device information
*/
public Device getDevice() {
return device;
}

/**
* @return ID of the client application granted with this refresh token
*/
public String getClientId() {
return clientId;
}

/**
*
* @return ID of the authenticated session used to obtain this refresh-token
*/
public String getSessionId() {
return sessionId;
}

/**
* @return True if the token is a rotating refresh token
*/
public Boolean isRotating() {
return rotating;
}

/**
* @return A list of the resource server IDs associated to this refresh-token and their granted scopes
*/
public List<ResourceServer> getResourceServers() {
return resourceServers;
}

/**
* @return The date and time when the refresh token was last exchanged
*/
public Date getLastExchangedAt() {
return lastExchangedAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.auth0.json.mgmt.users.refreshtokens;

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

import java.util.List;

/**
* Class that represents a page of Refresh Tokens.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RefreshTokensPage {
@JsonProperty("total")
private Integer total;

@JsonProperty("next")
private String next;

@JsonProperty("tokens")
private List<RefreshToken> tokens;

/**
* @return the total number of refresh tokens. This is only present when `include_totals` is passed as a query parameter.
*/
public Integer getTotal() {
return total;
}

/**
* @return the token ID from which to start selection for a new page
*/
public String getNext() {
return next;
}

/**
* @return the list of Tokens
*/
public List<RefreshToken> getTokens() {
return tokens;
}
}
Loading
Loading