Skip to content

Commit

Permalink
Add openid as default scope and add option to set custom scopes (#4078)
Browse files Browse the repository at this point in the history
  • Loading branch information
Axenu authored Jan 16, 2023
1 parent c206b38 commit 8b18b00
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions libs/keycloak-admin-client/src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Credentials {
totp?: string;
offlineToken?: boolean;
refreshToken?: string;
scopes?: string[];
}

export interface Settings {
Expand All @@ -32,6 +33,7 @@ export interface TokenResponseRaw {
not_before_policy: number;
session_state: string;
scope: string;
id_token?: string;
}

export interface TokenResponse {
Expand All @@ -43,6 +45,7 @@ export interface TokenResponse {
notBeforePolicy: number;
sessionState: string;
scope: string;
idToken?: string;
}

export const getToken = async (settings: Settings): Promise<TokenResponse> => {
Expand All @@ -61,6 +64,7 @@ export const getToken = async (settings: Settings): Promise<TokenResponse> => {
client_id: credentials.clientId,
totp: credentials.totp,
...(credentials.offlineToken ? { scope: "offline_access" } : {}),
...(credentials.scopes ? { scope: credentials.scopes.join(" ") } : {}),
...(credentials.refreshToken
? {
refresh_token: credentials.refreshToken,
Expand Down
23 changes: 23 additions & 0 deletions libs/keycloak-admin-client/test/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,27 @@ describe("Authorization", () => {
"scope"
);
});

it("should get token from local keycloak with custom scope", async () => {
const data = await getToken({
credentials: {
...credentials,
scopes: ["openid", "profile"],
},
});

expect(data).to.have.all.keys(
"accessToken",
"expiresIn",
"refreshExpiresIn",
"refreshToken",
"tokenType",
"notBeforePolicy",
"sessionState",
"scope",
"idToken"
);

expect(data.scope).to.equal("openid profile email");
});
});

0 comments on commit 8b18b00

Please sign in to comment.