Skip to content

Commit c8f710a

Browse files
Merge pull request #251 from movinsilva/feature/enhance-getauthorizationurl
2 parents 6bb00d5 + fe0a465 commit c8f710a

File tree

4 files changed

+139
-27
lines changed

4 files changed

+139
-27
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ If you are looking for an SDK to use in your application, then you can find the
2727
- [constructor](#constructor)
2828
- [initialize](#initialize)
2929
- [getDataLayer](#getDataLayer)
30+
- [getAuthorizationURLParams](#getAuthorizationURLParams)
3031
- [getAuthorizationURL](#getAuthorizationURL)
3132
- [requestAccessToken](#requestAccessToken)
3233
- [getSignOutURL](#getSignOutURL)
@@ -323,6 +324,48 @@ This method returns the `CryptoHelper` object used by the SDK to perform cryptog
323324
```TypeScript
324325
const cryptoHelper = auth.getCryptoHelper();
325326
```
327+
---
328+
329+
### getAuthorizationURLParams
330+
331+
```TypeScript
332+
getAuthorizationURLParams(config?: GetAuthURLConfig, userID?: string): Promise<Map<string, string>>
333+
```
334+
335+
#### Arguments
336+
337+
1. config: [`GetAuthURLConfig`](#GetAuthURLConfig) (optional)
338+
339+
An optional config object that has the necessary attributes to configure this method. The `forceInit` attribute can be set to `true` to trigger a request to the `.well-known` endpoint and obtain the OIDC endpoints. By default, a request to the `.well-known` endpoint will be sent only if a request to it had not been sent before. If you wish to force a request to the endpoint, you can use this attribute.
340+
341+
The object can only contain key-value pairs that you wish to append as path parameters to the authorization URL. For example, to set the `fidp` parameter, you can insert `fidp` as a key and its value to this object.
342+
343+
2. userID: `string` (optional)
344+
345+
If you want to use the SDK to manage multiple user sessions, you can pass a unique ID here to generate an authorization URL specific to that user. This can be useful when this SDK is used in backend applications.
346+
347+
#### Returns
348+
349+
A Promise that resolves with the authorization URL Parameters.
350+
351+
#### Description
352+
353+
This method returns a Promise that resolves with the authorization URL Parameters, which then can be used to build the authorization request.
354+
355+
#### Example
356+
357+
```TypeScript
358+
const config = {
359+
forceInit: true,
360+
fidp: "fb"
361+
}
362+
363+
auth.getAuthorizationURLParams(config).then((params)=>{
364+
console.log(params);
365+
}).catch((error)=>{
366+
console.error(error);
367+
});
368+
```
326369

327370
---
328371

lib/src/client.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,54 @@ export class AsgardeoAuthClient<T> {
169169
return AsgardeoAuthClient._instanceID;
170170
}
171171

172+
/**
173+
* This is an async method that returns a Promise that resolves with the authorization URL parameters.
174+
*
175+
* @param config - (Optional) A config object to force initialization and pass
176+
* custom path parameters such as the `fidp` parameter.
177+
* @param userID - (Optional) A unique ID of the user to be authenticated. This is useful in multi-user
178+
* scenarios where each user should be uniquely identified.
179+
*
180+
* @returns - A promise that resolves with the authorization URL parameters.
181+
*
182+
* @example
183+
* ```
184+
* auth.getAuthorizationURLParams().then((params)=>{
185+
* // console.log(params);
186+
* }).catch((error)=>{
187+
* // console.error(error);
188+
* });
189+
* ```
190+
*
191+
* {@link https://github.com/asgardeo/asgardeo-auth-js-sdk/tree/master#getAuthorizationURLParams}
192+
*
193+
* @preserve
194+
*/
195+
public async getAuthorizationURLParams(
196+
config?: GetAuthURLConfig,
197+
userID?: string
198+
): Promise<Map<string, string>> {
199+
const authRequestConfig: GetAuthURLConfig = { ...config };
200+
201+
delete authRequestConfig?.forceInit;
202+
203+
if (await this._dataLayer.getTemporaryDataParameter(OP_CONFIG_INITIATED)) {
204+
return this._authenticationCore.getAuthorizationURLParams(
205+
authRequestConfig,
206+
userID
207+
);
208+
}
209+
210+
return this._authenticationCore
211+
.getOIDCProviderMetaData(config?.forceInit as boolean)
212+
.then(() => {
213+
return this._authenticationCore.getAuthorizationURLParams(
214+
authRequestConfig,
215+
userID
216+
);
217+
});
218+
}
219+
172220
/**
173221
* This is an async method that returns a Promise that resolves with the authorization URL.
174222
*

lib/src/constants/client-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818

1919
export enum ResponseMode {
2020
formPost = "form_post",
21-
query = "query"
21+
query = "query",
22+
direct = "direct"
2223
}

lib/src/core/authentication-core.ts

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,17 @@ export class AuthenticationCore<T> {
6262
this._oidcProviderMetaData = async () => await this._dataLayer.getOIDCProviderMetaData();
6363
}
6464

65-
public async getAuthorizationURL(config?: AuthorizationURLParams, userID?: string): Promise<string> {
66-
const authorizeEndpoint: string = (await this._dataLayer.getOIDCProviderMetaDataParameter(
67-
AUTHORIZATION_ENDPOINT as keyof OIDCProviderMetaData
68-
)) as string;
69-
65+
public async getAuthorizationURLParams(
66+
config?: AuthorizationURLParams,
67+
userID?: string
68+
): Promise<Map<string, string>> {
7069
const configData: StrictAuthClientConfig = await this._config();
71-
72-
if (!authorizeEndpoint || authorizeEndpoint.trim().length === 0) {
73-
throw new AsgardeoAuthException(
74-
"JS-AUTH_CORE-GAU-NF01",
75-
"No authorization endpoint found.",
76-
"No authorization endpoint was found in the OIDC provider meta data from the well-known endpoint " +
77-
"or the authorization endpoint passed to the SDK is empty."
78-
);
79-
}
80-
81-
const authorizeRequest: URL = new URL(authorizeEndpoint);
82-
83-
const authorizeRequestParams: Map<string, string> = new Map<string, string>();
84-
70+
71+
const authorizeRequestParams: Map<string, string> = new Map<
72+
string,
73+
string
74+
>();
75+
8576
authorizeRequestParams.set("response_type", "code");
8677
authorizeRequestParams.set("client_id", configData.clientID);
8778

@@ -100,14 +91,21 @@ export class AuthenticationCore<T> {
10091
if (configData.responseMode) {
10192
authorizeRequestParams.set("response_mode", configData.responseMode);
10293
}
103-
104-
const pkceKey: string = await this._authenticationHelper.generatePKCEKey(userID);
105-
94+
95+
const pkceKey: string = await this._authenticationHelper.generatePKCEKey(
96+
userID
97+
);
98+
10699
if (configData.enablePKCE) {
107100
const codeVerifier: string = this._cryptoHelper?.getCodeVerifier();
108-
const codeChallenge: string = this._cryptoHelper?.getCodeChallenge(codeVerifier);
109-
110-
await this._dataLayer.setTemporaryDataParameter(pkceKey, codeVerifier, userID);
101+
const codeChallenge: string =
102+
this._cryptoHelper?.getCodeChallenge(codeVerifier);
103+
104+
await this._dataLayer.setTemporaryDataParameter(
105+
pkceKey,
106+
codeVerifier,
107+
userID
108+
);
111109
authorizeRequestParams.set("code_challenge_method", "S256");
112110
authorizeRequestParams.set("code_challenge", codeChallenge);
113111
}
@@ -130,9 +128,31 @@ export class AuthenticationCore<T> {
130128
STATE,
131129
AuthenticationUtils.generateStateParamForRequestCorrelation(
132130
pkceKey,
133-
customParams ? customParams[ STATE ]?.toString() : ""
131+
customParams ? customParams[STATE]?.toString() : ""
134132
)
135133
);
134+
135+
return authorizeRequestParams;
136+
}
137+
138+
public async getAuthorizationURL(config?: AuthorizationURLParams, userID?: string): Promise<string> {
139+
const authorizeEndpoint: string = (await this._dataLayer.getOIDCProviderMetaDataParameter(
140+
AUTHORIZATION_ENDPOINT as keyof OIDCProviderMetaData
141+
)) as string;
142+
143+
if (!authorizeEndpoint || authorizeEndpoint.trim().length === 0) {
144+
throw new AsgardeoAuthException(
145+
"JS-AUTH_CORE-GAU-NF01",
146+
"No authorization endpoint found.",
147+
"No authorization endpoint was found in the OIDC provider meta data from the well-known endpoint " +
148+
"or the authorization endpoint passed to the SDK is empty."
149+
);
150+
}
151+
152+
const authorizeRequest: URL = new URL(authorizeEndpoint);
153+
154+
const authorizeRequestParams: Map<string, string> =
155+
await this.getAuthorizationURLParams(config, userID);
136156

137157
for (const [ key, value ] of authorizeRequestParams.entries()) {
138158
authorizeRequest.searchParams.append(key, value);

0 commit comments

Comments
 (0)