Search in sources :

Example 1 with KeycloakUriInfo

use of org.keycloak.models.KeycloakUriInfo 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 2 with KeycloakUriInfo

use of org.keycloak.models.KeycloakUriInfo 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 3 with KeycloakUriInfo

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

the class NoCookieFlowRedirectAuthenticator method authenticate.

@Override
public void authenticate(AuthenticationFlowContext context) {
    HttpRequest httpRequest = context.getHttpRequest();
    // only do redirects for GET requests
    if (HttpMethod.GET.equalsIgnoreCase(httpRequest.getHttpMethod())) {
        KeycloakUriInfo uriInfo = context.getSession().getContext().getUri();
        if (!uriInfo.getQueryParameters().containsKey(LoginActionsService.AUTH_SESSION_ID)) {
            Response response = Response.status(302).header(HttpHeaders.LOCATION, context.getRefreshUrl(true)).build();
            context.challenge(response);
            return;
        }
    }
    context.success();
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) Response(javax.ws.rs.core.Response) KeycloakUriInfo(org.keycloak.models.KeycloakUriInfo)

Example 4 with KeycloakUriInfo

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

the class FreeMarkerEmailTemplateProvider method addLinkInfoIntoAttributes.

/**
 * Add link info into template attributes.
 *
 * @param link to add
 * @param expirationInMinutes to add
 * @param attributes to add link info into
 */
protected void addLinkInfoIntoAttributes(String link, long expirationInMinutes, Map<String, Object> attributes) throws EmailException {
    attributes.put("link", link);
    attributes.put("linkExpiration", expirationInMinutes);
    KeycloakUriInfo uriInfo = session.getContext().getUri();
    URI baseUri = uriInfo.getBaseUri();
    try {
        Locale locale = session.getContext().resolveLocale(user);
        attributes.put("linkExpirationFormatter", new LinkExpirationFormatterMethod(getTheme().getMessages(locale), locale));
        attributes.put("url", new UrlBean(realm, getTheme(), baseUri, null));
    } catch (IOException e) {
        throw new EmailException("Failed to template email", e);
    }
}
Also used : Locale(java.util.Locale) KeycloakUriInfo(org.keycloak.models.KeycloakUriInfo) UrlBean(org.keycloak.forms.login.freemarker.model.UrlBean) EmailException(org.keycloak.email.EmailException) LinkExpirationFormatterMethod(org.keycloak.theme.beans.LinkExpirationFormatterMethod) IOException(java.io.IOException) URI(java.net.URI)

Example 5 with KeycloakUriInfo

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

the class DefaultKeycloakContext method getUri.

@Override
public KeycloakUriInfo getUri(UrlType type) {
    if (uriInfo == null || !uriInfo.containsKey(type)) {
        if (uriInfo == null) {
            uriInfo = new HashMap<>();
        }
        UriInfo originalUriInfo = getContextObject(UriInfo.class);
        uriInfo.put(type, new KeycloakUriInfo(session, type, originalUriInfo));
    }
    return uriInfo.get(type);
}
Also used : KeycloakUriInfo(org.keycloak.models.KeycloakUriInfo) KeycloakUriInfo(org.keycloak.models.KeycloakUriInfo) UriInfo(javax.ws.rs.core.UriInfo)

Aggregations

KeycloakUriInfo (org.keycloak.models.KeycloakUriInfo)6 RealmModel (org.keycloak.models.RealmModel)3 URI (java.net.URI)2 UriBuilder (javax.ws.rs.core.UriBuilder)2 KeycloakContext (org.keycloak.models.KeycloakContext)2 OAuth2DeviceTokenStoreProvider (org.keycloak.models.OAuth2DeviceTokenStoreProvider)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Locale (java.util.Locale)1 Set (java.util.Set)1 Response (javax.ws.rs.core.Response)1 UriInfo (javax.ws.rs.core.UriInfo)1 HttpRequest (org.jboss.resteasy.spi.HttpRequest)1 EmailException (org.keycloak.email.EmailException)1 UrlBean (org.keycloak.forms.login.freemarker.model.UrlBean)1 LinkExpirationFormatterMethod (org.keycloak.theme.beans.LinkExpirationFormatterMethod)1