Warning: A non-numeric value encountered in /home/guillerm/public_html/wp-content/themes/Builder-Cohen/lib/builder-core/lib/layout-engine/modules/class-layout-module.php on line 499

OpenAM 12 Custom Authentication: Error Messages

This is the first of a series of posts in which I plan to cover some tips and pitfalls involving the creation of custom authentication modules in OpenAM 12. One of the most common people ask me is how do I customize the error messages returned by the OpenAM’s authentication engine?. Today I will explain step-by-step how to add custom error messages to your authentication modules.

Prerequisites

  • This approach will only work with OpenAM 12. In the newly released OpenAM 13 there is a simper way of achieving the same result, which I intend to cover in a future post.
  • Since code changes will be required you can only apply this technique for custom authentication modules.

You can find the sample code here.

Some background…

In OpenAM 12 the standard way of authenticating users is by invoking the authenticate REST endpoint:

If the authentication request is successful (like in the above example) the response will contain the authentication token for that user (e.g “tokenId“).

When authentication fails the endpoint will return an error message instead:

Invalid Username

Invalid Password

Next, I will show how you can add custom error messages to your authentication module not just for the error cases listed above (invalid username or password) but for any generic error message you might want to return to the client.

Solution in two simple steps

First, you need to add an error authentication state to your module descriptor file (typically this file can be found under $OPENAM_ROOT/config/auth/default/<MODULE_NAME>.xml):

There are a couple of things worth mentioning in the Callbacks definition:

  • The authentication module will replace the header value (header=”#ERROR MESSAGE#”) with the custom error message.
  • The Callbacks definition should have at least one callback. This seems to be unnecessary since the callback will never be used but OpenAM will throw an error if no callbacks are present.
  • The new state should be marked with error=”true”.

Once the new error state is available you’ll have to do two things in order to display a custom error message from your module:

  1. Replace the error state’s header with your custom message. You can add a simple method like the one below to your module. Notice that the substituteHeader method gets inherited from the AMLoginModule base class:
  2. Return the authentication error state (i.e. the order assign to your state in the descriptor file) from your process method after replacing the header:

In our case ERROR_STATE should be set to “2”.

After redeploying the authentication module OpenAM will return the new error messages in the HTTP response body:

Invalid Username

Invalid Password

Hope you find this useful and don’t forget to leave a comment with your questions/feedback!

See you soon.

 

Comments

  1. Hi.

    I am trying to do the same in OpenAM 13 but confused with with xml to exit under /tomcat_webapp_dir/openam/config/auth/default.
    I am using LDAP to authenticate.


Warning: A non-numeric value encountered in /home/guillerm/public_html/wp-content/themes/Builder-Cohen/lib/builder-core/lib/layout-engine/modules/class-layout-module.php on line 499