-
Notifications
You must be signed in to change notification settings - Fork 208
/
OidcDelegationClient.ts
71 lines (58 loc) · 3.09 KB
/
OidcDelegationClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2019 Bentley Systems, Incorporated. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license terms.
*--------------------------------------------------------------------------------------------*/
/** @module Authentication */
import { AccessToken, IncludePrefix } from "@bentley/imodeljs-clients";
import { GrantParams, TokenSet } from "openid-client";
import { BentleyStatus, BentleyError, ClientRequestContext } from "@bentley/bentleyjs-core";
import { OidcBackendClientConfiguration, OidcBackendClient } from "./OidcBackendClient";
/** @beta */
export type OidcDelegationClientConfiguration = OidcBackendClientConfiguration;
/** Utility to generate delegation OAuth or legacy SAML tokens for backend applications
* @beta
*/
export class OidcDelegationClient extends OidcBackendClient {
/** Creates an instance of OidcBackendClient.
*/
public constructor(configuration: OidcDelegationClientConfiguration) {
super(configuration);
}
private async exchangeToJwtToken(requestContext: ClientRequestContext, accessToken: AccessToken, grantType: string): Promise<AccessToken> {
requestContext.enter();
const grantParams: GrantParams = {
grant_type: grantType,
scope: this._configuration.scope,
assertion: accessToken.toTokenString(IncludePrefix.No),
};
const client = await this.getClient(requestContext);
const tokenSet: TokenSet = await client.grant(grantParams);
return this.createToken(tokenSet, accessToken.getUserInfo());
}
/** Get a JWT for the specified scope from a SAML token */
public async getJwtFromSaml(requestContext: ClientRequestContext, accessToken: AccessToken): Promise<AccessToken> {
requestContext.enter();
return this.exchangeToJwtToken(requestContext, accessToken, "urn:ietf:params:oauth:grant-type:saml-token");
}
/** Get a delegation JWT for a new scope from another JWT */
public async getJwtFromJwt(requestContext: ClientRequestContext, accessToken: AccessToken): Promise<AccessToken> {
requestContext.enter();
return this.exchangeToJwtToken(requestContext, accessToken, "urn:ietf:params:oauth:grant-type:jwt-bearer");
}
/** Get a SAML token for the specified scope from a JWT token */
public async getSamlFromJwt(requestContext: ClientRequestContext, jwt: AccessToken): Promise<AccessToken> {
requestContext.enter();
const grantType = "urn:ietf:params:oauth:grant-type:jwt-bearer";
const params: GrantParams = {
grant_type: grantType,
scope: this._configuration.scope,
assertion: jwt.toTokenString(IncludePrefix.No),
};
const client = await this.getClient(requestContext);
const tokenSet: TokenSet = await client.grant(params);
const samlToken = AccessToken.fromSamlTokenString(tokenSet.access_token, IncludePrefix.No);
if (!samlToken)
throw new BentleyError(BentleyStatus.ERROR, `Could not convert jwt to accessToken`);
return samlToken;
}
}