Java Code Examples for org.keycloak.models.KeycloakSession#getContext()

The following examples show how to use org.keycloak.models.KeycloakSession#getContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlacklistPasswordPolicyProviderFactory.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public PasswordPolicyProvider create(KeycloakSession session) {
    if (this.blacklistsBasePath == null) {
        synchronized (this) {
            if (this.blacklistsBasePath == null) {
                this.blacklistsBasePath = FileBasedPasswordBlacklist.detectBlacklistsBasePath(config);
            }
        }
    }
    return new BlacklistPasswordPolicyProvider(session.getContext(), this);
}
 
Example 2
Source File: Tokens.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static AccessToken getAccessToken(KeycloakSession keycloakSession) {
    AppAuthManager authManager = new AppAuthManager();
    KeycloakContext context = keycloakSession.getContext();
    AuthResult authResult = authManager.authenticateBearerToken(keycloakSession, context.getRealm(), context.getUri(), context.getConnection(), context.getRequestHeaders());

    if (authResult != null) {
        return authResult.getToken();
    }

    return null;
}
 
Example 3
Source File: Tokens.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static AccessToken getAccessToken(String accessToken, KeycloakSession keycloakSession) {
    AppAuthManager authManager = new AppAuthManager();
    KeycloakContext context = keycloakSession.getContext();
    AuthResult authResult = authManager.authenticateBearerToken(accessToken, keycloakSession, context.getRealm(), context.getUri(), context.getConnection(), context.getRequestHeaders());

    if (authResult != null) {
        return authResult.getToken();
    }

    return null;
}
 
Example 4
Source File: DeviceActivityManager.java    From keycloak with Apache License 2.0 4 votes vote down vote up
private static DeviceRepresentation getDeviceFromUserAgent(KeycloakSession session) {
    KeycloakContext context = session.getContext();
    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;
}
 
Example 5
Source File: DigitsPasswordPolicyProviderFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public PasswordPolicyProvider create(KeycloakSession session) {
    return new DigitsPasswordPolicyProvider(session.getContext());
}
 
Example 6
Source File: UpperCasePasswordPolicyProviderFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public PasswordPolicyProvider create(KeycloakSession session) {
    return new UpperCasePasswordPolicyProvider(session.getContext());
}
 
Example 7
Source File: SpecialCharsPasswordPolicyProviderFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public PasswordPolicyProvider create(KeycloakSession session) {
    return new SpecialCharsPasswordPolicyProvider(session.getContext());
}
 
Example 8
Source File: NotUsernamePasswordPolicyProviderFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public PasswordPolicyProvider create(KeycloakSession session) {
    return new NotUsernamePasswordPolicyProvider(session.getContext());
}
 
Example 9
Source File: LengthPasswordPolicyProviderFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public PasswordPolicyProvider create(KeycloakSession session) {
    return new LengthPasswordPolicyProvider(session.getContext());
}
 
Example 10
Source File: RegexPatternsPasswordPolicyProviderFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public PasswordPolicyProvider create(KeycloakSession session) {
    return new RegexPatternsPasswordPolicyProvider(session.getContext());
}
 
Example 11
Source File: LowerCasePasswordPolicyProviderFactory.java    From keycloak with Apache License 2.0 4 votes vote down vote up
@Override
public PasswordPolicyProvider create(KeycloakSession session) {
    return new LowerCasePasswordPolicyProvider(session.getContext());
}
 
Example 12
Source File: AppAuthManager.java    From keycloak with Apache License 2.0 4 votes vote down vote up
public AuthResult authenticateBearerToken(KeycloakSession session, RealmModel realm) {
    KeycloakContext ctx = session.getContext();
    return authenticateBearerToken(session, realm, ctx.getUri(), ctx.getConnection(), ctx.getRequestHeaders());
}