How to not send HTML error messages from Silhouette

#1

In my test case, my custom error handler is not getting called.

My controller is defined as follows

    
    silhouette.env.authenticatorService.discard(request.authenticator, Ok(Json.toJson(JsonResultSuccess(messagesApi("success.signoutSuccessful")(langs.availables(0))))))
  }```

If my request is not authenticated, I get error in HTML format

silhouette.not.authenticatedsilhouette.not.authenticated ```

I want that my application should send all the messages in JSON format. Can I change Silhouette’s behavior and send errors in JSON format?

I have extended the error handling class but I don’t know why I still got error in HTML



    /**
      * Called when a user is not authenticated.
      *
      * As defined by RFC 2616, the status code of the response should be 401 Unauthorized.
      *
      * @param request The request header.
      * @return The result to send to the client.
      */
    override def onNotAuthenticated(implicit request: RequestHeader) = {
      Future.successful(Unauthorized(Json.toJson(JsonResultError("You need to sign in first"))))

    }

  /**
      * Called when a user is authenticated but not authorized.
      *
      * As defined by RFC 2616, the status code of the response should be 403 Forbidden.
      *
      * @param request The request header.
      * @return The result to send to the client.
      */
  override def onNotAuthorized(implicit request: RequestHeader) = {
    Future.successful(Forbidden(Json.toJson(JsonResultError("You do not have permission for the operation"))))
  }
}


The code works if I specify the error handler per endpoint like the following but I am not able to set a global error handler

def signOutUser = silhouette.SecuredAction(new CustomSilhouetteSecuredErrorHandler()).async {...}
0 Likes

#2

my bad. I didn’t create Action handlers correctly in the unit test. This works

val securedAction = new DefaultSecuredAction(new DefaultSecuredRequestHandler(new CustomSilhouetteSecuredErrorHandler), defaultParser)
  val unsecuredAction = new DefaultUnsecuredAction(new DefaultUnsecuredRequestHandler(new CustomSilhouetteUnsecuredErrorHandler), defaultParser)
  val userAware = new DefaultUserAwareAction(new DefaultUserAwareRequestHandler(), defaultParser)
  val fakeSilhouette = new SilhouetteProvider[JWTEnv](fakeEnv, securedAction, unsecuredAction, userAware)

0 Likes