Search in sources :

Example 1 with CodeToTokenStoreProvider

use of org.keycloak.models.CodeToTokenStoreProvider in project keycloak by keycloak.

the class OAuth2CodeParser method parseCode.

/**
 * Will parse the code and retrieve the corresponding OAuth2Code and AuthenticatedClientSessionModel. Will also check if code wasn't already
 * used and if it wasn't expired. If it was already used (or other error happened during parsing), then returned parser will have "isIllegalHash"
 * set to true. If it was expired, the parser will have "isExpired" set to true
 *
 * @param session
 * @param code
 * @param realm
 * @param event
 * @return
 */
public static ParseResult parseCode(KeycloakSession session, String code, RealmModel realm, EventBuilder event) {
    ParseResult result = new ParseResult(code);
    String[] parsed = DOT.split(code, 3);
    if (parsed.length < 3) {
        logger.warn("Invalid format of the code");
        return result.illegalCode();
    }
    String userSessionId = parsed[1];
    String clientUUID = parsed[2];
    event.detail(Details.CODE_ID, userSessionId);
    event.session(userSessionId);
    // Parse UUID
    UUID codeUUID;
    try {
        codeUUID = UUID.fromString(parsed[0]);
    } catch (IllegalArgumentException re) {
        logger.warn("Invalid format of the UUID in the code");
        return result.illegalCode();
    }
    // Retrieve UserSession
    UserSessionModel userSession = new UserSessionCrossDCManager(session).getUserSessionWithClient(realm, userSessionId, clientUUID);
    if (userSession == null) {
        // Needed to track if code is invalid or was already used.
        userSession = session.sessions().getUserSession(realm, userSessionId);
        if (userSession == null) {
            return result.illegalCode();
        }
    }
    result.clientSession = userSession.getAuthenticatedClientSessionByClient(clientUUID);
    CodeToTokenStoreProvider codeStore = session.getProvider(CodeToTokenStoreProvider.class);
    Map<String, String> codeData = codeStore.remove(codeUUID);
    // Either code not available or was already used
    if (codeData == null) {
        logger.warnf("Code '%s' already used for userSession '%s' and client '%s'.", codeUUID, userSessionId, clientUUID);
        return result.illegalCode();
    }
    logger.tracef("Successfully verified code '%s'. User session: '%s', client: '%s'", codeUUID, userSessionId, clientUUID);
    result.codeData = OAuth2Code.deserializeCode(codeData);
    // Finally doublecheck if code is not expired
    int currentTime = Time.currentTime();
    if (currentTime > result.codeData.getExpiration()) {
        return result.expiredCode();
    }
    return result;
}
Also used : UserSessionModel(org.keycloak.models.UserSessionModel) CodeToTokenStoreProvider(org.keycloak.models.CodeToTokenStoreProvider) UUID(java.util.UUID) UserSessionCrossDCManager(org.keycloak.services.managers.UserSessionCrossDCManager)

Example 2 with CodeToTokenStoreProvider

use of org.keycloak.models.CodeToTokenStoreProvider in project keycloak by keycloak.

the class OAuth2CodeParser method persistCode.

/**
 * Will persist the code to the cache and return the object with the codeData and code correctly set
 *
 * @param session
 * @param clientSession
 * @param codeData
 * @return code parameter to be used in OAuth2 handshake
 */
public static String persistCode(KeycloakSession session, AuthenticatedClientSessionModel clientSession, OAuth2Code codeData) {
    CodeToTokenStoreProvider codeStore = session.getProvider(CodeToTokenStoreProvider.class);
    UUID key = codeData.getId();
    if (key == null) {
        throw new IllegalStateException("ID not present in the data");
    }
    Map<String, String> serialized = codeData.serializeCode();
    codeStore.put(key, clientSession.getUserSession().getRealm().getAccessCodeLifespan(), serialized);
    return key.toString() + "." + clientSession.getUserSession().getId() + "." + clientSession.getClient().getId();
}
Also used : CodeToTokenStoreProvider(org.keycloak.models.CodeToTokenStoreProvider) UUID(java.util.UUID)

Aggregations

UUID (java.util.UUID)2 CodeToTokenStoreProvider (org.keycloak.models.CodeToTokenStoreProvider)2 UserSessionModel (org.keycloak.models.UserSessionModel)1 UserSessionCrossDCManager (org.keycloak.services.managers.UserSessionCrossDCManager)1