This extension provides a ClientAuthentication object that you can inject to
provide the authorization token to HTTP clients when communicating with other
services.
Add this to your project's pom.xml:
...
<dependencies>
...
<dependency>
<groupId>org.jboss.pnc</groupId>
<artifactId>quarkus-pnc-client-auth</artifactId>
<version>PROJECT_VERSION</version>
</dependency>
...
</dependencies>@ApplicationScoped
public class Yummy {
@Inject
PNCClientAuth clientAuth;
...
public void method() {
String authHeaderValue = clientAuth.getHttpAuthorizationHeaderValue();
// if you only want the token
String authToken = clientAuth.getAuthToken();
...
}
}In your application.yaml:
pnc_client_auth:
type: OIDC # or LDAP
ldap_credentials:
path: /mnt/secrets/ldap_credentials # file must be in format: <username>:<password>
# We also need to configure the oidc-client
# More here: https://quarkus.io/guides/security-openid-connect-client-reference
quarkus:
oidc-client:
auth-server-url: https://keycloak/auth
client-id: quarkus_app
credentials:
secret: secret
refresh-token-time-skew: 2MIf you use PNCClientAuth.getHttpAuthorizationHeaderValueWithCachedToken, it is recommended to set the
refresh-token-time-skew configuration so that the OIDC access token gets refreshed way before they expire
You can mock PNCClientAuth in your Quarkus app for testing:
@Mock
@ApplicationScoped
public class PNCClientAuthMock implements PNCClientAuth {
@Override
public String getAuthToken() {
return "1234";
}
@Override
public String getHttpAuthorizationHeaderValue() {
return "Bearer 1234";
}
@Override
public String getHttpAuthorizationHeaderValueWithCachedToken() {
return getHttpAuthorizationHeaderValue();
}
@Override
public LDAPCredentials getLDAPCredentials() throws IOException {
return new LDAPCredentials("user", "password");
}
}and add in your application.properties:
%test.quarkus.arc.exclude-types=org.jboss.pnc.quarkus.client.auth.runtime.PNCClientAuthImpl
or in your application.yaml:
%test:
quarkus:
arc:
exclude-types: org.jboss.pnc.quarkus.client.auth.runtime.PNCClientAuthImplThis is required so that the implementation is never loaded and your mock is considered as the default implementation.