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);
}
}
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();
}
}
Aggregations