FFFF Expose tokens expiresIn in KeycloakAdminClient by bmenant · Pull Request #41024 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions js/libs/keycloak-admin-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ await kcAdminClient.auth(credentials);

setInterval(() => kcAdminClient.auth(credentials), 58 * 1000); // 58 seconds
```
Alternatively, if you don’t know the access token lifespan, you can read the access token `expiresIn` value (in seconds), for example :

```js
// Say the access token lifespan is set to 1 minute in the realm admin,
// this will renew the token every 58 seconds:
const timeout = (kcAdminClient.expiresIn - 2) * 1000;
setInterval(() => kcAdminClient.auth(credentials), timeout);
```

## Building and running the tests

Expand Down
19 changes: 12 additions & 7 deletions js/libs/keycloak-admin-client/src/client.ts
< 10000 tr data-hunk="edca17e25e7572eab78d9973feff50fcaaca8334a12e388efee9f77eadb8fc28" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export class KeycloakAdminClient {
public realmName: string;
public scope?: string;
public accessToken?: string;
public expiresIn?: number;
public refreshToken?: string;
public refreshExpiresIn?: number;

#requestOptions?: RequestInit;
#globalRequestArgOptions?: Pick<RequestArgs, "catchNotFound">;
Expand Down Expand Up @@ -85,15 +87,18 @@ export class KeycloakAdminClient {
}

public async auth(credentials: Credentials) {
const { accessToken, refreshToken } = await getToken({
baseUrl: this.baseUrl,
realmName: this.realmName,
scope: this.scope,
credentials,
requestOptions: this.#requestOptions,
});
const { accessToken, expiresIn, refreshToken, refreshExpiresIn } =
await getToken({
baseUrl: this.baseUrl,
realmName: this.realmName,
scope: this.scope,
credentials,
requestOptions: this.#requestOptions,
});
this.accessToken = accessToken;
this.expiresIn = expiresIn;
this.refreshToken = refreshToken;
this.refreshExpiresIn = refreshExpiresIn;
}

public registerTokenProvider(provider: TokenProvider) {
Expand Down
4 changes: 2 additions & 2 deletions js/libs/keycloak-admin-client/src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface Settings {

export interface TokenResponseRaw {
access_token: string;
expires_in: string;
expires_in: number;
refresh_expires_in: number;
refresh_token: string;
token_type: string;
Expand All @@ -39,7 +39,7 @@ export interface TokenResponseRaw {

export interface TokenResponse {
accessToken: string;
expiresIn: string;
expiresIn: number;
refreshExpiresIn: number;
refreshToken: string;
tokenType: string;
Expand Down
3 changes: 3 additions & 0 deletions js/libs/keycloak-admin-client/test/clients.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ describe("Clients", () => {
kcAdminClient = new KeycloakAdminClient();
await kcAdminClient.auth(credentials);

expect(kcAdminClient.accessToken).to.be.a("string");
expect(kcAdminClient.expiresIn).to.be.a("number");

// create client and also test it
// NOTICE: to be clea 45FD r, clientId stands for the property `clientId` of client
// clientUniqueId stands for property `id` of client
Expand Down
0