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

Added getTokensByLoginId method #648

Merged
merged 4 commits into from
Sep 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ public class AuthTokensRoute extends BaseRoute {
private DexGrpcClient dexGrpcClient;
private Environment env;

private static final String ID_TOKEN_KEY = "id_token";
private static final String ID_TOKEN_KEY = "id_token";
private static final String REFRESH_TOKEN_KEY = "refresh_token";
public static final String QUERY_PARAM_LOGIN_ID = "loginId";

// Regex to match /auth/tokens and /auth/tokens/ only
private static final String PATH_PATTERN = "\\/tokens\\/?";

private static final IBeanValidator<TokenPayload> validator = new TokenPayloadValidator();

public AuthTokensRoute(
ResponseBuilder responseBuilder,
IOidcProvider oidcProvider,
DexGrpcClient dexGrpcClient,
IAuthStoreService authStoreService,
Environment env
) {
ResponseBuilder responseBuilder,
IOidcProvider oidcProvider,
DexGrpcClient dexGrpcClient,
IAuthStoreService authStoreService,
Environment env) {
super(responseBuilder, PATH_PATTERN);
this.oidcProvider = oidcProvider;
this.dexGrpcClient = dexGrpcClient;
Expand All @@ -76,36 +76,73 @@ public AuthTokensRoute(
/**
* GET requests to /auth/tokens return all the tokens stored in the tokens
* database, sorted by creation date order by default.
* This endpoint takes an optional query parameter 'loginId' for e.g
* loginId=admin
* Passing it returns a filtered list of token records stored in the auth store
* that matches the given login ID
*/
@Override
public HttpServletResponse handleGetRequest(String pathInfo, QueryParameters queryParams,
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, FrameworkException {
throws FrameworkException {

logger.info("handleGetRequest() entered");

List<AuthToken> tokensToReturn = new ArrayList<>();
List<IInternalAuthToken> authTokensFromAuthStore = new ArrayList<>();

if (queryParams.isParameterPresent(QUERY_PARAM_LOGIN_ID)) {

String loginId = queryParams.getSingleString(QUERY_PARAM_LOGIN_ID, null);
validateLoginId(loginId, pathInfo);
authTokensFromAuthStore = getTokensByLoginId(loginId);

} else {
authTokensFromAuthStore = getAllTokens();
}

// Convert the token received from the auth store into the token bean that will
// be returned as JSON
List<AuthToken>tokensToReturn = convertAuthStoreTokenIntoTokenBeans(authTokensFromAuthStore);

return getResponseBuilder().buildResponse(request, response, "application/json",
getTokensAsJsonString(tokensToReturn), HttpServletResponse.SC_OK);
}

private List<IInternalAuthToken> getAllTokens() throws FrameworkException {

try {
// Retrieve all the tokens and put them into a mutable list before sorting them based on their creation time
// Retrieve all the tokens and put them into a mutable list before sorting them
// based on their creation time
List<IInternalAuthToken> tokens = new ArrayList<>(authStoreService.getTokens());
Collections.sort(tokens, Comparator.comparing(IInternalAuthToken::getCreationTime));

// Convert the token received from the auth store into the token bean that will be returned as JSON
for (IInternalAuthToken token : tokens) {
User user = new User(token.getOwner().getLoginId());
tokensToReturn.add(new AuthToken(
token.getTokenId(),
token.getDescription(),
token.getCreationTime(),
user)
);
}
return tokens;

} catch (AuthStoreException e) {
ServletError error = new ServletError(GAL5053_FAILED_TO_RETRIEVE_TOKENS);
throw new InternalServletException(error, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}

}

public List<IInternalAuthToken> getTokensByLoginId(String loginId)
throws FrameworkException {

logger.info("fetching access tokens by loginId");
aashir21 marked this conversation as resolved.
Show resolved Hide resolved

try {

List<IInternalAuthToken> tokens = new ArrayList<>(authStoreService.getTokensByLoginId(loginId));
aashir21 marked this conversation as resolved.
Show resolved Hide resolved
Collections.sort(tokens, Comparator.comparing(IInternalAuthToken::getCreationTime));

logger.info("Access tokens by loginId fetched from auth store");
return tokens;

} catch (AuthStoreException e) {
ServletError error = new ServletError(GAL5053_FAILED_TO_RETRIEVE_TOKENS);
throw new InternalServletException(error, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}

return getResponseBuilder().buildResponse(request, response, "application/json", getTokensAsJsonString(tokensToReturn), HttpServletResponse.SC_OK);
}

/**
Expand All @@ -114,7 +151,8 @@ public HttpServletResponse handleGetRequest(String pathInfo, QueryParameters que
*/
@Override
public HttpServletResponse handlePostRequest(String pathInfo, QueryParameters queryParameters,
HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, FrameworkException {
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, FrameworkException {

logger.info("AuthRoute: handlePostRequest() entered.");

Expand All @@ -128,14 +166,16 @@ public HttpServletResponse handlePostRequest(String pathInfo, QueryParameters qu
JsonObject tokenResponseBodyJson = sendTokenPost(requestPayload);

// Return the JWT and refresh token as the servlet's response
if (tokenResponseBodyJson != null && tokenResponseBodyJson.has(ID_TOKEN_KEY) && tokenResponseBodyJson.has(REFRESH_TOKEN_KEY)) {
if (tokenResponseBodyJson != null && tokenResponseBodyJson.has(ID_TOKEN_KEY)
&& tokenResponseBodyJson.has(REFRESH_TOKEN_KEY)) {
logger.info("Bearer and refresh tokens successfully received from issuer.");

String jwt = tokenResponseBodyJson.get(ID_TOKEN_KEY).getAsString();
responseJson.addProperty("jwt", jwt);
responseJson.addProperty(REFRESH_TOKEN_KEY, tokenResponseBodyJson.get(REFRESH_TOKEN_KEY).getAsString());

// If we're refreshing an existing token, then we don't want to create a new entry in the tokens database.
// If we're refreshing an existing token, then we don't want to create a new
// entry in the tokens database.
// We only want to store tokens in the tokens database when they are created.
String tokenDescription = requestPayload.getDescription();
if (requestPayload.getRefreshToken() == null && tokenDescription != null) {
Expand All @@ -156,7 +196,8 @@ public HttpServletResponse handlePostRequest(String pathInfo, QueryParameters qu
throw new InternalServletException(error, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
}

return getResponseBuilder().buildResponse(request, response, "application/json", gson.toJson(responseJson), HttpServletResponse.SC_OK);
return getResponseBuilder().buildResponse(request, response, "application/json", gson.toJson(responseJson),
HttpServletResponse.SC_OK);
}

/**
Expand Down Expand Up @@ -202,7 +243,8 @@ private JsonObject sendTokenPost(TokenPayload requestBodyJson)
if (refreshToken != null) {
tokenResponse = oidcProvider.sendTokenPost(clientId, clientSecret, refreshToken);
} else {
tokenResponse = oidcProvider.sendTokenPost(clientId, clientSecret, requestBodyJson.getCode(), AuthCallbackRoute.getExternalAuthCallbackUrl());
tokenResponse = oidcProvider.sendTokenPost(clientId, clientSecret, requestBodyJson.getCode(),
AuthCallbackRoute.getExternalAuthCallbackUrl());
}

if (tokenResponse != null) {
Expand All @@ -215,8 +257,9 @@ private JsonObject sendTokenPost(TokenPayload requestBodyJson)
/**
* Records a new Galasa token in the auth store.
*
* @param clientId the ID of the client that a user has authenticated with
* @param jwt the JWT that was returned after authenticating with the client, identifying the user
* @param clientId the ID of the client that a user has authenticated with
* @param jwt the JWT that was returned after authenticating with the
* client, identifying the user
* @param description the description of the Galasa token provided by the user
* @throws InternalServletException
*/
Expand All @@ -233,4 +276,31 @@ private void addTokenToAuthStore(String clientId, String jwt, String description
}
logger.info("Stored token record in the auth store OK");
}

private List<AuthToken> convertAuthStoreTokenIntoTokenBeans(List<IInternalAuthToken> authStoreTokens) {

List<AuthToken> tokensToReturn = new ArrayList<>();

for (IInternalAuthToken token : authStoreTokens) {

User user = new User(token.getOwner().getLoginId());
tokensToReturn.add(new AuthToken(
token.getTokenId(),
token.getDescription(),
token.getCreationTime(),
user));
}

return tokensToReturn;

}

private void validateLoginId(String loginId, String servletPath) throws InternalServletException {

if (loginId == null || loginId.trim().length() == 0) {
ServletError error = new ServletError(GAL5067_ERROR_INVALID_LOGINID, servletPath);
throw new InternalServletException(error, HttpServletResponse.SC_BAD_REQUEST);
}

}
}
Loading