Skip to content

Commit 7c036e7

Browse files
jaktanya732
authored andcommitted
feat: add sessions and refresh tokens
1 parent 02b0a15 commit 7c036e7

18 files changed

Lines changed: 959 additions & 0 deletions

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.users.refreshtokens.RefreshTokensPage;
17+
import com.auth0.json.mgmt.users.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, CheckpointPaginationFilter 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, CheckpointPaginationFilter 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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.auth0.client.mgmt.filter;
2+
3+
public class CheckpointPaginationFilter extends BaseFilter {
4+
5+
/**
6+
* Return results inside an object that contains the total result count (true) or as a direct array of results (false, default).
7+
*
8+
* @param includeTotals whether to include or not total result count.
9+
* @return this filter instance
10+
*/
11+
public CheckpointPaginationFilter withTotals(boolean includeTotals) {
12+
parameters.put("include_totals", includeTotals);
13+
return this;
14+
}
15+
16+
/**
17+
* Optional ID from which to start selection (exclusive).
18+
*
19+
* @param from the ID from which to start selection. This can be obtained from the {@code next} field returned from
20+
* a checkpoint-paginated result.
21+
* @return this filter instance.
22+
*/
23+
public CheckpointPaginationFilter withFrom(String from) {
24+
parameters.put("from", from);
25+
return this;
26+
}
27+
28+
/**
29+
* Number of results per page. Defaults to 50.
30+
*
31+
* @param take the amount of entries to retrieve per page.
32+
* @return this filter instance.
33+
*/
34+
public CheckpointPaginationFilter withTake(int take) {
35+
parameters.put("take", take);
36+
return this;
37+
}
38+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.auth0.json.mgmt.users.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+
import java.util.Date;
8+
import java.util.List;
9+
10+
@JsonIgnoreProperties(ignoreUnknown = true)
11+
@JsonInclude(JsonInclude.Include.NON_NULL)
12+
public class RefreshToken {
13+
@JsonProperty("id")
14+
private String id;
15+
@JsonProperty("user_id")
16+
private String userId;
17+
@JsonProperty("created_at")
18+
private Date createdAt;
19+
@JsonProperty("idle_expires_at")
20+
private Date idleExpiresAt;
21+
@JsonProperty("expires_at")
22+
private Date expiresAt;
23+
@JsonProperty("client_id")
24+
private String clientId;
25+
@JsonProperty("session_id")
26+
private String sessionId;
27+
@JsonProperty("rotating")
28+
private Boolean rotating;
29+
@JsonProperty("resource_servers")
30+
private List<ResourceServer> resourceServers;
31+
32+
/**
33+
* @return The ID of the refresh token
34+
*/
35+
public String getId() {
36+
return id;
37+
}
38+
39+
/**
40+
* @return ID of the user which can be used when interacting with other APIs.
41+
*/
42+
public String getUserId() {
43+
return userId;
44+
}
45+
46+
/**
47+
* @return The date and time when the refresh token was created
48+
*/
49+
public Date getCreatedAt() {
50+
return createdAt;
51+
}
52+
53+
/**
54+
*
55+
* @return The date and time when the refresh token will expire if idle
56+
*/
57+
public Date getIdleExpiresAt() {
58+
return idleExpiresAt;
59+
}
60+
61+
/**
62+
*
63+
* @return The date and time when the refresh token will expire
64+
*/
65+
public Date getExpiresAt() {
66+
return expiresAt;
67+
}
68+
69+
/**
70+
* @return ID of the client application granted with this refresh token
71+
*/
72+
public String getClientId() {
73+
return clientId;
74+
}
75+
76+
/**
77+
*
78+
* @return ID of the authenticated session used to obtain this refresh-token
79+
*/
80+
public String getSessionId() {
81+
return sessionId;
82+
}
83+
84+
/**
85+
* @return True if the token is a rotating refresh token
86+
*/
87+
public Boolean isRotating() {
88+
return rotating;
89+
}
90+
91+
/**
92+
* @return A list of the resource server IDs associated to this refresh-token and their granted scopes
93+
*/
94+
public List<ResourceServer> getResourceServers() {
95+
return resourceServers;
96+
}
97+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.auth0.json.mgmt.users.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+
import java.util.List;
8+
9+
/**
10+
* This does not extend com.auth0.json.mgmt.Page<RefreshToken> because the URL only supports "next" and "take" pagination.
11+
*/
12+
@JsonIgnoreProperties(ignoreUnknown = true)
13+
@JsonInclude(JsonInclude.Include.NON_NULL)
14+
public class RefreshTokensPage {
15+
@JsonProperty("total")
16+
private Integer total;
17+
18+
@JsonProperty("next")
19+
private String next;
20+
21+
@JsonProperty("tokens")
22+
private List<RefreshToken> tokens;
23+
24+
/**
25+
* @return the total number of refresh tokens. This is only present when `include_totals` is passed as a query parameter.
26+
*/
27+
public Integer getTotal() {
28+
return total;
29+
}
30+
31+
/**
32+
* @return the token ID from which to start selection for a new page
33+
*/
34+
public String getNext() {
35+
return next;
36+
}
37+
38+
/**
39+
* @return the list of Tokens
40+
*/
41+
public List<RefreshToken> getTokens() {
42+
return tokens;
43+
}
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.auth0.json.mgmt.users.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+
import java.util.List;
8+
9+
@JsonIgnoreProperties(ignoreUnknown = true)
10+
@JsonInclude(JsonInclude.Include.NON_NULL)
11+
public class ResourceServer {
12+
@JsonProperty("audience")
13+
private String audience;
14+
@JsonProperty("scopes")
15+
private List<String> scopes;
16+
17+
/**
18+
* @return Resource server ID
19+
*/
20+
public String getAudience() {
21+
return audience;
22+
}
23+
24+
/**
25+
* @return List of scopes for the refresh token
26+
*/
27+
public List<String> getScopes() {
28+
return scopes;
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.auth0.json.mgmt.users.sessions;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
import java.util.List;
8+
9+
@JsonIgnoreProperties(ignoreUnknown = true)
10+
@JsonInclude(JsonInclude.Include.NON_NULL)
11+
public class Authentication {
12+
@JsonProperty("methods")
13+
private List<AuthenticationMethod> methods;
14+
15+
/**
16+
* @return Contains the authentication methods a user has completed during their session
17+
*/
18+
public List<AuthenticationMethod> getMethods() {
19+
return methods;
20+
}
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.auth0.json.mgmt.users.sessions;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
import java.util.Date;
8+
9+
@JsonIgnoreProperties(ignoreUnknown = true)
10+
@JsonInclude(JsonInclude.Include.NON_NULL)
11+
public class AuthenticationMethod {
12+
@JsonProperty("name")
13+
private String name;
14+
@JsonProperty("timestamp")
15+
private Date timestamp;
16+
@JsonProperty("type")
17+
private String type;
18+
19+
/**
20+
* @return One of: "federated", "passkey", "pwd", "sms", "email", "mfa", "mock" or a custom method denoted by a URL
21+
*/
22+
public String getName() {
23+
return name;
24+
}
25+
26+
/**
27+
* @return Timestamp of when the signal was received
28+
*/
29+
public Date getTimestamp() {
30+
return timestamp;
31+
}
32+
33+
/**
34+
* @return A specific MFA factor. Only present when "name" is set to "mfa"
35+
*/
36+
public String getType() {
37+
return type;
38+
}
39+
}

0 commit comments

Comments
 (0)