Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
import com.walmartlabs.concord.client2.*;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class ApiKeyIT extends AbstractServerIT {

Expand Down Expand Up @@ -62,4 +63,38 @@ public void testOwner() throws Exception {
cakr = apiKeyResource.createUserApiKey(new CreateApiKeyRequest().username(userAName));
assertTrue(cakr.getOk());
}

@Test
public void testCreatingKeyWithoutUsername() throws Exception {
String userName = "userA_" + randomString();

UsersApi usersApi = new UsersApi(getApiClient());
CreateUserResponse user = usersApi.createOrUpdateUser(new CreateUserRequest()
.username(userName)
.type(CreateUserRequest.TypeEnum.LOCAL));

// the new user has no api keys initially

ApiKeysApi apiKeyResource = new ApiKeysApi(getApiClient());
List<ApiKeyEntry> keys = apiKeyResource.listUserApiKeys(user.getId());
assertEquals(0, keys.size());

// admin creates a new api key for the new user

CreateApiKeyResponse cakr = apiKeyResource.createUserApiKey(new CreateApiKeyRequest().username(userName));
assertTrue(cakr.getOk());
keys = apiKeyResource.listUserApiKeys(user.getId());
assertEquals(1, keys.size());

// the new user creates another api key for themselves

setApiKey(cakr.getKey());
cakr = apiKeyResource.createUserApiKey(new CreateApiKeyRequest());
assertTrue(cakr.getOk());

// the new user lists all their api keys (should be 2)

keys = apiKeyResource.listUserApiKeys(user.getId());
assertEquals(2, keys.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ public ApiKeyManager(ApiKeyConfiguration cfg,
this.auditLog = requireNonNull(auditLog);
}


public CreateApiKeyResponse create(CreateApiKeyRequest req) {
String key = assertKeyValue(req);

UUID userId = assertUserId(req.getUserId());
if (userId == null) {
userId = assertUsername(req.getUsername(), req.getUserDomain(), req.getUserType());
}


if (userId == null) {
userId = UserPrincipal.assertCurrent().getId();
}

assertOwner(userId);

String name = trim(req.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
@WithTimer
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
UserPrincipal p = principals.oneByType(UserPrincipal.class);
if (!REALM_NAME.equals(p.getRealm())) {
if (p == null || !REALM_NAME.equals(p.getRealm())) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
UserPrincipal p = principals.oneByType(UserPrincipal.class);
if (!REALM_NAME.equals(p.getRealm())) {
if (p == null || !REALM_NAME.equals(p.getRealm())) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
UserPrincipal p = principals.oneByType(UserPrincipal.class);
if (!REALM_NAME.equals(p.getRealm())) {
if (p == null || !REALM_NAME.equals(p.getRealm())) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
UserPrincipal p = principals.oneByType(UserPrincipal.class);
if (!REALM_NAME.equals(p.getRealm())) {
if (p == null || !REALM_NAME.equals(p.getRealm())) {
return null;
}

Expand Down