Skip to content

Add CustomClaims to AccessKey creation #104

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

Merged
merged 2 commits into from
Mar 6, 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
1 change: 1 addition & 0 deletions src/main/java/com/descope/model/mgmt/AccessKeyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public class AccessKeyRequest {
private List<String> roleNames;
private List<Map<String, Object>> keyTenants;
private String userId;
private Map<String, Object> customClaims;
}
7 changes: 7 additions & 0 deletions src/main/java/com/descope/sdk/mgmt/AccessKeyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
import com.descope.model.mgmt.AccessKeyResponse;
import com.descope.model.mgmt.AccessKeyResponseList;
import java.util.List;
import java.util.Map;

public interface AccessKeyService {

AccessKeyResponse create(String name, int expireTime, List<String> roleNames, List<AssociatedTenant> keyTenants)
throws DescopeException;

AccessKeyResponse create(String name, int expireTime, List<String> roleNames, List<AssociatedTenant> keyTenants,
Map<String, Object> customClaims) throws DescopeException;

AccessKeyResponse create(String name, int expireTime, List<String> roleNames, List<AssociatedTenant> keyTenants,
String userId) throws DescopeException;

AccessKeyResponse create(String name, int expireTime, List<String> roleNames, List<AssociatedTenant> keyTenants,
String userId, Map<String, Object> customClaims) throws DescopeException;

AccessKeyResponse load(String id) throws DescopeException;

AccessKeyResponseList searchAll(List<String> tenantIDs) throws DescopeException;
Expand Down
34 changes: 22 additions & 12 deletions src/main/java/com/descope/sdk/mgmt/impl/AccessKeyServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,34 @@ class AccessKeyServiceImpl extends ManagementsBase implements AccessKeyService {
public AccessKeyResponse create(
String name, int expireTime, List<String> roleNames, List<AssociatedTenant> keyTenants)
throws DescopeException {
if (StringUtils.isBlank(name)) {
throw ServerCommonException.invalidArgument("Name");
}
AccessKeyRequest body = createAccessKeyBody(name, expireTime, roleNames, keyTenants, null);
ApiProxy apiProxy = getApiProxy();
return apiProxy.post(getUri(MANAGEMENT_ACCESS_KEY_CREATE_LINK), body, AccessKeyResponse.class);
return create(name, expireTime, roleNames, keyTenants, null, null);
}

@Override
public AccessKeyResponse create(
String name, int expireTime, List<String> roleNames, List<AssociatedTenant> keyTenants,
Map<String, Object> customClaims) throws DescopeException {
return create(name, expireTime, roleNames, keyTenants, null, customClaims);
}

@Override
public AccessKeyResponse create(
String name, int expireTime, List<String> roleNames, List<AssociatedTenant> keyTenants, String userId)
throws DescopeException {
if (StringUtils.isBlank(name)) {
throw ServerCommonException.invalidArgument("Name");
}
if (StringUtils.isBlank(userId)) {
throw ServerCommonException.invalidArgument("user id");
}
AccessKeyRequest body = createAccessKeyBody(name, expireTime, roleNames, keyTenants, userId);
return create(name, expireTime, roleNames, keyTenants, userId, null);
}

@Override
public AccessKeyResponse create(
String name, int expireTime, List<String> roleNames, List<AssociatedTenant> keyTenants, String userId,
Map<String, Object> customClaims) throws DescopeException {
if (StringUtils.isBlank(name)) {
throw ServerCommonException.invalidArgument("Name");
}
AccessKeyRequest body = createAccessKeyBody(name, expireTime, roleNames, keyTenants, userId, customClaims);
ApiProxy apiProxy = getApiProxy();
return apiProxy.post(getUri(MANAGEMENT_ACCESS_KEY_CREATE_LINK), body, AccessKeyResponse.class);
}
Expand Down Expand Up @@ -123,14 +132,15 @@ public void delete(String id) throws DescopeException {
apiProxy.post(getUri(MANAGEMENT_ACCESS_KEY_DELETE_LINK), request, Void.class);
}

private AccessKeyRequest createAccessKeyBody(
String name, int expireTime, List<String> roleNames, List<AssociatedTenant> keyTenants, String userId) {
private AccessKeyRequest createAccessKeyBody(String name, int expireTime, List<String> roleNames,
List<AssociatedTenant> keyTenants, String userId, Map<String, Object> customClaims) {
return AccessKeyRequest.builder()
.name(name)
.expireTime(expireTime)
.roleNames(roleNames)
.keyTenants(MgmtUtils.createAssociatedTenantList(keyTenants))
.userId(userId)
.customClaims(customClaims)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,17 @@ void testFunctionalFullCycle() throws Exception {
@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);
AccessKeyResponse resp = accessKeyService.create(name, 0, null, null, null, mapOf("K1", "V1"));
Token token = authenticationService.exchangeAccessKey(resp.getCleartext(),
new AccessKeyLoginOptions(mapOf("kuku", "kiki")));

// temporary
@SuppressWarnings("unchecked")
// Validate the nsec claims (passed through the exchange method)
Map<String, Object> nsecClaims = Map.class.cast(token.getClaims().get("nsec"));
assertEquals("kiki", nsecClaims.get("kuku"));
// Validate the secured claims (passed through the Create method)
assertEquals("V1", token.getClaims().get("K1"));
accessKeyService.delete(resp.getKey().getId());
}
}