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 access keys custom claims #101

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions src/main/java/com/descope/model/auth/AccessKeyLoginOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.descope.model.auth;

import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AccessKeyLoginOptions {
private Map<String, Object> customClaims;
}
11 changes: 11 additions & 0 deletions src/main/java/com/descope/sdk/auth/AuthenticationService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.descope.sdk.auth;

import com.descope.exception.DescopeException;
import com.descope.model.auth.AccessKeyLoginOptions;
import com.descope.model.jwt.Token;
import com.descope.model.user.response.UserHistoryResponse;
import com.descope.model.user.response.UserResponse;
Expand Down Expand Up @@ -50,6 +51,16 @@ Token validateAndRefreshSessionWithTokens(String sessionToken, String refreshTok
*/
Token exchangeAccessKey(String accessKey) throws DescopeException;

/**
* Use to exchange an access key for a session token with login options.
*
* @param accessKey - Access Key
* @param loginOptions - {@link AccessKeyLoginOptions loginOptions}
* @return {@link Token Token}
* @throws DescopeException if there is an error
*/
Token exchangeAccessKey(String accessKey, AccessKeyLoginOptions loginOptions) throws DescopeException;

/**
* Use to ensure that a validated session token has been granted the specified permissions. This
* is a shortcut for validatePermissions(token, "", permissions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import static com.descope.literals.Routes.AuthEndPoints.LOG_OUT_ALL_LINK;
import static com.descope.literals.Routes.AuthEndPoints.LOG_OUT_LINK;
import static com.descope.literals.Routes.AuthEndPoints.ME_LINK;
import static com.descope.utils.CollectionUtils.mapOf;

import com.descope.exception.DescopeException;
import com.descope.exception.ServerCommonException;
import com.descope.model.auth.AccessKeyLoginOptions;
import com.descope.model.auth.AuthenticationInfo;
import com.descope.model.auth.ExchangeTokenRequest;
import com.descope.model.client.Client;
Expand Down Expand Up @@ -71,10 +73,19 @@ public Token validateAndRefreshSessionWithTokens(String sessionToken, String ref

@Override
public Token exchangeAccessKey(String accessKey) throws DescopeException {
return exchangeAccessKey(accessKey, null);
}

@Override
public Token exchangeAccessKey(String accessKey, AccessKeyLoginOptions loginOptions) throws DescopeException {
if (StringUtils.isBlank(accessKey)) {
throw ServerCommonException.invalidArgument("accessKey");
}
ApiProxy apiProxy = getApiProxy(accessKey);
URI exchangeAccessKeyLinkURL = composeExchangeAccessKeyLinkURL();

JWTResponse jwtResponse = apiProxy.post(exchangeAccessKeyLinkURL, null, JWTResponse.class);
JWTResponse jwtResponse = apiProxy.post(exchangeAccessKeyLinkURL,
mapOf("loginOptions", loginOptions), JWTResponse.class);
AuthenticationInfo authenticationInfo = getAuthenticationInfo(jwtResponse);
return authenticationInfo.getToken();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.descope.sdk.auth.impl;

import static com.descope.sdk.TestUtils.MOCK_TOKEN;
import static com.descope.utils.CollectionUtils.mapOf;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -9,22 +10,26 @@

import com.descope.enums.DeliveryMethod;
import com.descope.exception.RateLimitExceededException;
import com.descope.model.auth.AccessKeyLoginOptions;
import com.descope.model.auth.AuthenticationInfo;
import com.descope.model.auth.AuthenticationServices;
import com.descope.model.client.Client;
import com.descope.model.jwt.Token;
import com.descope.model.mgmt.AccessKeyResponse;
import com.descope.model.mgmt.ManagementServices;
import com.descope.model.user.request.UserRequest;
import com.descope.model.user.response.OTPTestUserResponse;
import com.descope.model.user.response.UserResponse;
import com.descope.sdk.TestUtils;
import com.descope.sdk.auth.AuthenticationService;
import com.descope.sdk.auth.OTPService;
import com.descope.sdk.mgmt.AccessKeyService;
import com.descope.sdk.mgmt.RolesService;
import com.descope.sdk.mgmt.TenantService;
import com.descope.sdk.mgmt.UserService;
import com.descope.sdk.mgmt.impl.ManagementServiceBuilder;
import java.util.Arrays;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.RetryingTest;
Expand All @@ -36,6 +41,7 @@ public class AuthenticationServiceImplTest {
private OTPService otpService;
private RolesService roleService;
private TenantService tenantService;
private AccessKeyService accessKeyService;

@BeforeEach
void setUp() {
Expand All @@ -47,6 +53,7 @@ void setUp() {
this.userService = mgmtServices.getUserService();
this.roleService = mgmtServices.getRolesService();
this.tenantService = mgmtServices.getTenantService();
this.accessKeyService = mgmtServices.getAccessKeyService();
}

@Test
Expand Down Expand Up @@ -122,4 +129,17 @@ void testFunctionalFullCycle() throws Exception {
authenticationService.logout(authInfo.getRefreshToken().getJwt());
userService.delete(loginId);
}

@RetryingTest(value = 3, suspendForMs = 30000, onExceptions = RateLimitExceededException.class)
void testFunctionalExchangeToken() throws Exception {
String name = TestUtils.getRandomName("ak-");
AccessKeyResponse resp = accessKeyService.create(name, 0, null, null);
Token token = authenticationService.exchangeAccessKey(resp.getCleartext(),
new AccessKeyLoginOptions(mapOf("kuku", "kiki")));
// temporary
@SuppressWarnings("unchecked")
Map<String, Object> nsecClaims = Map.class.cast(token.getClaims().get("nsec"));
assertEquals("kiki", nsecClaims.get("kuku"));
accessKeyService.delete(resp.getKey().getId());
}
}
Loading