use of org.apache.knox.gateway.services.security.token.UnknownTokenException in project knox by apache.
the class JDBCTokenStateService method getTokenMetadata.
@Override
public TokenMetadata getTokenMetadata(String tokenId) throws UnknownTokenException {
// To support HA, there is no in-memory lookup here; we should go directly to the DB.
// See KNOX-2658 for more details.
TokenMetadata tokenMetadata = null;
try {
tokenMetadata = tokenDatabase.getTokenMetadata(tokenId);
if (tokenMetadata != null) {
log.fetchedMetadataFromDatabase(Tokens.getTokenIDDisplayText(tokenId));
// Update the in-memory cache to avoid subsequent DB look-ups for the same state
super.addMetadata(tokenId, tokenMetadata);
} else {
throw new UnknownTokenException(tokenId);
}
} catch (SQLException e) {
log.errorFetchingMetadataFromDatabase(Tokens.getTokenIDDisplayText(tokenId), e.getMessage(), e);
}
return tokenMetadata;
}
use of org.apache.knox.gateway.services.security.token.UnknownTokenException in project knox by apache.
the class JournalBasedTokenStateService method getTokenExpiration.
@Override
public long getTokenExpiration(final String tokenId, boolean validate) throws UnknownTokenException {
// Check the in-memory collection first, to avoid file access when possible
try {
// return the expiration from there.
return super.getTokenExpiration(tokenId, validate);
} catch (UnknownTokenException e) {
// It's not in memory
}
if (validate) {
validateToken(tokenId);
}
// If there is no associated state in the in-memory cache, proceed to check the journal
long expiration = 0;
try {
JournalEntry entry = journal.get(tokenId);
if (entry == null) {
throw new UnknownTokenException(tokenId);
}
expiration = Long.parseLong(entry.getExpiration());
super.addToken(tokenId, Long.parseLong(entry.getIssueTime()), expiration, Long.parseLong(entry.getMaxLifetime()));
} catch (IOException e) {
log.failedToLoadJournalEntry(e);
}
return expiration;
}
use of org.apache.knox.gateway.services.security.token.UnknownTokenException in project knox by apache.
the class JournalBasedTokenStateService method getMaxLifetime.
@Override
protected long getMaxLifetime(final String tokenId) {
long result = super.getMaxLifetime(tokenId);
// If there is no result from the in-memory collection, proceed to check the journal
if (result < 1L) {
try {
JournalEntry entry = journal.get(tokenId);
if (entry == null) {
throw new UnknownTokenException(tokenId);
}
result = Long.parseLong(entry.getMaxLifetime());
super.setMaxLifetime(tokenId, Long.parseLong(entry.getIssueTime()), result);
} catch (Exception e) {
log.failedToLoadJournalEntry(e);
}
}
return result;
}
use of org.apache.knox.gateway.services.security.token.UnknownTokenException in project knox by apache.
the class DefaultTokenStateServiceTest method testAddTokenMetadata.
@SuppressWarnings("PMD.JUnitUseExpected")
@Test
public void testAddTokenMetadata() throws Exception {
final JWT token = getJWTToken(System.currentTimeMillis());
final String tokenId = token.getClaim(JWTToken.KNOX_ID_CLAIM);
final TokenStateService tss = new DefaultTokenStateService();
tss.addToken((JWTToken) token, System.currentTimeMillis());
try {
tss.getTokenMetadata(tokenId);
fail("Expected exception since there is no metadata for the token ID.");
} catch (UnknownTokenException e) {
// Expected
}
final String userName = "testUser";
tss.addMetadata(token.getClaim(JWTToken.KNOX_ID_CLAIM), new TokenMetadata(userName));
assertNotNull(tss.getTokenMetadata(tokenId));
assertEquals(tss.getTokenMetadata(tokenId).getUserName(), userName);
assertNull(tss.getTokenMetadata(tokenId).getComment());
final String comment = "this is my test comment";
tss.addMetadata(token.getClaim(JWTToken.KNOX_ID_CLAIM), new TokenMetadata(userName, comment, true));
assertNotNull(tss.getTokenMetadata(tokenId));
assertEquals(tss.getTokenMetadata(tokenId).getComment(), comment);
assertTrue(tss.getTokenMetadata(tokenId).isEnabled());
final String passcode = "myPasscode";
final TokenMetadata metadata = new TokenMetadata(userName, comment, true);
metadata.setPasscode(passcode);
tss.addMetadata(token.getClaim(JWTToken.KNOX_ID_CLAIM), metadata);
assertNotNull(tss.getTokenMetadata(tokenId));
assertEquals(tss.getTokenMetadata(tokenId).getPasscode(), passcode);
}
use of org.apache.knox.gateway.services.security.token.UnknownTokenException in project knox by apache.
the class TokenResource method setTokenEnabledFlag.
private Response setTokenEnabledFlag(String tokenId, boolean enabled) {
String error = "";
ErrorCode errorCode = ErrorCode.UNKNOWN;
if (tokenStateService == null) {
error = "Unable to " + (enabled ? "enable" : "disable") + " tokens because token management is not configured";
errorCode = ErrorCode.CONFIGURATION_ERROR;
} else {
try {
final TokenMetadata tokenMetadata = tokenStateService.getTokenMetadata(tokenId);
if (enabled && tokenMetadata.isEnabled()) {
error = "Token is already enabled";
errorCode = ErrorCode.ALREADY_ENABLED;
} else if (!enabled && !tokenMetadata.isEnabled()) {
error = "Token is already disabled";
errorCode = ErrorCode.ALREADY_DISABLED;
} else {
tokenMetadata.setEnabled(enabled);
tokenStateService.addMetadata(tokenId, tokenMetadata);
}
} catch (UnknownTokenException e) {
error = safeGetMessage(e);
errorCode = ErrorCode.UNKNOWN_TOKEN;
}
}
if (error.isEmpty()) {
return Response.status(Response.Status.OK).entity("{\n \"setEnabledFlag\": \"true\",\n \"isEnabled\": \"" + enabled + "\"\n}\n").build();
} else {
log.badSetEnabledFlagRequest(getTopologyName(), Tokens.getTokenIDDisplayText(tokenId), error);
return Response.status(Response.Status.BAD_REQUEST).entity("{\n \"setEnabledFlag\": \"false\",\n \"error\": \"" + error + "\",\n \"code\": " + errorCode.toInt() + "\n}\n").build();
}
}
Aggregations