-
Notifications
You must be signed in to change notification settings - Fork 36
feat(core): add authentication & authorization #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7036546
feat(core): :sparkles: add custom exception class - AsgardeoUIException
movinsilva 6816344
feat(core): :sparkles: add auth client
movinsilva ef0bedc
feat(core): :sparkles: add authorize function
movinsilva 829591f
feat(core): :sparkles: add authenticate function
movinsilva 4a94d23
feat(core): :sparkles: improve config types
movinsilva ca7398f
style(core): :art: Change the comment structure, add jsdoc comments &…
movinsilva 0ae3ff9
refactor(core): :art: update custom exception class and add docs
movinsilva 94b14c1
docs(core): :memo: update JSDocs for auth client
movinsilva 3c52535
chore(workspace): :wrench: update issue template
movinsilva 21e44da
style(core): :art: improve format & comments
movinsilva b236cc1
style(core): :art: update eslint and prettier config packages & inter…
movinsilva 23afc0e
fix(core): :bug: update stack parameter type in AsgardeoUIException
movinsilva 166f031
refactor(workspace):
movinsilva baf3fc6
chore: fix formatting issue
brionmario 5a298eb
chore: fix formatting issue
brionmario c76ee31
chore: fix formatting issue
brionmario 368c625
chore: fix formatting issue
brionmario 6f3ed1c
chore: fix formatting issue
brionmario File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {AuthClient} from 'src/auth-client'; | ||
import {AuthApiRequestBody} from 'src/models/auth-api-request'; | ||
import AsgardeoUIException from '../exception'; | ||
import {AuthApiResponse} from '../models/auth-api-response'; | ||
|
||
/** | ||
* Send an authentication request to the authentication API. | ||
* | ||
* @param {AuthApiRequestBody} props - The authentication request body. | ||
* @returns {Promise<AuthApiResponse>} A promise that resolves with the authentication API response. | ||
*/ | ||
const authenticate = async (props: AuthApiRequestBody): Promise<AuthApiResponse> => { | ||
let authnRequest: Request; | ||
let response: Response; | ||
|
||
try { | ||
const formBody: string = JSON.stringify(props); | ||
|
||
const headers: Headers = new Headers(); | ||
headers.append('Content-Type', 'application/json'); | ||
|
||
const requestOptions: RequestInit = { | ||
body: formBody, | ||
headers, | ||
method: 'POST', | ||
}; | ||
|
||
/* Getting baseURL from authClient's data layer */ | ||
const {baseUrl} = await AuthClient.getInstance().getDataLayer().getConfigData(); | ||
|
||
authnRequest = new Request(`${baseUrl}/oauth2/authn`, requestOptions); | ||
} catch (error) { | ||
throw new AsgardeoUIException('JS_UI_CORE-AUTHN-A-NF', 'Authentication request building failed', error.stack); | ||
} | ||
|
||
try { | ||
response = await fetch(authnRequest); | ||
} catch (error) { | ||
throw new AsgardeoUIException('JS_UI_CORE-AUTHN-A-NE', `Authentication API call Failed: ${error}`, error.stack); | ||
} | ||
|
||
if (response.ok) { | ||
return (await response.json()) as AuthApiResponse; | ||
} | ||
|
||
throw new AsgardeoUIException( | ||
brionmario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'JS_UI_CORE-AUTHN-AN-HE', | ||
'Failed to receive a successful response from the authentication endpoint', | ||
); | ||
}; | ||
|
||
export default authenticate; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {UIAuthClient} from 'src/models/auth-config'; | ||
movinsilva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import {AuthClient} from '../auth-client'; | ||
import AsgardeoUIException from '../exception'; | ||
import {AuthApiResponse} from '../models/auth-api-response'; | ||
|
||
/** | ||
* This function is used to authorize the user. | ||
* @returns {Promise<AuthApiResponse>} A promise that resolves with the authorization response. | ||
*/ | ||
const authorize = async (): Promise<AuthApiResponse> => { | ||
let response: Response; | ||
let requestOptions: RequestInit; | ||
let authzURL: string; | ||
|
||
try { | ||
brionmario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const authInstace: UIAuthClient = AuthClient.getInstance(); | ||
const params: Map<string, string> = await authInstace.getAuthorizationURLParams(); | ||
|
||
const formBody: URLSearchParams = new URLSearchParams(); | ||
|
||
Array.from(params.entries()).forEach(([key, value]: [string, string]) => { | ||
formBody.append(key, value); | ||
}); | ||
|
||
/* Save the state temporarily in the data layer, this needs to be passed when token is requested */ | ||
await authInstace.getDataLayer().setTemporaryDataParameter('state', params.get('state')); | ||
|
||
const headers: Headers = new Headers(); | ||
headers.append('Accept', 'application/json'); | ||
headers.append('Content-Type', 'application/x-www-form-urlencoded'); | ||
|
||
requestOptions = { | ||
body: formBody.toString(), | ||
headers, | ||
method: 'POST', | ||
}; | ||
|
||
authzURL = (await authInstace.getOIDCServiceEndpoints()).authorizationEndpoint; | ||
} catch (error) { | ||
throw new AsgardeoUIException('JS_UI_CORE-AUTHZ-A-NF', 'Authorization request building failed', error.stack); | ||
} | ||
|
||
try { | ||
response = await fetch(authzURL, requestOptions); | ||
} catch (error) { | ||
throw new AsgardeoUIException('JS_UI_CORE-AUTHZ-A-NE', 'Authorization API call failed', error.stack); | ||
} | ||
|
||
if (response.ok) { | ||
return (await response.json()) as AuthApiResponse; | ||
} | ||
|
||
throw new AsgardeoUIException( | ||
brionmario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'UI_CORE-AUTHZ-A-HE', | ||
'Failed to receive a successful response from the authorization server', | ||
); | ||
}; | ||
|
||
export default authorize; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import {AsgardeoAuthClient, Store, CryptoUtils} from '@asgardeo/auth-js'; | ||
import {UIAuthClient, UIAuthConfig} from './models/auth-config'; | ||
|
||
/** | ||
* The `AuthClient` class is a singleton class that provides an instance of the `UIAuthClient`. | ||
*/ | ||
export class AuthClient { | ||
private static instance: UIAuthClient; | ||
|
||
/** | ||
* Private constructor to prevent direct object creation. | ||
* This is necessary because this is a singleton class. | ||
* @private | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
private constructor() {} | ||
|
||
/** | ||
* Returns the singleton instance of `UIAuthClient`. If the instance does not exist, it is created. | ||
* | ||
* @param {UIAuthConfig} authClientConfig - The configuration for the `UIAuthClient`. | ||
* @param {Store} store - The store for the `UIAuthClient`. | ||
* @param {CryptoUtils} cryptoUtils - The crypto utilities for the `UIAuthClient`. | ||
* @returns {UIAuthClient} The singleton instance of `UIAuthClient`. | ||
*/ | ||
static getInstance(authClientConfig?: UIAuthConfig, store?: Store, cryptoUtils?: CryptoUtils): UIAuthClient { | ||
if (!AuthClient.instance) { | ||
AuthClient.instance = new AsgardeoAuthClient(); | ||
AuthClient.instance.initialize(authClientConfig, store, cryptoUtils); | ||
} | ||
|
||
return AuthClient.instance; | ||
brionmario marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
/* Interfaces, classes and enums required from the auth-js package */ | ||
export {CryptoUtils, JWKInterface, Store, AsgardeoAuthClient, TokenResponse, ResponseMode} from '@asgardeo/auth-js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/** | ||
* `AsgardeoUIException` is a custom error class that extends the built-in `Error` class. | ||
* | ||
* @extends {Error} | ||
*/ | ||
export default class AsgardeoUIException extends Error { | ||
/** | ||
* The name of the error, which is set to the name of the constructor. | ||
* @override | ||
*/ | ||
public override name: string; | ||
|
||
/** | ||
* The stack trace of the error. | ||
* @override | ||
*/ | ||
public override stack: string; | ||
|
||
/** | ||
* The code of the error. | ||
*/ | ||
public code: string; | ||
|
||
/** | ||
* Constructs a new `AsgardeoUIException`. | ||
* | ||
* @param {string} code - The error code. | ||
* @param {string} message - The error message. | ||
* @param {string} [stack] - The error stack trace. | ||
*/ | ||
constructor(code: string, message: string, stack?: string) { | ||
super(message); | ||
this.code = code; | ||
this.name = this.constructor.name; | ||
this.stack = stack; | ||
} | ||
} |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/** | ||
* Interface for the Authn API request body. | ||
*/ | ||
export interface AuthApiRequestBody { | ||
/** | ||
* The authentication flow id. | ||
*/ | ||
flowId: string; | ||
/** | ||
* Contains selected authenticator's required details. | ||
*/ | ||
selectedAuthenticator: SelectedAuthenticator; | ||
} | ||
|
||
/** | ||
* Interface for the selected authenticator. | ||
*/ | ||
export interface SelectedAuthenticator { | ||
/** | ||
* The authentication authenticator id. | ||
*/ | ||
authenticatorId: string; | ||
/** | ||
*Required parameters for the selected authenticator. | ||
*/ | ||
params?: Record<string, string>; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.