Search in sources :

Example 1 with KnoxToken

use of org.apache.knox.gateway.services.security.token.KnoxToken in project knox by apache.

the class DefaultTokenStateService method getTokens.

@Override
public Collection<KnoxToken> getTokens(String userName) {
    final Collection<KnoxToken> tokens = new TreeSet<>();
    metadataMap.entrySet().stream().filter(entry -> entry.getValue().getUserName().equals(userName)).forEach(metadata -> {
        String tokenId = metadata.getKey();
        try {
            tokens.add(new KnoxToken(tokenId, getTokenIssueTime(tokenId), getTokenExpiration(tokenId), getMaxLifetime(tokenId), metadata.getValue()));
        } catch (UnknownTokenException e) {
        // NOP: since this is coming from memory the only reason an UTE is thrown that the token got removed/revoked.
        // In that case we would not want to return it anyway
        }
    });
    return tokens;
}
Also used : KnoxToken(org.apache.knox.gateway.services.security.token.KnoxToken) MessagesFactory(org.apache.knox.gateway.i18n.messages.MessagesFactory) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) JWT(org.apache.knox.gateway.services.security.token.impl.JWT) TokenMetadata(org.apache.knox.gateway.services.security.token.TokenMetadata) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) JWTToken(org.apache.knox.gateway.services.security.token.impl.JWTToken) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) Tokens(org.apache.knox.gateway.util.Tokens) UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MBeanRegistrationException(javax.management.MBeanRegistrationException) ManagementFactory(java.lang.management.ManagementFactory) TokenStateServiceStatistics(org.apache.knox.gateway.services.token.TokenStateServiceStatistics) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ObjectName(javax.management.ObjectName) Instant(java.time.Instant) TokenUtils(org.apache.knox.gateway.services.security.token.TokenUtils) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) TokenStateService(org.apache.knox.gateway.services.security.token.TokenStateService) TimeUnit(java.util.concurrent.TimeUnit) MalformedObjectNameException(javax.management.MalformedObjectNameException) List(java.util.List) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) Collections(java.util.Collections) TreeSet(java.util.TreeSet) KnoxToken(org.apache.knox.gateway.services.security.token.KnoxToken) UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException)

Example 2 with KnoxToken

use of org.apache.knox.gateway.services.security.token.KnoxToken in project knox by apache.

the class TokenStateDatabase method getTokens.

Collection<KnoxToken> getTokens(String userName) throws SQLException {
    Map<String, KnoxToken> tokenMap = new LinkedHashMap<>();
    try (Connection connection = dataSource.getConnection();
        PreparedStatement getTokenIdsStatement = connection.prepareStatement(GET_TOKENS_BY_USER_NAME_SQL)) {
        getTokenIdsStatement.setString(1, userName);
        try (ResultSet rs = getTokenIdsStatement.executeQuery()) {
            while (rs.next()) {
                String tokenId = rs.getString(1);
                long issueTime = rs.getLong(2);
                long expiration = rs.getLong(3);
                long maxLifeTime = rs.getLong(4);
                String metaName = rs.getString(5);
                String metaValue = rs.getString(6);
                KnoxToken token = tokenMap.computeIfAbsent(tokenId, id -> new KnoxToken(tokenId, issueTime, expiration, maxLifeTime));
                token.addMetadata(metaName, decodeMetadata(metaName, metaValue));
            }
            return tokenMap.values();
        }
    }
}
Also used : KnoxToken(org.apache.knox.gateway.services.security.token.KnoxToken) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

KnoxToken (org.apache.knox.gateway.services.security.token.KnoxToken)2 ManagementFactory (java.lang.management.ManagementFactory)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Instant (java.time.Instant)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)1