# Protect a service application by using OpenID Connect (OIDC) Bearer token authentication

Here, you use the Quarkus OpenID Connect (OIDC) extension to secure a Jakarta REST application using Bearer token authentication. The bearer tokens are issued by OIDC and OAuth 2.0 compliant authorization servers, such as Keycloak.

To better understand OIDC Bearer token authentication, see OIDC Bearer token authentication.

If you want to protect web applications by using OIDC Authorization Code Flow authentication, see OIDC authorization code flow authentication.

## Prerequisites

To complete this guide, you need:

- Roughly 15 minutes

- An IDE

- JDK 11+ installed with `JAVA_HOME` configured appropriately

- Apache Maven 3.9.3

- A working container runtime (Docker or Podman)

- Optionally the Quarkus CLI if you want to use it

- Optionally Mandrel or GraalVM installed and configured appropriately if you want to build a native executable (or Docker if you use a native container build)

- jq tool

## Architecture

In this example, we build a simple microservice which offers two endpoints:

- `/api/users/me`

- `/api/admin`

These endpoints are protected and can only be accessed if a client is sending a bearer token along with the request, which must be valid (e.g.: signature, expiration and audience) and trusted by the microservice.

The bearer token is issued by a Keycloak Server and represents the subject to which the token was issued for. For being an OAuth 2.0 Authorization Server, the token also references the client acting on behalf of the user.

The `/api/users/me` endpoint can be accessed by any user with a valid token. As a response, it returns a JSON document with details about the user where these details are obtained from the information carried on the token.

The `/api/admin` endpoint is protected with RBAC (Role-Based Access Control) where only users granted with the `admin` role can access. At this endpoint, we use the `@RolesAllowed` annotation to declaratively enforce the access constraint.

## Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Clone the Git repository: `git clone https://github.com/quarkusio/quarkus-quickstarts.git`, or download an archive.

The solution is located in the `security-openid-connect-quickstart` directory.

## Procedure

### Create the Maven project

First, we need a new project. Create a new project with the following command:

CLI

    quarkus create app org.acme:security-openid-connect-quickstart \
        --extension='oidc,resteasy-reactive-jackson' \
        --no-code
    cd security-openid-connect-quickstart

To create a Gradle project, add the `--gradle` or `--gradle-kotlin-dsl` option.

For more information about how to install and use the Quarkus CLI, see the Quarkus CLI guide.

Maven

    mvn io.quarkus.platform:quarkus-maven-plugin:3.3.0:create \
        -DprojectGroupId=org.acme \
        -DprojectArtifactId=security-openid-connect-quickstart \
        -Dextensions='oidc,resteasy-reactive-jackson' \
        -DnoCode
    cd security-openid-connect-quickstart

To create a Gradle project, add the `-DbuildTool=gradle` or `-DbuildTool=gradle-kotlin-dsl` option.

For Windows users:

- If using cmd, (don’t use backward slash `\` and put everything on the same line)

- If using Powershell, wrap `-D` parameters in double quotes e.g. `"-DprojectArtifactId=security-openid-connect-quickstart"`

This command generates a Maven project, importing the `oidc` extension which is an implementation of OIDC for Quarkus.

If you already have your Quarkus project configured, you can add the `oidc` extension to your project by running the following command in your project base directory:

CLI

    quarkus extension add 'oidc'

Maven

    ./mvnw quarkus:add-extension -Dextensions='oidc'

Gradle

    ./gradlew addExtension --extensions='oidc'

This will add the following to your build file:

pom.xml

    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-oidc</artifactId>
    </dependency>

build.gradle

    implementation("io.quarkus:quarkus-oidc")

### Write the application

Let’s start by implementing the `/api/users/me` endpoint. As you can see from the source code below it is just a regular Jakarta REST resource:

    package org.acme.security.openid.connect;

    import jakarta.annotation.security.RolesAllowed;
    import jakarta.inject.Inject;
    import jakarta.ws.rs.GET;
    import jakarta.ws.rs.Path;

    import org.jboss.resteasy.reactive.NoCache;
    import io.quarkus.security.identity.SecurityIdentity;

    @Path("/api/users")
    public class UsersResource {

        @Inject
        SecurityIdentity securityIdentity;

        @GET
        @Path("/me")
        @RolesAllowed("user")
        @NoCache
        public User me() {
            return new User(securityIdentity);
        }

        public static class User {

            private final String userName;

            User(SecurityIdentity securityIdentity) {
                this.userName = securityIdentity.getPrincipal().getName();
            }

            public String getUserName() {
                return userName;
            }
        }
    }

The source code for the `/api/admin` endpoint is also very simple. The main difference here is that we are using a `@RolesAllowed` annotation to make sure that only users granted with the `admin` role can access the endpoint:

    package org.acme.security.openid.connect;

    import jakarta.annotation.security.RolesAllowed;
    import jakarta.ws.rs.GET;
    import jakarta.ws.rs.Path;
    import jakarta.ws.rs.Produces;
    import jakarta.ws.rs.core.MediaType;

    @Path("/api/admin")
    public class AdminResource {

        @GET
        @RolesAllowed("admin")
        @Produces(MediaType.TEXT_PLAIN)
        public String admin() {
            return "granted";
        }
    }

Injection of the `SecurityIdentity` is supported in both `@RequestScoped` and `@ApplicationScoped` contexts.

### Configure the application

Configure the Quarkus OpenID Connect (OIDC) extension by setting the following configuration properties in the `src/main/resources/application.properties` file.

    %prod.quarkus.oidc.auth-server-url=http://localhost:8180/realms/quarkus
    quarkus.oidc.client-id=backend-service
    quarkus.oidc.credentials.secret=secret

    # Tell Dev Services for Keycloak to import the realm file
    # This property is not effective when running the application in JVM or Native modes

    quarkus.keycloak.devservices.realm-path=quarkus-realm.json

Where:

- `%prod.quarkus.oidc.auth-server-url` sets the base URL of the OpenID Connect (OIDC) server. The `%prod.` profile prefix ensures that `Dev Services for Keycloak` launches a container when you run the application in dev mode. See Running the Application in Dev mode section below for more information.

- `quarkus.oidc.client-id` sets a client-id that identifies the application.

- `quarkus.oidc.credentials.secret` sets the client secret, which is used by the `client_secret_basic` authentication method.

For more information, see OpenID Connect (OIDC) configuration properties.

### Start and configure the Keycloak server

Before you start with configuration, put the realm configuration file on the classpath (`target/classes` directory) to import it automatically when running in dev mode - unless you have already built a complete solution. In this case, the realm file is added to the classpath during the build.

| Do not start the Keycloak server when you run the application in a dev mode - `Dev Services for Keycloak` will launch a container. See the Running the Application in Dev mode section below for more information.

To start a Keycloak Server, you can use Docker and just run the following command:

    docker run --name keycloak -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin -p 8180:8080 quay.io/keycloak/keycloak:{keycloak.version} start-dev

where `keycloak.version` should be set to `17.0.0` or higher.

You should be able to access your Keycloak Server at localhost:8180.

Log in as the `admin` user to access the Keycloak Administration Console. Username should be `admin` and password `admin`.

Import the realm configuration file to create a new realm. For more details, see the Keycloak documentation about how to create a new realm.

| If you want to use the Keycloak Admin Client to configure your server from your application, include the either `quarkus-keycloak-admin-client` or the `quarkus-keycloak-admin-client-reactive` (if the application uses `quarkus-rest-client-reactive`) extension. See the Quarkus Keycloak Admin Client guide for more information.

### Run the application in Dev mode

To run the application in a dev mode, use:

CLI

    quarkus dev

Maven

    ./mvnw quarkus:dev

Gradle

    ./gradlew --console=plain quarkusDev

Dev Services for Keycloak will launch a Keycloak container and import a `quarkus-realm.json`.

Open a Dev UI available at /q/dev-v1 and click on a `Provider: Keycloak` link in an `OpenID Connect` `Dev UI` card.

You will be asked to log in into a `Single Page Application` provided by `OpenID Connect Dev UI`:

- Login as `alice` (password: `alice`) who has a `user` role

  - accessing `/api/admin` will return `403`

  - accessing `/api/users/me` will return `200`

- Logout and login as `admin` (password: `admin`) who has both `admin` and `user` roles

  - accessing `/api/admin` will return `200`

  - accessing `/api/users/me` will return `200`

### Run the Application in JVM mode

When you’re done playing with the `dev` mode" you can run it as a standard Java application.

First compile it:

CLI

    quarkus build

Maven

    ./mvnw install

Gradle

    ./gradlew build

Then run it:

    java -jar target/quarkus-app/quarkus-run.jar

### Run the application in Native mode

This same demo can be compiled into native code: no modifications required.

This implies that you no longer need to install a JVM on your production environment, as the runtime technology is included in the produced binary, and optimized to run with minimal resource overhead.

Compilation will take a bit longer, so this step is disabled by default; let’s build again by enabling the `native` profile:

CLI

    quarkus build --native

Maven

    ./mvnw install -Dnative

Gradle

    ./gradlew build -Dquarkus.package.type=native

After getting a cup of coffee, you’ll be able to run this binary directly:

    ./target/security-openid-connect-quickstart-1.0.0-SNAPSHOT-runner

### Test the application

See the Running the Application in Dev mode section above about testing your application in a dev mode.

You can test the application launched in JVM or Native modes with `curl`.

The application is using Bearer token authentication and the first thing to do is obtain an access token from the Keycloak Server in order to access the application resources:

    export access_token=$(\
        curl --insecure -X POST http://localhost:8180/realms/quarkus/protocol/openid-connect/token \
        --user backend-service:secret \
        -H 'content-type: application/x-www-form-urlencoded' \
        -d 'username=alice&password=alice&grant_type=password' | jq --raw-output '.access_token' \
     )

The example above obtains an access token for user `alice`.

Any user is allowed to access the `http://localhost:8080/api/users/me` endpoint, which basically returns a JSON payload with details about the user.

    curl -v -X GET \
      http://localhost:8080/api/users/me \
      -H "Authorization: Bearer "$access_token

The `http://localhost:8080/api/admin` endpoint can only be accessed by users with the `admin` role. If you try to access this endpoint with the previously issued access token, you should get a `403` response from the server.

    curl -v -X GET \
       http://localhost:8080/api/admin \
       -H "Authorization: Bearer "$access_token

In order to access the admin endpoint, you should obtain a token for the `admin` user:

    export access_token=$(\
        curl --insecure -X POST http://localhost:8180/realms/quarkus/protocol/openid-connect/token \
        --user backend-service:secret \
        -H 'content-type: application/x-www-form-urlencoded' \
        -d 'username=admin&password=admin&grant_type=password' | jq --raw-output '.access_token' \
     )

Please also see the OIDC Bearer token authentication, Dev Services for Keycloak section, about writing the integration tests which depend on `Dev Services for Keycloak`.

## References

- OIDC configuration properties

- OIDC Bearer token authentication

- Keycloak Documentation

- OpenID Connect

- JSON Web Token

- OpenID Connect and OAuth2 Client and Filters Reference Guide

- Dev Services for Keycloak

- Sign and encrypt JWT tokens with SmallRye JWT Build

- Combining authentication mechanisms

- Quarkus Security

- Quarkus Keycloak Admin Client