Skip to content

jans-auth-server: Enhance ROPC script to throw custom error and error_description #14148

@ayushjain0702

Description

@ayushjain0702

We are running custom ROPC scripts and at the moment, there is no way of telling the end user, why the script failed and tell them a bit more about that.

For example, we want to create custom error and error_description, conforming to OIDC specs and use following sample in our ROPC script:

exception = context.createWebApplicationException(503,'{"error": "access_denied", "error_description": "User is blacklisted"}')
context.setWebApplicationException(exception)

This should send the below response to the end user:
{"error": "access_denied", "error_description": "User is blacklisted"}
with HTTP status code as 503.

For this, we would have to enhance the ExternalResourceOwnerPasswordCredentialsService.java file to have some better Exception handling. At the moment, it does not do anything about the exception, but just saves it in the Script configurations.

https://github.com/JanssenProject/jans/blob/main/jans-auth-server/server/src/main/java/io/jans/as/server/service/external/ExternalResourceOwnerPasswordCredentialsService.java#L71

        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            saveScriptError(customScriptConfiguration.getCustomScript(), ex);
            return false;
        }

We would like to change it to this:

       } catch (WebApplicationException e) {
            if (log.isTraceEnabled()) {
                log.trace("WebApplicationException from script", e);
            }
            throw e;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            saveScriptError(context.getScript().getCustomScript(), ex);
            throw new WebApplicationException(errorResponseFactory
                    .newErrorResponse(Response.Status.INTERNAL_SERVER_ERROR)
                    .entity(errorResponseFactory.getErrorAsJson(TokenErrorResponseType.ACCESS_DENIED, "", "Unable to run 'executeExternalAuthenticate' method in ROPC script."))
                    .build());
        }

In this way, if we are throwing custom WebApplicationException with proper error and error_description then, it will be thrown as it is. Else, if there is any other type of exception, then it shall be converted to WebApplicationException and the error user shall be shown:

{"error":"access_denied","error_description":"The resource owner or authorization server denied the request.","reason":"Unable to run 'executeExternalAuthenticate' method in ROPC script."}

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions