Skip to content

Commit 87e4501

Browse files
committed
feat(javascript): add a identifyPlatform util to detect the identity platform
1 parent 8f70f57 commit 87e4501

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

packages/javascript/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Platform } from './models/platforms';
12
/**
23
* Copyright (c) 2020, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
34
*
@@ -57,6 +58,7 @@ export {default as AsgardeoRuntimeError} from './errors/AsgardeoRuntimeError';
5758
export {AsgardeoAuthException} from './errors/exception';
5859

5960
export {AllOrganizationsApiResponse} from './models/organization';
61+
export {Platform} from './models/platforms';
6062
export {
6163
EmbeddedSignInFlowInitiateResponse,
6264
EmbeddedSignInFlowStatus,
@@ -142,6 +144,7 @@ export {default as generateUserProfile} from './utils/generateUserProfile';
142144
export {default as getLatestStateParam} from './utils/getLatestStateParam';
143145
export {default as generateFlattenedUserProfile} from './utils/generateFlattenedUserProfile';
144146
export {default as getI18nBundles} from './utils/getI18nBundles';
147+
export {default as identifyPlatform} from './utils/identifyPlatform';
145148
export {default as isEmpty} from './utils/isEmpty';
146149
export {default as set} from './utils/set';
147150
export {default as get} from './utils/get';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
/**
20+
* Enumeration of supported identity platforms.
21+
*
22+
* - `Asgardeo`: Represents the Asgardeo cloud identity platform (asgardeo.io domains).
23+
* - `IdentityServer`: Represents WSO2 Identity Server (on-prem or custom domains).
24+
* - `Unknown`: Used when the platform cannot be determined from the configuration.
25+
*
26+
* This enum is used to distinguish between different identity providers and to
27+
* enable platform-specific logic throughout the SDK.
28+
*/
29+
export enum Platform {
30+
/** Asgardeo cloud identity platform (asgardeo.io domains) */
31+
Asgardeo = 'ASGARDEO',
32+
/** WSO2 Identity Server (on-prem or custom domains) */
33+
IdentityServer = 'IDENTITY_SERVER',
34+
/** Unknown or unsupported platform */
35+
Unknown = 'UNKNOWN',
36+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
import {describe, it, expect, vi, afterEach} from 'vitest';
20+
import identifyPlatform from '../identifyPlatform';
21+
import {Platform} from '../../models/platforms';
22+
import {Config} from '../../models/config';
23+
24+
vi.mock('../logger', () => ({default: {debug: vi.fn(), warn: vi.fn()}}));
25+
26+
describe('identifyPlatform', () => {
27+
afterEach(() => {
28+
vi.clearAllMocks();
29+
});
30+
31+
it('should return Platform.Asgardeo for recognized asgardeo domains', () => {
32+
const configs: Config[] = [
33+
{baseUrl: 'https://api.asgardeo.io/t/org', clientId: '', applicationId: ''},
34+
{baseUrl: 'https://accounts.asgardeo.io/t/org', clientId: '', applicationId: ''},
35+
{baseUrl: 'https://asgardeo.io/t/org', clientId: '', applicationId: ''},
36+
];
37+
38+
configs.forEach(config => {
39+
expect(identifyPlatform(config)).toBe(Platform.Asgardeo);
40+
});
41+
});
42+
43+
it('should return Platform.IdentityServer for non-asgardeo recognized base Urls', () => {
44+
const configs: Config[] = [
45+
{baseUrl: 'https://localhost:9443/t/carbon.super', clientId: '', applicationId: ''},
46+
{baseUrl: 'https://is.dev.com/t/abc.com', clientId: '', applicationId: ''},
47+
{baseUrl: 'https://192.168.1.1/t/mytenant', clientId: '', applicationId: ''},
48+
];
49+
50+
configs.forEach(config => {
51+
expect(identifyPlatform(config)).toBe(Platform.IdentityServer);
52+
});
53+
});
54+
55+
it('should return Platform.IdentityServer if baseUrl is not recognized', () => {
56+
const config: Config = {baseUrl: undefined, clientId: '', applicationId: ''};
57+
58+
expect(identifyPlatform(config)).toBe(Platform.Unknown);
59+
});
60+
61+
it('should return Platform.IdentityServer if baseUrl is malformed', () => {
62+
const config: Config = {baseUrl: 'http://[::1', clientId: '', applicationId: ''};
63+
64+
expect(identifyPlatform(config)).toBe(Platform.Unknown);
65+
});
66+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com). All Rights Reserved.
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
import {Config} from '../models/config';
20+
import {Platform} from '../models/platforms';
21+
import isRecognizedBaseUrlPattern from './isRecognizedBaseUrlPattern';
22+
import logger from './logger';
23+
24+
/**
25+
* Identifies the platform based on the given base URL.
26+
*
27+
* If the URL is recognized and matches the Asgardeo domain, returns Platform.Asgardeo.
28+
* Otherwise, returns Platform.IdentityServer.
29+
*
30+
* @param baseUrl - The base URL to check
31+
* @returns Platform enum value
32+
*/
33+
const identifyPlatform = (config: Config): Platform => {
34+
const {baseUrl} = config;
35+
36+
try {
37+
if (isRecognizedBaseUrlPattern(baseUrl)) {
38+
try {
39+
const url = new URL(baseUrl!);
40+
// Check for asgardeo domain (e.g., api.asgardeo.io, etc.)
41+
if (/\.asgardeo\.io$/i.test(url.hostname) || /asgardeo\.io$/i.test(url.hostname)) {
42+
return Platform.Asgardeo;
43+
}
44+
} catch {
45+
// Fallback to IdentityServer if URL parsing fails.
46+
logger.debug(
47+
`[identifyPlatform] Could not identify platform from the base URL: ${baseUrl}. Defaulting to WSO2 Identity Server as the platform.`,
48+
);
49+
}
50+
51+
return Platform.IdentityServer;
52+
}
53+
} catch (error) {
54+
logger.debug(`[identifyPlatform] Error identifying platform from base URL: ${baseUrl}. Error: ${error.message}`);
55+
56+
return Platform.Unknown;
57+
}
58+
};
59+
60+
export default identifyPlatform;

0 commit comments

Comments
 (0)