Add openid as default scope and add option to set custom scopes (#4078)

This commit is contained in:
Axenu 2023-01-16 13:43:04 +01:00 committed by GitHub
parent c206b38c3d
commit 8b18b00f63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View file

@ -14,6 +14,7 @@ export interface Credentials {
totp?: string; totp?: string;
offlineToken?: boolean; offlineToken?: boolean;
refreshToken?: string; refreshToken?: string;
scopes?: string[];
} }
export interface Settings { export interface Settings {
@ -32,6 +33,7 @@ export interface TokenResponseRaw {
not_before_policy: number; not_before_policy: number;
session_state: string; session_state: string;
scope: string; scope: string;
id_token?: string;
} }
export interface TokenResponse { export interface TokenResponse {
@ -43,6 +45,7 @@ export interface TokenResponse {
notBeforePolicy: number; notBeforePolicy: number;
sessionState: string; sessionState: string;
scope: string; scope: string;
idToken?: string;
} }
export const getToken = async (settings: Settings): Promise<TokenResponse> => { export const getToken = async (settings: Settings): Promise<TokenResponse> => {
@ -61,6 +64,7 @@ export const getToken = async (settings: Settings): Promise<TokenResponse> => {
client_id: credentials.clientId, client_id: credentials.clientId,
totp: credentials.totp, totp: credentials.totp,
...(credentials.offlineToken ? { scope: "offline_access" } : {}), ...(credentials.offlineToken ? { scope: "offline_access" } : {}),
...(credentials.scopes ? { scope: credentials.scopes.join(" ") } : {}),
...(credentials.refreshToken ...(credentials.refreshToken
? { ? {
refresh_token: credentials.refreshToken, refresh_token: credentials.refreshToken,

View file

@ -21,4 +21,27 @@ describe("Authorization", () => {
"scope" "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");
});
}); });