Skip to content

Commit

Permalink
added support for /session and /refreshTokens API's and updated relev…
Browse files Browse the repository at this point in the history
…ant test cases
  • Loading branch information
tanya732 committed Sep 5, 2024
1 parent a556bcd commit 507f8c3
Show file tree
Hide file tree
Showing 30 changed files with 841 additions and 188 deletions.
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
*/
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);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/auth0/client/mgmt/UsersEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +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.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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.auth0.json.mgmt.users.refreshtokens;
package com.auth0.json.mgmt.refreshtokens;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.auth0.json.mgmt.users.refreshtokens;
package com.auth0.json.mgmt.refreshtokens;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.auth0.json.mgmt.users.refreshtokens;
package com.auth0.json.mgmt.refreshtokens;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.auth0.json.mgmt.users.refreshtokens;
package com.auth0.json.mgmt.refreshtokens;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.auth0.json.mgmt.users.sessions;
package com.auth0.json.mgmt.sessions;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.auth0.json.mgmt.users.sessions;
package com.auth0.json.mgmt.sessions;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.auth0.json.mgmt.users.sessions;
package com.auth0.json.mgmt.sessions;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.auth0.json.mgmt.users.sessions;
package com.auth0.json.mgmt.sessions;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.auth0.json.mgmt.users.sessions;
package com.auth0.json.mgmt.sessions;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.auth0.json.mgmt.users.sessions;
package com.auth0.json.mgmt.sessions;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/auth0/client/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public class MockServer {
public static final String MGMT_USERS_PAGED_LIST = "src/test/resources/mgmt/users_paged_list.json";
public static final String MGMT_USER_PERMISSIONS_PAGED_LIST = "src/test/resources/mgmt/user_permissions_paged_list.json";
public static final String MGMT_USER_ROLES_PAGED_LIST = "src/test/resources/mgmt/user_roles_paged_list.json";
public static final String MGMT_REFRESH_TOKEN = "src/test/resources/mgmt/refresh_token.json";
public static final String MGMT_SESSION = "src/test/resources/mgmt/session.json";
public static final String MGMT_USER_REFRESH_TOKENS = "src/test/resources/mgmt/user_refresh_tokens.json";
public static final String MGMT_USER_SESSIONS = "src/test/resources/mgmt/user_sessions.json";
public static final String MGMT_USER = "src/test/resources/mgmt/user.json";
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/com/auth0/client/mgmt/RefreshTokensEntityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.auth0.client.mgmt;

import com.auth0.json.mgmt.refreshtokens.RefreshToken;
import com.auth0.net.Request;
import com.auth0.net.client.HttpMethod;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.Test;

import static com.auth0.AssertsUtil.verifyThrows;
import static com.auth0.client.MockServer.MGMT_REFRESH_TOKEN;
import static com.auth0.client.RecordedRequestMatcher.hasHeader;
import static com.auth0.client.RecordedRequestMatcher.hasMethodAndPath;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

public class RefreshTokensEntityTest extends BaseMgmtEntityTest{

@Test
public void shouldThrowOnGetWithNullRefreshTokenId() {
verifyThrows(IllegalArgumentException.class,
() -> api.refreshTokens().get(null),
"'refresh token ID' cannot be null!");
}

@Test
public void shouldGetRefreshToken() throws Exception {
Request<RefreshToken> request = api.refreshTokens().get("refresh_token_ID");
assertThat(request, is(notNullValue()));

server.jsonResponse(MGMT_REFRESH_TOKEN, 200);
RefreshToken response =request.execute().getBody();
RecordedRequest recordedRequest = server.takeRequest();

assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/refresh-tokens/refresh_token_ID"));
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));

assertThat(response, is(notNullValue()));
}

@Test
public void shouldThrowOnDeleteWithNullRefreshTokenId() {
verifyThrows(IllegalArgumentException.class,
() -> api.refreshTokens().delete(null),
"'refresh token ID' cannot be null!");
}

@Test
public void shouldDeleteRefreshToken() throws Exception {
Request<Void> request = api.refreshTokens().delete("refresh_token_ID");
assertThat(request, is(notNullValue()));

server.noContentResponse();
request.execute().getBody();
RecordedRequest recordedRequest = server.takeRequest();

assertThat(recordedRequest, hasMethodAndPath(HttpMethod.DELETE, "/api/v2/refresh-tokens/refresh_token_ID"));
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
}
}
Loading

0 comments on commit 507f8c3

Please sign in to comment.