Search in sources :

Example 1 with KeycloakContext

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

the class DeviceActivityManager method getDeviceFromUserAgent.

private static DeviceRepresentation getDeviceFromUserAgent(KeycloakSession session) {
    KeycloakContext context = session.getContext();
    if (context.getRequestHeaders() == null) {
        return null;
    }
    String userAgent = context.getRequestHeaders().getHeaderString(HttpHeaders.USER_AGENT);
    if (userAgent == null) {
        return null;
    }
    if (userAgent.length() > USER_AGENT_MAX_LENGTH) {
        logger.warn("Ignoring User-Agent header. Length is above the permitted: " + USER_AGENT_MAX_LENGTH);
        return null;
    }
    DeviceRepresentation current;
    try {
        Client client = UA_PARSER.parse(userAgent);
        current = new DeviceRepresentation();
        current.setDevice(client.device.family);
        String browserVersion = client.userAgent.major;
        if (client.userAgent.minor != null) {
            browserVersion += "." + client.userAgent.minor;
        }
        if (client.userAgent.patch != null) {
            browserVersion += "." + client.userAgent.patch;
        }
        if (browserVersion == null) {
            browserVersion = DeviceRepresentation.UNKNOWN;
        }
        current.setBrowser(client.userAgent.family, browserVersion);
        current.setOs(client.os.family);
        String osVersion = client.os.major;
        if (client.os.minor != null) {
            osVersion += "." + client.os.minor;
        }
        if (client.os.patch != null) {
            osVersion += "." + client.os.patch;
        }
        if (client.os.patchMinor != null) {
            osVersion += "." + client.os.patchMinor;
        }
        current.setOsVersion(osVersion);
        current.setIpAddress(context.getConnection().getRemoteAddr());
        current.setMobile(userAgent.toLowerCase().contains("mobile"));
    } catch (Exception cause) {
        logger.error("Failed to create device info from user agent header", cause);
        return null;
    }
    return current;
}
Also used : DeviceRepresentation(org.keycloak.representations.account.DeviceRepresentation) KeycloakContext(org.keycloak.models.KeycloakContext) Client(ua_parser.Client) IOException(java.io.IOException)

Example 2 with KeycloakContext

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

the class LocalDateValidator method doValidate.

@Override
protected void doValidate(String value, String inputHint, ValidationContext context, ValidatorConfig config) {
    UserModel user = (UserModel) context.getAttributes().get(UserModel.class.getName());
    KeycloakSession session = context.getSession();
    KeycloakContext keycloakContext = session.getContext();
    Locale locale = keycloakContext.resolveLocale(user);
    DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
    formatter.setLenient(false);
    try {
        formatter.parse(value);
    } catch (ParseException e) {
        context.addError(new ValidationError(ID, inputHint, MESSAGE_INVALID_DATE));
    }
}
Also used : UserModel(org.keycloak.models.UserModel) Locale(java.util.Locale) KeycloakContext(org.keycloak.models.KeycloakContext) KeycloakSession(org.keycloak.models.KeycloakSession) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) ValidationError(org.keycloak.validate.ValidationError)

Example 3 with KeycloakContext

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

the class DeviceGrantType method denyOAuth2DeviceAuthorization.

public static Response denyOAuth2DeviceAuthorization(AuthenticationSessionModel authSession, LoginProtocol.Error error, KeycloakSession session) {
    KeycloakContext context = session.getContext();
    RealmModel realm = context.getRealm();
    KeycloakUriInfo uri = context.getUri();
    UriBuilder uriBuilder = DeviceGrantType.oauth2DeviceVerificationCompletedUrl(uri);
    String errorType = OAuthErrorException.SERVER_ERROR;
    if (error == LoginProtocol.Error.CONSENT_DENIED) {
        String verifiedUserCode = authSession.getClientNote(DeviceGrantType.OAUTH2_DEVICE_VERIFIED_USER_CODE);
        OAuth2DeviceTokenStoreProvider store = session.getProvider(OAuth2DeviceTokenStoreProvider.class);
        if (!store.deny(realm, verifiedUserCode)) {
            // Already expired and removed in the store
            errorType = OAuthErrorException.EXPIRED_TOKEN;
        } else {
            errorType = OAuthErrorException.ACCESS_DENIED;
        }
    }
    return Response.status(302).location(uriBuilder.queryParam(OAuth2Constants.ERROR, errorType).build(realm.getName())).build();
}
Also used : RealmModel(org.keycloak.models.RealmModel) OAuth2DeviceTokenStoreProvider(org.keycloak.models.OAuth2DeviceTokenStoreProvider) KeycloakUriInfo(org.keycloak.models.KeycloakUriInfo) KeycloakContext(org.keycloak.models.KeycloakContext) UriBuilder(javax.ws.rs.core.UriBuilder)

Example 4 with KeycloakContext

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

the class DeviceGrantType method approveOAuth2DeviceAuthorization.

public static Response approveOAuth2DeviceAuthorization(AuthenticationSessionModel authSession, AuthenticatedClientSessionModel clientSession, KeycloakSession session) {
    KeycloakContext context = session.getContext();
    RealmModel realm = context.getRealm();
    KeycloakUriInfo uriInfo = context.getUri();
    UriBuilder uriBuilder = DeviceGrantType.oauth2DeviceVerificationCompletedUrl(uriInfo);
    String verifiedUserCode = authSession.getClientNote(DeviceGrantType.OAUTH2_DEVICE_VERIFIED_USER_CODE);
    String userSessionId = clientSession.getUserSession().getId();
    OAuth2DeviceTokenStoreProvider store = session.getProvider(OAuth2DeviceTokenStoreProvider.class);
    if (!store.approve(realm, verifiedUserCode, userSessionId, null)) {
        // Already expired and removed in the store
        return Response.status(302).location(uriBuilder.queryParam(OAuth2Constants.ERROR, OAuthErrorException.EXPIRED_TOKEN).build(realm.getName())).build();
    }
    // Now, remove the verified user code
    store.removeUserCode(realm, verifiedUserCode);
    return Response.status(302).location(uriBuilder.build(realm.getName())).build();
}
Also used : RealmModel(org.keycloak.models.RealmModel) OAuth2DeviceTokenStoreProvider(org.keycloak.models.OAuth2DeviceTokenStoreProvider) KeycloakUriInfo(org.keycloak.models.KeycloakUriInfo) KeycloakContext(org.keycloak.models.KeycloakContext) UriBuilder(javax.ws.rs.core.UriBuilder)

Example 5 with KeycloakContext

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

the class JBossLoggingEventListenerProvider method setKeycloakContext.

private void setKeycloakContext(StringBuilder sb) {
    KeycloakContext context = session.getContext();
    UriInfo uriInfo = context.getUri();
    HttpHeaders headers = context.getRequestHeaders();
    if (uriInfo != null) {
        sb.append(", requestUri=");
        sb.append(uriInfo.getRequestUri().toString());
    }
    if (headers != null) {
        sb.append(", cookies=[");
        boolean f = true;
        for (Map.Entry<String, Cookie> e : headers.getCookies().entrySet()) {
            if (f) {
                f = false;
            } else {
                sb.append(", ");
            }
            sb.append(e.getValue().toString());
        }
        sb.append("]");
    }
}
Also used : Cookie(javax.ws.rs.core.Cookie) HttpHeaders(javax.ws.rs.core.HttpHeaders) KeycloakContext(org.keycloak.models.KeycloakContext) Map(java.util.Map) UriInfo(javax.ws.rs.core.UriInfo)

Aggregations

KeycloakContext (org.keycloak.models.KeycloakContext)10 RealmModel (org.keycloak.models.RealmModel)7 KeycloakSession (org.keycloak.models.KeycloakSession)4 UserModel (org.keycloak.models.UserModel)3 UriBuilder (javax.ws.rs.core.UriBuilder)2 EventBuilder (org.keycloak.events.EventBuilder)2 KeycloakUriInfo (org.keycloak.models.KeycloakUriInfo)2 OAuth2DeviceTokenStoreProvider (org.keycloak.models.OAuth2DeviceTokenStoreProvider)2 IOException (java.io.IOException)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Cookie (javax.ws.rs.core.Cookie)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 UriInfo (javax.ws.rs.core.UriInfo)1 Test (org.junit.Test)1 ComponentModel (org.keycloak.component.ComponentModel)1 UserManager (org.keycloak.models.UserManager)1 DeviceRepresentation (org.keycloak.representations.account.DeviceRepresentation)1