use of org.wso2.carbon.identity.oauth.endpoint.exception.TokenEndpointBadRequestException in project identity-inbound-auth-oauth by wso2-extensions.
the class EndpointUtil method parseJsonTokenRequest.
public static Map<String, List<String>> parseJsonTokenRequest(String jsonPayload) throws TokenEndpointBadRequestException {
JsonFactory factory = new JsonFactory();
Map<String, List<String>> requestParams = new HashMap<>();
try {
JsonParser parser = factory.createParser(jsonPayload);
// Skip the first START_OBJECT token. i.e the beginning of the payload: '{'.
parser.nextToken();
while (!parser.isClosed()) {
JsonToken currentToken = parser.nextToken();
if (currentToken == null) {
continue;
}
if (currentToken.isScalarValue()) {
// If the current token is a scalar value, add it to a map along with the corresponding json key.
String key = parser.currentName();
String value = parser.getValueAsString();
requestParams.computeIfAbsent(key, val -> new ArrayList<>()).add(value);
} else if (currentToken != JsonToken.FIELD_NAME && currentToken != JsonToken.END_OBJECT) {
// If the current token is a complex value (array or object), flatten the value and add it to map
// with the corresponding json key.
String key = parser.currentName();
String value = (new ObjectMapper()).readTree(parser).toString();
requestParams.computeIfAbsent(key, val -> new ArrayList<>()).add(value);
}
}
} catch (IOException e) {
throw new TokenEndpointBadRequestException("Malformed or unsupported request payload", e);
}
return requestParams;
}
use of org.wso2.carbon.identity.oauth.endpoint.exception.TokenEndpointBadRequestException in project identity-inbound-auth-oauth by wso2-extensions.
the class DeviceEndpoint method authorize.
@POST
@Path("/")
@Consumes("application/x-www-form-urlencoded")
@Produces("application/json")
public Response authorize(@Context HttpServletRequest request, MultivaluedMap<String, String> paramMap, @Context HttpServletResponse response) throws IdentityOAuth2Exception, OAuthSystemException {
OAuthClientAuthnContext oAuthClientAuthnContext = getValidationObject(request);
if (!oAuthClientAuthnContext.isAuthenticated()) {
return handleErrorResponse(oAuthClientAuthnContext);
}
try {
validateRepeatedParams(request, paramMap);
String deviceCode = UUID.randomUUID().toString();
String scopes = request.getParameter(Constants.SCOPE);
String userCode = getUniqueUserCode(deviceCode, oAuthClientAuthnContext.getClientId(), scopes);
String redirectionUri = ServiceURLBuilder.create().addPath(Constants.DEVICE_ENDPOINT_PATH).build().getAbsolutePublicURL();
String redirectionUriComplete = ServiceURLBuilder.create().addPath(Constants.DEVICE_ENDPOINT_PATH).addParameter("user_code", userCode).build().getAbsolutePublicURL();
return buildResponseObject(deviceCode, userCode, redirectionUri, redirectionUriComplete);
} catch (IdentityOAuth2Exception e) {
return handleIdentityOAuth2Exception(e);
} catch (TokenEndpointBadRequestException e) {
return handleTokenEndpointBadRequestException(e);
} catch (URLBuilderException e) {
return handleURLBuilderException(e);
}
}
use of org.wso2.carbon.identity.oauth.endpoint.exception.TokenEndpointBadRequestException in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuth2TokenEndpoint method issueAccessToken.
protected Response issueAccessToken(HttpServletRequest request, Map<String, List<String>> paramMap) throws OAuthSystemException, InvalidRequestParentException {
try {
startSuperTenantFlow();
validateRepeatedParams(request, paramMap);
HttpServletRequestWrapper httpRequest = new OAuthRequestWrapper(request, paramMap);
CarbonOAuthTokenRequest oauthRequest = buildCarbonOAuthTokenRequest(httpRequest);
validateOAuthApplication(oauthRequest.getoAuthClientAuthnContext());
OAuth2AccessTokenRespDTO oauth2AccessTokenResp = issueAccessToken(oauthRequest, httpRequest);
if (oauth2AccessTokenResp.getErrorMsg() != null) {
return handleErrorResponse(oauth2AccessTokenResp);
} else {
return buildTokenResponse(oauth2AccessTokenResp);
}
} catch (TokenEndpointBadRequestException | OAuthSystemException | InvalidApplicationClientException e) {
triggerOnTokenExceptionListeners(e, request, paramMap);
throw e;
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
Aggregations