README.adoc

http-custom-mechanism: Create a custom HTTP authorization mechanism

The http-custom-mechanism quickstart demonstrates how to implement a custom HTTP authentication mechanism that can be registered with Elytron.

What is it?

The http-custom-mechanism quickstart demonstrates how to implement a custom HTTP mechanism and then register it with Elytron. This makes it possible to override the configuration within the deployment to make use of this mechanism without requiring modifications to the deployment. This example makes use of custom HTTP headers to receive a clear text username and password and use them for authentication.

Warning
Generally you should avoid passing clear text passwords. It is only done here to simplify the example.

This example consists of the following Maven projects.

Project Description

webapp

The project in the webapp/ folder contains a deployable web application consisting of a secured servlet that is used to test the configuration. It generates the {artifactId}-webapp.war archive that is used to test the secured application.

custom-module

This project contains the source for a custom HTTP authentication module that overrides the deployment configuration. It contains of the following Java classes and resources.

  • CustomMechanismFactory: This class implements HttpServerAuthenticationMechanismFactory and creates the new custom HttpServerAuthenticationMechanism.

  • CustomHeaderHttpAuthenticationMechanism: This class implements HttpServerAuthenticationMechanism and contains the source code for the custom mechanism processing. An HttpServerRequest object is passed to the evaluateRequest method. The mechanism processes the requests and uses one of the following callback methods on the request to indicate the outcome:

    • authenticationComplete - The mechanism successfully authenticated the request.

    • authenticationFailed - Authentication was attempted but failed.

    • authenticationInProgress - Authentication started but an additional round trip is needed.

    • badRequest - The authentication for this mechanism failed validation of the request.

    • noAuthenticationInProgress - The mechanism did not attempt any stage of authentication.

  • org.wildfly.security.examples.CustomMechanismFactory: This resource file contains a single line of text that names the custom mechanism factory class, which in this case is org.jboss.as.quickstart.http_custom_mechanism.CustomMechanismFactory.

You will follow these basic steps to test this quickstart:

  1. Add the application user.

  2. Back up the server configuration before starting the server.

  3. Configure the application security domain.

  4. Build and deploy the application.

  5. Test the secured servlet deployment.

  6. Build the custom HTTP mechanism module and add it to the server.

  7. Configure the server to override the deployment and use the custom module.

  8. Test the secured servlet using the custom mechanism

Configure the Application Security Domain

By default, the deployed application uses the other security domain. You need to map this security domain to an Elytron HTTP authentication factory. For your convenience, this quickstart includes the command to configure the security domain in the configure-security-domain.cli script located in the root directory of this quickstart.

In a terminal, navigate to the root directory of this quickstart, and run the following command, replacing {jbossHomeName} with the path to your server.

$ {jbossHomeName}/bin/jboss-cli.sh --connect --file=configure-security-domain.cli

You should see the following result.

{"outcome" => "success"}
WFLYUT0021: Registered web context: '/http-custom-mechanism-webapp' for server 'default-server'
WFLYSRV0010: Deployed "http-custom-mechanism-webapp.war" (runtime-name : "http-custom-mechanism-webapp.war")

Test the Secured Servlet

Before you continue, you must test the secured servlet deployment to make sure it is working. Since this application uses a standard mechanism, it could be tested using a browser. However, after you implement the custom HTTP mechanism, the browser will not understand the request, so it is better to test the call using a client that will allow you to manipulate the headers yourself.

Issue the following command to test the deployment.

curl -v http://localhost:8080/http-custom-mechanism-webapp/secured -u quickstartUser:quickstartPwd1!

You should see the HTTP result HTTP/1.1 200 OK, along with some header information, then followed by this output.

<html>
 <head><title>Secured Servlet</title></head>
 <body>
   <h1>Secured Servlet</h1>
   <p>
Current Principal 'quickstartUser'    </p>
 </body>
</html>

Build the Custom HTTP Mechanism JAR

Once the secured servlet is deployed and you have tested it to make sure it is working, you need to build the module for the custom HTTP mechanism.

  1. Open a terminal and navigate to the custom-module/ folder located in the root directory of this quickstart.

  2. Type the following command to build the custom HTTP mechanism.

    $ mvn clean install

This creates the target/{artifactId}.jar file in the quickstart custom-module/ directory.

You should see [INFO] BUILD SUCCESS in the console output.

Add the Custom Module to the Server

Now you must add the {artifactId}.jar as a custom module to the {productName} server. For your convenience, this quickstart includes the command to add the module in the add-custom-module.cli script located in the custom-module/ directory of this quickstart.

In a terminal, navigate to the custom-module/ folder located in the root directory of this quickstart, and run the following command, replacing {jbossHomeName} with the path to your server.

$ {jbossHomeName}/bin/jboss-cli.sh --connect --file=add-custom-module.cli
Note
For Windows, use the {jbossHomeName}\bin\jboss-cli.bat script.

This creates the custom {jbossHomeName}/modules/modules/org/jboss/as/quickstart/http_custom_mechanism/main folder, then copies in the {artifactId}.jar file and creates the required module.xml file.

You can verify the module structure in your file manager.

.
└── {jbossHomeName}
    └── modules
        └── org
            └── jboss
                └── as
                    └── quickstart
                        └── http_custom_mechanism
                            └── main
                                ├── {artifactId}.jar
                                └── module.xml

Configure the Server to Use the Custom Module

You configure the server to use the custom module by running CLI commands. For your convenience, this quickstart batches the commands into a configure-elytron.cli script provided in the root directory of this quickstart.

  1. Before you begin, make sure you have done the following:

  2. Review the configure-elytron.cli file in the root of this quickstart directory. This script adds the configuration that enables Elytron security fto use the custom HTTP module created by this quickstart . Comments in the script describe the purpose of each block of commands.

  3. Open a new terminal, navigate to the root directory of this quickstart, and run the following command, replacing {jbossHomeName} with the path to your server.

    $ {jbossHomeName}/bin/jboss-cli.sh --connect --file=configure-elytron.cli
    Note
    For Windows, use the {jbossHomeName}\bin\jboss-cli.bat script.

    You should see the following result when you run the script.

    The batch executed successfully
    process-state: reload-required
  4. Stop the {productName} server.

Review the Modified Server Configuration

After stopping the server, open the {jbossHomeName}/standalone/configuration/standalone.xml file and review the changes.

  1. The following service-loader-http-server-mechanism-factory was added to the http element of the elytron subsystem:

    <http>
        <http-authentication-factory name="custom-mechanism" http-server-mechanism-factory="custom-factory" security-domain="ApplicationDomain">
            <mechanism-configuration>
                <mechanism mechanism-name="CUSTOM_MECHANISM"/>
              </mechanism-configuration>
        </http-authentication-factory>
        ...
        <service-loader-http-server-mechanism-factory name="custom-factory" module="org.jboss.as.quickstart.http_custom_mechanism.custom-http-mechanism"/>
    </http>
  2. The application-security-domain in the undertow subsystem was updated to use the custom-mechanism authentication factory with override-deployment-config set to true.

    <application-security-domains>
        <application-security-domain name="other" http-authentication-factory="custom-mechanism" override-deployment-config="true"/>
    </application-security-domains>

Test the Secured Servlet Using the Custom Mechanism

Now you need to test the override of the deployment with the custom HTTP mechanism.

If you use the same curl command as when you tested the servlet before implementing the custom HTTP mechanism, it will fail with the following error.

< HTTP/1.1 401 Unauthorized
....
< X-MESSAGE: Please resubmit the request with a username specified using the X-USERNAME and a password specified using the X-PASSWORD header.

This is because the authentication mechanism rejected the call and subsequently added a header describing how to do authentication. You must modify the curl command to the following.

curl -v http://localhost:8080/http-custom-mechanism-webapp/secured -H "X-USERNAME:quickstartUser" -H "X-PASSWORD:password"

You should see the HTTP result HTTP/1.1 200 OK, along with some header information, then followed by this output.

<html>
 <head><title>Secured Servlet</title></head>
 <body>
   <h1>Secured Servlet</h1>
   <p>
Current Principal 'quickstartUser'    </p>
 </body>
</html>