Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
* 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
Expand Down
9 changes: 7 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@
},
"devDependencies": {
"@types/node": "^20.12.7",
"@wso2/eslint-plugin": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/eslint-plugin?5bf60cabe9e9a2571e8b1dd16d0c3bdc76db2c4f",
"@wso2/prettier-config": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/prettier-config?5bf60cabe9e9a2571e8b1dd16d0c3bdc76db2c4f",
"@wso2/eslint-plugin": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/eslint-plugin?fa0b844715320a3953d6d055997c0770f8695082",
"@wso2/prettier-config": "https://gitpkg.now.sh/brionmario/wso2-ui-configs/packages/prettier-config?fa0b844715320a3953d6d055997c0770f8695082",
"eslint": "~8.57.0",
"prettier": "^3.2.5",
"tslib": "^2.6.2",
"typescript": "^5.4.5"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@asgardeo/auth-js": "^5.0.1",
"@asgardeo/js-ui-core": "link:"
}
}
2 changes: 1 addition & 1 deletion packages/core/prettier.config.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
* 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
Expand Down
70 changes: 70 additions & 0 deletions packages/core/src/api/authenticate.ts
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(
'JS_UI_CORE-AUTHN-AN-HE',
'Failed to receive a successful response from the authentication endpoint',
);
};

export default authenticate;
77 changes: 77 additions & 0 deletions packages/core/src/api/authorize.ts
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';
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 {
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(
'UI_CORE-AUTHZ-A-HE',
'Failed to receive a successful response from the authorization server',
);
};

export default authorize;
55 changes: 55 additions & 0 deletions packages/core/src/auth-client.ts
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;
}
}

/* Interfaces, classes and enums required from the auth-js package */
export {CryptoUtils, JWKInterface, Store, AsgardeoAuthClient, TokenResponse, ResponseMode} from '@asgardeo/auth-js';
55 changes: 55 additions & 0 deletions packages/core/src/exception.ts
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 added packages/core/src/index.ts
Empty file.
45 changes: 45 additions & 0 deletions packages/core/src/models/auth-api-request.ts
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>;
}
Loading