# Modifying the default RestStatus code in Custom Realm auth response

**URL:** <https://discuss.elastic.co/t/modifying-the-default-reststatus-code-in-custom-realm-auth-response/207436>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-security\
**Created:** [November 12, 2019, 4:14am UTC](https://discuss.elastic.co/t/modifying-the-default-reststatus-code-in-custom-realm-auth-response/207436 "2019-11-12T04:14:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gautu7](https://avatars.discourse-cdn.com/v4/letter/g/c89c15/32.png) [@gautu7](https://discuss.elastic.co/u/gautu7)\
**Post date:** [November 12, 2019, 4:14am UTC](https://discuss.elastic.co/t/modifying-the-default-reststatus-code-in-custom-realm-auth-response/207436/1 "2019-11-12T04:14:52Z")

</div>

Hi,

I have a custom Realm authorization plugin which validates with an external endpoint, when there is a failure I want to be able to return specific error codes back to the client calling the Elasticsearch endpoint. For example, if there was an Internal Server Error within the plugin, I want to return status code 500 in the response. Currently, when the `AuthenticationToken` object is not set in the extended Realm's `token` method irrespective of the type of error, Elasticsearch responds with 401Unauthorized to the calling client. I tried extending the `DefaultAuthenticationFailureHandler` but this seems to add optional headers in addition to the 401Unauthorized status code since it's method `createAuthenticationError` is always looking for the 401. Is there a way we can modify a RestStatus code back in the response or the only way around it is to use the 401 response code and play around with the headers? The default response I see on a failure -  
{  
"error": {  
"root\_cause": [  
{  
"type": "security\_exception",  
"reason": "missing authentication token for REST request [/]",  
"header": {  
"WWW-Authenticate": "Basic realm="security" charset="UTF-8""  
}  
}  
],  
"type": "security\_exception",  
"reason": "missing authentication token for REST request [/]",  
"header": {  
"WWW-Authenticate": "Basic realm="security" charset="UTF-8""  
}  
},  
"status": 401  
}

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 10, 2019, 4:14am UTC](https://discuss.elastic.co/t/modifying-the-default-reststatus-code-in-custom-realm-auth-response/207436/2 "2019-12-10T04:14:52Z")

</div>

Closed on December 10, 2019, 4:14am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
