Search in sources :

Example 1 with DeviceAuthorizationCacheControl

use of org.gluu.oxauth.model.common.DeviceAuthorizationCacheControl in project oxAuth by GluuFederation.

the class DeviceAuthorizationAction method processUserCodeVerification.

/**
 * Processes user code introduced or loaded in the veritification page and redirects whether user code is correct
 * or return an error if there is something wrong.
 */
public void processUserCodeVerification() {
    SessionId session = sessionIdService.getSessionId();
    if (session == null) {
        facesMessages.add(FacesMessage.SEVERITY_WARN, languageBean.getMessage("error.errorEncountered"));
        return;
    }
    if (!preventBruteForcing(session)) {
        facesMessages.add(FacesMessage.SEVERITY_WARN, languageBean.getMessage("device.authorization.brute.forcing.msg"));
        return;
    }
    String userCode;
    if (StringUtils.isBlank(userCodePart1) && StringUtils.isBlank(userCodePart2)) {
        userCode = session.getSessionAttributes().get(SESSION_USER_CODE);
    } else {
        userCode = userCodePart1 + '-' + userCodePart2;
    }
    userCode = userCode.toUpperCase();
    if (!validateFormat(userCode)) {
        facesMessages.add(FacesMessage.SEVERITY_WARN, languageBean.getMessage("device.authorization.invalid.user.code"));
        return;
    }
    DeviceAuthorizationCacheControl cacheData = deviceAuthorizationService.getDeviceAuthzByUserCode(userCode);
    log.debug("Verifying device authorization cache data: {}", cacheData);
    String message = null;
    if (cacheData != null) {
        if (cacheData.getStatus() == DeviceAuthorizationStatus.PENDING) {
            session.getSessionAttributes().put(SESSION_USER_CODE, userCode);
            session.getSessionAttributes().remove(SESSION_LAST_ATTEMPT);
            session.getSessionAttributes().remove(SESSION_ATTEMPTS);
            sessionIdService.updateSessionId(session);
            redirectToAuthorization(cacheData);
        } else if (cacheData.getStatus() == DeviceAuthorizationStatus.DENIED) {
            message = languageBean.getMessage("device.authorization.access.denied.msg");
        } else {
            message = languageBean.getMessage("device.authorization.expired.code.msg");
        }
    } else {
        message = languageBean.getMessage("device.authorization.invalid.user.code");
    }
    if (message != null) {
        facesMessages.add(FacesMessage.SEVERITY_WARN, message);
    }
}
Also used : DeviceAuthorizationCacheControl(org.gluu.oxauth.model.common.DeviceAuthorizationCacheControl) SessionId(org.gluu.oxauth.model.common.SessionId)

Example 2 with DeviceAuthorizationCacheControl

use of org.gluu.oxauth.model.common.DeviceAuthorizationCacheControl in project oxAuth by GluuFederation.

the class DeviceAuthorizationRestWebServiceImpl method deviceAuthorization.

@Override
public Response deviceAuthorization(String clientId, String scope, HttpServletRequest httpRequest, HttpServletResponse httpResponse, SecurityContext securityContext) {
    // it may be encoded
    scope = ServerUtil.urlDecode(scope);
    OAuth2AuditLog oAuth2AuditLog = new OAuth2AuditLog(ServerUtil.getIpAddress(httpRequest), Action.DEVICE_CODE_AUTHORIZATION);
    oAuth2AuditLog.setClientId(clientId);
    oAuth2AuditLog.setScope(scope);
    try {
        log.debug("Attempting to request device codes: clientId = {}, scope = {}", clientId, scope);
        SessionClient sessionClient = identity.getSessionClient();
        Client client = sessionClient != null ? sessionClient.getClient() : null;
        if (client == null) {
            client = clientService.getClient(clientId);
            if (!clientService.isPublic(client)) {
                log.trace("Client is not public and not authenticated. Skip device authorization, clientId: {}", clientId);
                throw errorResponseFactory.createWebApplicationException(Response.Status.UNAUTHORIZED, INVALID_CLIENT, "");
            }
        }
        if (client == null) {
            log.trace("Client is not unknown. Skip revoking.");
            throw errorResponseFactory.createWebApplicationException(Response.Status.UNAUTHORIZED, INVALID_CLIENT, "");
        }
        if (!deviceAuthorizationService.hasDeviceCodeCompatibility(client)) {
            throw errorResponseFactory.createWebApplicationException(Response.Status.BAD_REQUEST, INVALID_GRANT, "");
        }
        List<String> scopes = new ArrayList<>();
        if (StringHelper.isNotEmpty(scope)) {
            Set<String> grantedScopes = scopeChecker.checkScopesPolicy(client, scope);
            scopes.addAll(grantedScopes);
        }
        // Entropy 20^8 which is suggested in the RFC8628 section 6.1
        String userCode = StringUtils.generateRandomReadableCode((byte) 8);
        // Entropy 160 bits which is over userCode entropy based on RFC8628 section 5.2
        String deviceCode = StringUtils.generateRandomCode((byte) 24);
        URI verificationUri = UriBuilder.fromUri(appConfiguration.getIssuer()).path("device-code").build();
        int expiresIn = appConfiguration.getDeviceAuthzRequestExpiresIn();
        int interval = appConfiguration.getDeviceAuthzTokenPollInterval();
        long lastAccess = System.currentTimeMillis();
        DeviceAuthorizationStatus status = DeviceAuthorizationStatus.PENDING;
        DeviceAuthorizationCacheControl deviceAuthorizationCacheControl = new DeviceAuthorizationCacheControl(userCode, deviceCode, client, scopes, verificationUri, expiresIn, interval, lastAccess, status);
        deviceAuthorizationService.saveInCache(deviceAuthorizationCacheControl, true, true);
        log.info("Device authorization flow initiated, userCode: {}, deviceCode: {}, clientId: {}, verificationUri: {}, expiresIn: {}, interval: {}", userCode, deviceCode, clientId, verificationUri, expiresIn, interval);
        applicationAuditLogger.sendMessage(oAuth2AuditLog);
        return Response.ok().entity(getResponseJSONObject(deviceAuthorizationCacheControl).toString(4).replace("\\/", "/")).type(MediaType.APPLICATION_JSON_TYPE).build();
    } catch (WebApplicationException wae) {
        throw wae;
    } catch (Exception e) {
        log.error("Problems processing device authorization init flow, clientId: {}, scope: {}", clientId, scope, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : DeviceAuthorizationCacheControl(org.gluu.oxauth.model.common.DeviceAuthorizationCacheControl) WebApplicationException(javax.ws.rs.WebApplicationException) SessionClient(org.gluu.oxauth.model.session.SessionClient) OAuth2AuditLog(org.gluu.oxauth.model.audit.OAuth2AuditLog) ArrayList(java.util.ArrayList) URI(java.net.URI) JSONException(org.json.JSONException) WebApplicationException(javax.ws.rs.WebApplicationException) DeviceAuthorizationStatus(org.gluu.oxauth.model.common.DeviceAuthorizationStatus) SessionClient(org.gluu.oxauth.model.session.SessionClient) Client(org.gluu.oxauth.model.registration.Client)

Aggregations

DeviceAuthorizationCacheControl (org.gluu.oxauth.model.common.DeviceAuthorizationCacheControl)2 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 OAuth2AuditLog (org.gluu.oxauth.model.audit.OAuth2AuditLog)1 DeviceAuthorizationStatus (org.gluu.oxauth.model.common.DeviceAuthorizationStatus)1 SessionId (org.gluu.oxauth.model.common.SessionId)1 Client (org.gluu.oxauth.model.registration.Client)1 SessionClient (org.gluu.oxauth.model.session.SessionClient)1 JSONException (org.json.JSONException)1