# OpenID Connect client and token propagation quickstart

Learn how to use OpenID Connect (OIDC) and OAuth2 clients with filters to get, refresh, and propagate access tokens in your applications.

For more information about `OIDC Client` and `Token Propagation` support in Quarkus, see the [OpenID Connect (OIDC) and OAuth2 client and filters reference guide](/guides/security-openid-connect-client-reference/index.md).

To protect your applications by using Bearer Token Authorization, see the [OpenID Connect (OIDC) Bearer token authentication](/guides/security-oidc-bearer-token-authentication/index.md) guide.

## Prerequisites

To complete this guide, you need:

- Roughly 15 minutes
- An IDE
- JDK 21+ installed with `JAVA_HOME` configured appropriately
- Apache Maven 3.9.16
- A working container runtime (Docker or [Podman](/guides/podman/index.md))
- Optionally the [Quarkus CLI](/guides/cli-tooling/index.md) if you want to use it
- Optionally Mandrel or GraalVM installed and [configured appropriately](/guides/building-native-image/index.md#configuring-graalvm) if you want to build a native executable (or Docker if you use a native container build)

- [jq tool](https://stedolan.github.io/jq/)

## Architecture

In this example, an application is built with two Jakarta REST resources, `FrontendResource` and `ProtectedResource`.

- `FrontendResource` uses one of three methods to propagate access tokens to `ProtectedResource`:

\*\* It can get a token by using an OIDC client filter before propagating it. \*\* It can get a token by using a programmatically created OIDC client and propagate it by passing it to a REST client method as an HTTP `Authorization` header value. \*\* It can use an OIDC token propagation filter to propagate the incoming access token.

- `FrontendResource` has eight endpoints:

\*\* `/frontend/user-name-with-oidc-client-token` \*\* `/frontend/admin-name-with-oidc-client-token` \*\* `/frontend/user-name-with-oidc-client-token-header-param` \*\* `/frontend/admin-name-with-oidc-client-token-header-param` \*\* `/frontend/user-name-with-oidc-client-token-header-param-blocking` \*\* `/frontend/admin-name-with-oidc-client-token-header-param-blocking` \*\*  `/frontend/user-name-with-propagated-token` \*\*  `/frontend/admin-name-with-propagated-token`

- When either `/frontend/user-name-with-oidc-client-token` or `/frontend/admin-name-with-oidc-client-token` endpoint is called, `FrontendResource` uses a REST client with an OIDC client filter to get and propagate an access token to `ProtectedResource` .

- When either `/frontend/user-name-with-oidc-client-token-header-param` or `/frontend/admin-name-with-oidc-client-token-header-param` endpoint is called, `FrontendResource` uses a programmatically created OIDC client to get and propagate an access token to `ProtectedResource` by passing it to a REST client method as an HTTP `Authorization` header value.

- When either `/frontend/user-name-with-propagated-token` or `/frontend/admin-name-with-propagated-token` endpoint is called, `FrontendResource` uses a REST client with `OIDC Token Propagation Filter` to propagate the current incoming access token to `ProtectedResource`.

- `ProtectedResource` has two endpoints:

\*\*  `/protected/user-name` \*\*  `/protected/admin-name` + Both endpoints return the username extracted from the incoming access token, which was propagated to `ProtectedResource` from `FrontendResource`. The only difference between these endpoints is that calling `/protected/user-name` is only allowed if the current access token has a `user` role, and calling `/protected/admin-name` is only allowed if the current access token has an `admin` role.

## 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](https://github.com/quarkusio/quarkus-quickstarts/archive/main.zip).

The solution is in the `security-openid-connect-client-quickstart` [directory](https://github.com/quarkusio/quarkus-quickstarts/tree/main/security-openid-connect-client-quickstart).

## Creating the Maven project

Create a new project with the following command:

**CLI**

```bash
quarkus create app org.acme:security-openid-connect-client-quickstart \
    --extension='oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest' \
    --no-code
cd security-openid-connect-client-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](/guides/cli-tooling/index.md) guide.

**Maven**

```bash
mvn io.quarkus.platform:quarkus-maven-plugin:4.0.1:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=security-openid-connect-client-quickstart \
    -Dextensions='oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest' \
    -DnoCode
cd security-openid-connect-client-quickstart
```

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

For Windows users:

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

It generates a Maven project, importing the `oidc`, `rest-client-oidc-filter`, `rest-client-oidc-token-propagation`, and `rest` extensions.

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

**CLI**

```bash
quarkus extension add oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest
```

**Maven**

```bash
./mvnw quarkus:add-extension -Dextensions='oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest'
```

**Gradle**

```bash
./gradlew addExtension --extensions='oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest'
```

It adds the following extensions to your build file:

**pom.xml**

```xml
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-oidc</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-rest-client-oidc-filter</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-rest-client-oidc-token-propagation</artifactId>
</dependency>
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-rest</artifactId>
</dependency>
```

**build.gradle**

```gradle
implementation("io.quarkus:quarkus-oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest")
```

## Writing the application

1. Implement `ProtectedResource`:

   ```java
   package org.acme.security.openid.connect.client;

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

   import io.quarkus.security.Authenticated;
   import io.smallrye.mutiny.Uni;

   import org.eclipse.microprofile.jwt.JsonWebToken;

   @Path("/protected")
   @Authenticated
   public class ProtectedResource {

       @Inject
       JsonWebToken principal;

       @GET
       @RolesAllowed("user")
       @Produces("text/plain")
       @Path("userName")
       public Uni<String> userName() {
           return Uni.createFrom().item(principal.getName());
       }

       @GET
       @RolesAllowed("admin")
       @Produces("text/plain")
       @Path("adminName")
       public Uni<String> adminName() {
           return Uni.createFrom().item(principal.getName());
       }
   }
   ```

   `ProtectedResource` returns a name from both `userName()` and `adminName()` methods. The name is extracted from the current `JsonWebToken`.


1. Add the following REST clients:
2. `RestClientWithOidcClientFilter`, which uses an OIDC client filter provided by the `quarkus-rest-client-oidc-filter` extension to get and propagate an access token.
3. `RestClientWithTokenHeaderParam`, which accepts a token already acquired by the programmatically created OidcClient as an HTTP `Authorization` header value.
4. `RestClientWithTokenPropagationFilter`, which uses an OIDC token propagation filter provided by the `quarkus-rest-client-oidc-token-propagation` extension to get and propagate an access token.

1. Add the `RestClientWithOidcClientFilter` REST client:

   ```java
   package org.acme.security.openid.connect.client;

   import jakarta.ws.rs.GET;
   import jakarta.ws.rs.Path;
   import jakarta.ws.rs.Produces;

   import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

   import io.quarkus.oidc.client.filter.OidcClientFilter;
   import io.smallrye.mutiny.Uni;

   @RegisterRestClient
   @OidcClientFilter <1>
   @Path("/")
   public interface RestClientWithOidcClientFilter {

       @GET
       @Produces("text/plain")
       @Path("userName")
       Uni<String> getUserName();

       @GET
       @Produces("text/plain")
       @Path("adminName")
       Uni<String> getAdminName();
   }
   ```

   1. Register an OIDC client filter with the REST client to get and propagate the tokens.


1. Add the `RestClientWithTokenHeaderParam` REST client:

   ```java
   package org.acme.security.openid.connect.client;

   import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

   import io.smallrye.mutiny.Uni;
   import jakarta.ws.rs.GET;
   import jakarta.ws.rs.HeaderParam;
   import jakarta.ws.rs.Path;
   import jakarta.ws.rs.Produces;

   @RegisterRestClient
   @Path("/")
   public interface RestClientWithTokenHeaderParam {

       @GET
       @Produces("text/plain")
       @Path("userName")
       Uni<String> getUserName(@HeaderParam("Authorization") String authorization); <1>

       @GET
       @Produces("text/plain")
       @Path("adminName")
       Uni<String> getAdminName(@HeaderParam("Authorization") String authorization); <1>
   }
   ```

   1. `RestClientWithTokenHeaderParam` REST client expects that the tokens will be passed to it as HTTP `Authorization` header values.


1. Add the `RestClientWithTokenPropagationFilter` REST client:

   ```java
   package org.acme.security.openid.connect.client;

   import jakarta.ws.rs.GET;
   import jakarta.ws.rs.Path;
   import jakarta.ws.rs.Produces;

   import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

   import io.quarkus.oidc.token.propagation.common.AccessToken;

   import io.smallrye.mutiny.Uni;

   @RegisterRestClient
   @AccessToken <1>
   @Path("/")
   public interface RestClientWithTokenPropagationFilter {

       @GET
       @Produces("text/plain")
       @Path("userName")
       Uni<String> getUserName();

       @GET
       @Produces("text/plain")
       @Path("adminName")
       Uni<String> getAdminName();
   }
   ```

   1. Register an OIDC token propagation filter with the REST client to propagate the incoming already-existing tokens.

   > [!IMPORTANT]
   > Do not use the `RestClientWithOidcClientFilter` and `RestClientWithTokenPropagationFilter` interfaces in the same REST client because they can conflict, leading to issues.
   >
   > For example, the OIDC client filter can override the token from the OIDC token propagation filter, or the propagation filter might not work correctly if it attempts to propagate a token when none is available, expecting the OIDC client filter to obtain a new token instead.


1. Add `OidcClientCreator` to create an OIDC client programmatically at startup. `OidcClientCreator` supports `RestClientWithTokenHeaderParam` REST client calls:

   ```java
   package org.acme.security.openid.connect.client;

   import java.util.Map;

   import org.eclipse.microprofile.config.inject.ConfigProperty;

   import io.quarkus.oidc.client.OidcClient;
   import io.quarkus.oidc.client.OidcClients;
   import io.quarkus.oidc.client.runtime.OidcClientConfig;
   import io.quarkus.oidc.client.runtime.OidcClientConfig.Grant.Type;
   import io.quarkus.runtime.StartupEvent;
   import io.smallrye.mutiny.Uni;
   import jakarta.enterprise.context.ApplicationScoped;
   import jakarta.enterprise.event.Observes;
   import jakarta.inject.Inject;

   @ApplicationScoped
   public class OidcClientCreator {

       @Inject
       OidcClients oidcClients; <1>
       @ConfigProperty(name = "quarkus.oidc.auth-server-url")
       String oidcProviderAddress;

       private volatile OidcClient oidcClient;

       public void startup(@Observes StartupEvent event) {
       	createOidcClient().subscribe().with(client -> {oidcClient = client;});
       }

       public OidcClient getOidcClient() {
           return oidcClient;
       }

       private Uni<OidcClient> createOidcClient() {
           OidcClientConfig cfg = OidcClientConfig
               .authServerUrl(oidcProviderAddress)
               .id("myclient")
               .clientId("backend-service")
               .credentials("secret")
               .grant(Type.PASSWORD)
               .grantOptions("password", Map.of("username", "alice", "password", "alice"))
               .build();
           return oidcClients.newClient(cfg);
       }
   }
   ```

   1. `OidcClients` can be used to retrieve the already initialized, named OIDC clients and create new OIDC clients on demand.


1. Finish creating the application by adding `FrontendResource`:

   ```java
   package org.acme.security.openid.connect.client;

   import jakarta.inject.Inject;
   import jakarta.ws.rs.GET;
   import jakarta.ws.rs.Path;
   import jakarta.ws.rs.Produces;

   import io.quarkus.oidc.client.Tokens;
   import io.quarkus.oidc.client.runtime.TokensHelper;

   import org.eclipse.microprofile.rest.client.inject.RestClient;

   import io.smallrye.mutiny.Uni;

   @Path("/frontend")
   public class FrontendResource {
       @Inject
       @RestClient
       RestClientWithOidcClientFilter restClientWithOidcClientFilter; <1>

       @Inject
       @RestClient
       RestClientWithTokenPropagationFilter restClientWithTokenPropagationFilter; <2>

       @Inject
       OidcClientCreator oidcClientCreator;

       TokensHelper tokenHelper = new TokensHelper(); <3>

       @Inject
       @RestClient
       RestClientWithTokenHeaderParam restClientWithTokenHeaderParam; <4>

       @GET
       @Path("user-name-with-oidc-client-token")
       @Produces("text/plain")
       public Uni<String> getUserNameWithOidcClientToken() { <1>
           return restClientWithOidcClientFilter.getUserName();
       }

       @GET
       @Path("admin-name-with-oidc-client-token")
       @Produces("text/plain")
       public Uni<String> getAdminNameWithOidcClientToken() { <1>
           return restClientWithOidcClientFilter.getAdminName();
       }

       @GET
       @Path("user-name-with-propagated-token")
       @Produces("text/plain")
       public Uni<String> getUserNameWithPropagatedToken() { <2>
           return restClientWithTokenPropagationFilter.getUserName();
       }

       @GET
       @Path("admin-name-with-propagated-token")
       @Produces("text/plain")
       public Uni<String> getAdminNameWithPropagatedToken() { <2>
           return restClientWithTokenPropagationFilter.getAdminName();
       }

       @GET
       @Path("user-name-with-oidc-client-token-header-param")
       @Produces("text/plain")
       public Uni<String> getUserNameWithOidcClientTokenHeaderParam() { <4>
           return tokenHelper.getTokens(oidcClientCreator.getOidcClient()).onItem()
                   .transformToUni(tokens -> restClientWithTokenHeaderParam.getUserName("Bearer " + tokens.getAccessToken()));
       }

       @GET
       @Path("admin-name-with-oidc-client-token-header-param")
       @Produces("text/plain")
       public Uni<String> getAdminNameWithOidcClientTokenHeaderParam() { <4>
           return tokenHelper.getTokens(oidcClientCreator.getOidcClient()).onItem()
                   .transformToUni(tokens -> restClientWithTokenHeaderParam.getAdminName("Bearer " + tokens.getAccessToken()));
       }

       @GET
       @Path("user-name-with-oidc-client-token-header-param-blocking")
       @Produces("text/plain")
       public String getUserNameWithOidcClientTokenHeaderParamBlocking() { <5>
           Tokens tokens = tokenHelper.getTokens(oidcClientCreator.getOidcClient()).await().indefinitely();
           return restClientWithTokenHeaderParam.getUserName("Bearer " + tokens.getAccessToken()).await().indefinitely();
       }

       @GET
       @Path("admin-name-with-oidc-client-token-header-param-blocking")
       @Produces("text/plain")
       public String getAdminNameWithOidcClientTokenHeaderParamBlocking() { <5>
           Tokens tokens = tokenHelper.getTokens(oidcClientCreator.getOidcClient()).await().indefinitely();
           return restClientWithTokenHeaderParam.getAdminName("Bearer " + tokens.getAccessToken()).await().indefinitely();
       }

   }
   ```

   1. `FrontendResource` uses the injected `RestClientWithOidcClientFilter` REST client with the OIDC client filter to get and propagate an access token to `ProtectedResource` when either `/frontend/user-name-with-oidc-client-token` or `/frontend/admin-name-with-oidc-client-token` is called.
   2. `FrontendResource` uses the injected `RestClientWithTokenPropagationFilter` REST client with the OIDC token propagation filter to propagate the current incoming access token to `ProtectedResource` when either `/frontend/user-name-with-propagated-token` or `/frontend/admin-name-with-propagated-token` is called.
   3. `io.quarkus.oidc.client.runtime.TokensHelper` is useful when the OIDC client is used directly, without the OIDC client filter. Pass the OIDC client to `TokensHelper` to get the tokens. `TokensHelper` acquires the tokens and refreshes them if necessary in a thread-safe way.
   4. `FrontendResource` uses the programmatically created OIDC client to get and propagate an access token to `ProtectedResource` by passing it directly to the injected `RestClientWithTokenHeaderParam` REST client's method as an HTTP `Authorization` header value when either `/frontend/user-name-with-oidc-client-token-header-param` or `/frontend/admin-name-with-oidc-client-token-header-param` is called.
   5. Sometimes, an application needs to acquire tokens in a blocking manner before propagating them with the REST client. This example shows how to acquire the tokens in such cases.


1. Add a Jakarta REST `ExceptionMapper`:

   ```java
   package org.acme.security.openid.connect.client;

   import jakarta.ws.rs.core.Response;
   import jakarta.ws.rs.ext.ExceptionMapper;
   import jakarta.ws.rs.ext.Provider;

   import org.jboss.resteasy.reactive.ClientWebApplicationException;

   @Provider
   public class FrontendExceptionMapper implements ExceptionMapper<ClientWebApplicationException> {

   	@Override
   	public Response toResponse(ClientWebApplicationException t) {
   		return Response.status(t.getResponse().getStatus()).build();
   	}

   }
   ```

   This exception mapper is only added to verify during the tests that `ProtectedResource` returns `403` when the token has no expected role.

   Without this mapper, Quarkus REST (formerly RESTEasy Reactive) would correctly convert the exceptions that escape from REST client calls to `500` to avoid leaking the information from the downstream resources such as `ProtectedResource`. However, in the tests, it would not be possible to assert that `500` is caused by an authorization exception instead of some internal error.


## Configuring the application

Having prepared the code, you configure the application:

```properties
# Configure OIDC

%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 ineffective when running the application in JVM or Native modes but only in dev and test modes.

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

# Configure OIDC Client

quarkus.oidc-client.auth-server-url=${quarkus.oidc.auth-server-url}
quarkus.oidc-client.client-id=${quarkus.oidc.client-id}
quarkus.oidc-client.credentials.secret=${quarkus.oidc.credentials.secret}
quarkus.oidc-client.grant.type=password
quarkus.oidc-client.grant-options.password.username=alice
quarkus.oidc-client.grant-options.password.password=alice

# Configure REST clients

%prod.port=8080
%dev.port=8080
%test.port=8081

org.acme.security.openid.connect.client.RestClientWithOidcClientFilter/mp-rest/url=http://localhost:${port}/protected
org.acme.security.openid.connect.client.RestClientWithTokenHeaderParam/mp-rest/url=http://localhost:${port}/protected
org.acme.security.openid.connect.client.RestClientWithTokenPropagationFilter/mp-rest/url=http://localhost:${port}/protected
```

The preceding configuration references Keycloak, which is used by `ProtectedResource` to verify the incoming access tokens and by `OidcClient` to get the tokens for a user `alice` by using a `password` grant. Both REST clients point to ``ProtectedResource``'s HTTP address.

> [!NOTE]
> Adding a `%prod.` profile prefix to `quarkus.oidc.auth-server-url` ensures that `Dev Services for Keycloak` launches a container for you when the application is run in dev or test modes.
>
> For more information, see the [Running the application in dev mode](#oidc-client-keycloak-dev-mode) section.

## Starting and configuring the Keycloak server

> [!NOTE]
> Do not start the Keycloak server when you run the application in dev or test modes; `Dev Services for Keycloak` launches a container.
>
> For more information, see the [Running the application in dev mode](#oidc-client-keycloak-dev-mode) section.
>
> Ensure you put the [realm configuration file](https://github.com/quarkusio/quarkus-quickstarts/tree/main/security-openid-connect-client-quickstart/config/quarkus-realm.json) on the classpath, in the `target/classes` directory. This placement ensures that the file is automatically imported in dev mode. However, if you have already built a [complete solution](https://github.com/quarkusio/quarkus-quickstarts/tree/main/security-openid-connect-quickstart), you do not need to add the realm file to the classpath because the build process has already done so.

1. Start a Keycloak Server by using Docker:

   ```bash
   docker run --name keycloak -e KC_BOOTSTRAP_ADMIN_USERNAME=admin -e KC_BOOTSTRAP_ADMIN_PASSWORD=admin -p 8180:8080 quay.io/keycloak/keycloak:26.8.2 start-dev
   ```

   You can access your Keycloak Server at [localhost:8180](http://localhost:8180).


1. Log in as the `admin` user to access the Keycloak Administration Console. The password is `admin`.

1. Import the [realm configuration file](https://github.com/quarkusio/quarkus-quickstarts/tree/main/security-openid-connect-client-quickstart/config/quarkus-realm.json) to create a new realm.

   For more details, see the Keycloak documentation about how to [create a new realm](https://www.keycloak.org/docs/latest/server_admin/index.html#_create-realm).

   This `quarkus` realm file adds a `frontend` client, and `alice` and `admin` users. `alice` has a `user` role. `admin` has both `user` and `admin` roles.


<a id="oidc-client-keycloak-dev-mode"></a>
## Running the application in dev mode

1. Run the application in a dev mode:

   **CLI**

   ```bash
   quarkus dev
   ```

   **Maven**

   ```bash
   ./mvnw quarkus:dev
   ```

   **Gradle**

   ```bash
   ./gradlew --console=plain quarkusDev
   ```

   [Dev Services for Keycloak](/guides/security-openid-connect-dev-services/index.md) launches a Keycloak container and imports `quarkus-realm.json`.


1. Open a [Dev UI](/guides/dev-ui/index.md) available at [/q/dev-ui](http://localhost:8080/q/dev-ui) and click a `Keycloak provider` link in the **OpenID Connect Dev UI** card.

1. When asked, log in to a `Single Page Application` provided by the OpenID Connect Dev UI, log in as `admin`, with the password, `admin`.

   This user has both `admin` and `user` roles.

   1. Access `/frontend/user-name-with-propagated-token`, which returns `200`.
   2. Access `/frontend/admin-name-with-propagated-token`, which returns `200`.
2. Log out and back in as `alice` with the password, `alice`.

   This user has a `user` role.

   1. Access `/frontend/user-name-with-propagated-token`, which returns `200`.
   2. Access `/frontend/admin-name-with-propagated-token`, which returns `403`.

      You have tested that `FrontendResource` can propagate the access tokens from the OpenID Connect Dev UI.

## Running the application in JVM mode

1. After exploring the application in dev mode, run it as a standard Java application by compiling it:

   **CLI**

   ```bash
   quarkus build
   ```

   **Maven**

   ```bash
   ./mvnw install
   ```

   **Gradle**

   ```bash
   ./gradlew build
   ```


1. Run it:

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


## Running the application in native mode

You can compile this demo into native code; no modifications are 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 resources.

Compilation takes longer, so this step is turned off by default. To build again, enable the `native` profile:

**CLI**

```bash
quarkus build --native
```

**Maven**

```bash
./mvnw install -Dnative
```

**Gradle**

```bash
./gradlew build -Dquarkus.native.enabled=true
```

After a little while, when the build finishes, you can run the native binary directly:

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

<a id="testing"></a>
## Testing the application

For more information about testing your application in dev mode, see the preceding [Running the application in dev mode](#oidc-client-keycloak-dev-mode) section.

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

1. Obtain an access token for `alice`:

   ```bash
   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' \
    )
   ```


1. Use this token to call `/frontend/user-name-with-propagated-token`. This command returns the `200` status code and the name `alice`:

   ```bash
   curl -i -X GET \
     http://localhost:8080/frontend/user-name-with-propagated-token \
     -H "Authorization: Bearer "$access_token
   ```


1. Use the same token to call `/frontend/admin-name-with-propagated-token`. In contrast to the preceding command, this command returns `403` because `alice` has only a `user` role:

   ```bash
   curl -i -X GET \
     http://localhost:8080/frontend/admin-name-with-propagated-token \
     -H "Authorization: Bearer "$access_token
   ```


1. Obtain an access token for `admin`:

   ```bash
   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' \
    )
   ```


1. Use this token to call `/frontend/user-name-with-propagated-token`. This command returns a `200` status code and the name `admin`:

   ```bash
   curl -i -X GET \
     http://localhost:8080/frontend/user-name-with-propagated-token \
     -H "Authorization: Bearer "$access_token
   ```


1. Use the same token to call `/frontend/admin-name-with-propagated-token`. This command also returns the `200` status code and the name `admin` because `admin` has both `user` and `admin` roles:

   ```bash
   curl -i -X GET \
     http://localhost:8080/frontend/admin-name-with-propagated-token \
     -H "Authorization: Bearer "$access_token
   ```


1. Check the `FrontendResource` methods, which do not propagate the existing tokens but use `OidcClient` to get and propagate the tokens.

   As already shown, `OidcClient` is configured to get the tokens for the `alice` user.

   ```bash
   curl -i -X GET \
     http://localhost:8080/frontend/user-name-with-oidc-client-token
   ```

   This command returns the `200` status code and the name `alice`.

   ```bash
   curl -i -X GET \
     http://localhost:8080/frontend/admin-name-with-oidc-client-token
   ```

   In contrast with the preceding command, this command returns a `403` status code.


1. Test that the programmatically created OIDC client correctly acquires and propagates the token with `RestClientWithTokenHeaderParam` both in reactive and imperative (blocking) modes.

.. Call the `/user-name-with-oidc-client-token-header-param`. This command returns the `200` status code and the name `alice`: +

```bash
curl -i -X GET \
  http://localhost:8080/frontend/user-name-with-oidc-client-token-header-param
```

.. Call the `/admin-name-with-oidc-client-token-header-param`. In contrast with the preceding command, this command returns a `403` status code: +

```bash
curl -i -X GET \
  http://localhost:8080/frontend/admin-name-with-oidc-client-token-header-param
```

1. Test the endpoints that use OIDC client in the blocking mode.

.. Call the `/user-name-with-oidc-client-token-header-param-blocking`. This command returns the `200` status code and the name `alice`: +

```bash
curl -i -X GET \
  http://localhost:8080/frontend/user-name-with-oidc-client-token-header-param-blocking
```

.. Call the `/admin-name-with-oidc-client-token-header-param-blocking`. In contrast with the preceding command, this command returns a `403` status code: +

```bash
curl -i -X GET \
  http://localhost:8080/frontend/admin-name-with-oidc-client-token-header-param-blocking
```

## References

- [OpenID Connect Client and Token Propagation Reference Guide](/guides/security-openid-connect-client-reference/index.md)
- [OIDC Bearer token authentication](/guides/security-oidc-bearer-token-authentication/index.md)
- [Quarkus Security overview](/guides/security-overview/index.md)
