Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #248 from galasa-dev/iss1729-revoke-tokens
Browse files Browse the repository at this point in the history
Separate external User bean from its internal usage, add Dex user ID to token records
  • Loading branch information
KirbyKatcher authored Jul 9, 2024
2 parents e9885b9 + 81c7fa9 commit bea4f96
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import dev.galasa.extensions.common.couchdb.pojos.ViewRow;
import dev.galasa.extensions.common.api.HttpRequestFactory;
import dev.galasa.framework.spi.auth.IInternalAuthToken;
import dev.galasa.framework.spi.auth.IInternalUser;
import dev.galasa.framework.spi.auth.IAuthStore;
import dev.galasa.framework.spi.auth.User;
import dev.galasa.framework.spi.utils.ITimeService;
import dev.galasa.framework.spi.auth.AuthStoreException;

Expand Down Expand Up @@ -94,9 +94,10 @@ public void shutdown() throws AuthStoreException {
}

@Override
public void storeToken(String clientId, String description, User owner) throws AuthStoreException {
public void storeToken(String clientId, String description, IInternalUser owner) throws AuthStoreException {
// Create the JSON payload representing the token to store
String tokenJson = gson.toJson(new CouchdbAuthToken(clientId, description, timeService.now(), owner));
CouchdbUser couchdbUser = new CouchdbUser(owner);
String tokenJson = gson.toJson(new CouchdbAuthToken(clientId, description, timeService.now(), couchdbUser));

try {
createDocument(TOKENS_DATABASE_NAME, tokenJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
import java.time.Instant;

import dev.galasa.framework.spi.auth.IInternalAuthToken;
import dev.galasa.framework.spi.auth.User;
import dev.galasa.framework.spi.auth.IInternalUser;

public class CouchdbAuthToken implements IInternalAuthToken {

private String _id;
private String dexClientId;
private String description;
private Instant creationTime;
private User owner;
private CouchdbUser owner;

public CouchdbAuthToken(String clientId, String description, Instant creationTime, User owner) {
public CouchdbAuthToken(String clientId, String description, Instant creationTime, CouchdbUser owner) {
this.dexClientId = clientId;
this.description = description;
this.creationTime = creationTime;
this.owner = owner;
}

public CouchdbAuthToken(String documentId, String clientId, String description, Instant creationTime, User owner) {
public CouchdbAuthToken(String documentId, String clientId, String description, Instant creationTime, CouchdbUser owner) {
this(clientId, description, creationTime, owner);
this._id = documentId;
}
Expand All @@ -42,7 +42,7 @@ public Instant getCreationTime() {
return creationTime;
}

public User getOwner() {
public IInternalUser getOwner() {
return owner;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright contributors to the Galasa project
*
* SPDX-License-Identifier: EPL-2.0
*/
package dev.galasa.auth.couchdb.internal;

import dev.galasa.framework.spi.auth.IInternalUser;

public class CouchdbUser implements IInternalUser {

private String loginId;
private String dexUserId;

public CouchdbUser(String loginId, String dexUserId) {
this.loginId = loginId;
this.dexUserId = dexUserId;
}

public CouchdbUser(IInternalUser user) {
this.loginId = user.getLoginId();
this.dexUserId = user.getDexUserId();
}

@Override
public String getDexUserId() {
return dexUserId;
}

@Override
public String getLoginId() {
return loginId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import dev.galasa.auth.couchdb.internal.CouchdbAuthStore;
import dev.galasa.auth.couchdb.internal.CouchdbAuthToken;
import dev.galasa.auth.couchdb.internal.CouchdbUser;
import dev.galasa.extensions.common.couchdb.pojos.IdRev;
import dev.galasa.extensions.common.couchdb.pojos.PutPostResponse;
import dev.galasa.extensions.common.couchdb.pojos.ViewResponse;
Expand All @@ -33,7 +34,6 @@
import dev.galasa.extensions.mocks.couchdb.MockCouchdbValidator;
import dev.galasa.framework.spi.auth.AuthStoreException;
import dev.galasa.framework.spi.auth.IInternalAuthToken;
import dev.galasa.framework.spi.auth.User;

public class TestCouchdbAuthStore {

Expand Down Expand Up @@ -135,7 +135,7 @@ public void testGetTokensReturnsTokensFromCouchdbOK() throws Exception {
ViewResponse mockAllDocsResponse = new ViewResponse();
mockAllDocsResponse.rows = mockDocs;

CouchdbAuthToken mockToken = new CouchdbAuthToken("token1", "dex-client", "my test token", Instant.now(), new User("johndoe"));
CouchdbAuthToken mockToken = new CouchdbAuthToken("token1", "dex-client", "my test token", Instant.now(), new CouchdbUser("johndoe", "dex-user-id"));
List<HttpInteraction> interactions = new ArrayList<HttpInteraction>();
interactions.add(new GetAllTokenDocumentsInteraction("https://my-auth-store/galasa_tokens/_all_docs", HttpStatus.SC_OK, mockAllDocsResponse));
interactions.add(new GetTokenDocumentInteraction<CouchdbAuthToken>("https://my-auth-store/galasa_tokens/token1", HttpStatus.SC_OK, mockToken));
Expand Down Expand Up @@ -174,7 +174,7 @@ public void testStoreTokenSendsRequestToCreateTokenDocumentOK() throws Exception
CouchdbAuthStore authStore = new CouchdbAuthStore(authStoreUri, httpClientFactory, new HttpRequestFactoryImpl(), logFactory, new MockCouchdbValidator(), mockTimeService);

// When...
authStore.storeToken("this-is-a-dex-id", "my token", new User("user1"));
authStore.storeToken("this-is-a-dex-id", "my token", new CouchdbUser("user1", "user1-id"));

// Then the assertions made in the create token document interaction shouldn't have failed.
}
Expand All @@ -196,7 +196,7 @@ public void testStoreTokenWithFailingRequestToCreateTokenDocumentReturnsError()
CouchdbAuthStore authStore = new CouchdbAuthStore(authStoreUri, httpClientFactory, new HttpRequestFactoryImpl(), logFactory, new MockCouchdbValidator(), mockTimeService);

// When...
AuthStoreException thrown = catchThrowableOfType(() -> authStore.storeToken("this-is-a-dex-id", "my token", new User("user1")), AuthStoreException.class);
AuthStoreException thrown = catchThrowableOfType(() -> authStore.storeToken("this-is-a-dex-id", "my token", new CouchdbUser("user1", "user1-id")), AuthStoreException.class);

// Then...
assertThat(thrown).isNotNull();
Expand Down

0 comments on commit bea4f96

Please sign in to comment.