Search in sources :

Example 6 with UnknownTokenException

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;
}
Also used : SQLException(java.sql.SQLException) UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) TokenMetadata(org.apache.knox.gateway.services.security.token.TokenMetadata)

Example 7 with UnknownTokenException

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;
}
Also used : UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) IOException(java.io.IOException) JournalEntry(org.apache.knox.gateway.services.token.state.JournalEntry)

Example 8 with UnknownTokenException

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;
}
Also used : UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) JournalEntry(org.apache.knox.gateway.services.token.state.JournalEntry) UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) IOException(java.io.IOException)

Example 9 with UnknownTokenException

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);
}
Also used : JWT(org.apache.knox.gateway.services.security.token.impl.JWT) UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) TokenMetadata(org.apache.knox.gateway.services.security.token.TokenMetadata) Test(org.junit.Test)

Example 10 with UnknownTokenException

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();
    }
}
Also used : UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) TokenMetadata(org.apache.knox.gateway.services.security.token.TokenMetadata)

Aggregations

UnknownTokenException (org.apache.knox.gateway.services.security.token.UnknownTokenException)15 ParseException (java.text.ParseException)5 JWTToken (org.apache.knox.gateway.services.security.token.impl.JWTToken)5 IOException (java.io.IOException)4 Subject (javax.security.auth.Subject)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 TokenMetadata (org.apache.knox.gateway.services.security.token.TokenMetadata)4 TokenStateService (org.apache.knox.gateway.services.security.token.TokenStateService)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 JWT (org.apache.knox.gateway.services.security.token.impl.JWT)3 ServletException (javax.servlet.ServletException)2 PrimaryPrincipal (org.apache.knox.gateway.security.PrimaryPrincipal)2 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)2 JournalEntry (org.apache.knox.gateway.services.token.state.JournalEntry)2 Test (org.junit.Test)2 ManagementFactory (java.lang.management.ManagementFactory)1 SQLException (java.sql.SQLException)1 Instant (java.time.Instant)1 DateTimeParseException (java.time.format.DateTimeParseException)1 Collection (java.util.Collection)1