Skip to content
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

feat: improve auth URL generation and add extra params. #49

Merged
merged 12 commits into from
Dec 16, 2024
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ npm run build
To test the SDK, you just need to run the command:

```bash
npm run test
npm run test;
```

## Documentation
Expand Down
113 changes: 72 additions & 41 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ import InAppBrowser from 'react-native-inappbrowser-reborn';
import Url from 'url-parse';
import RNStorage from '../src/SDK/Storage/RNStorage';
import { openWebBrowser } from '../src/SDK/Utils';
import Storage from '../src/SDK/Storage';

const crypto = require('crypto');

Object.defineProperty(globalThis, 'crypto', {
value: {
getRandomValues: (arr) => crypto.randomBytes(arr.length),
subtle: {
digest: (algorithm, data) => {
return new Promise((resolve) => {
const hash = crypto.createHash(
algorithm.replace('-', '').toLowerCase()
);
hash.update(Buffer.from(data));
resolve(hash.digest());
});
}
}
}
});

const fakeTokenResponse = {
access_token: 'this_is_access_token',
Expand Down Expand Up @@ -83,6 +103,11 @@ jest.mock(process.cwd() + '/src/SDK/Utils', () => ({
generateRandomString: jest
.fn()
.mockReturnValue('uUj8nEDL-jxeDbS_si86i7UsFmG5ewf0axDu96pdHGc'),
isAdditionalParameters: jest.requireActual(process.cwd() + '/src/SDK/Utils')
.isAdditionalParameters,
additionalParametersToLoginMethodParams: jest.requireActual(
process.cwd() + '/src/SDK/Utils'
).additionalParametersToLoginMethodParams,
checkNotNull: jest.fn((reference, name) => {
if (reference === null || reference === undefined) {
throw new Error(`${name} cannot be empty`);
Expand Down Expand Up @@ -239,21 +264,27 @@ describe('KindeSDK', () => {
InAppBrowser.isAvailable = jest.fn().mockReturnValue(true);
await globalClient.login();

const urlParmams = new URLSearchParams({
client_id: configuration.clientId,
redirect_uri: configuration.redirectUri,
client_secret: configuration.clientSecret || '',
scope: configuration.scope,
grant_type: 'authorization_code',
const state = Storage.getState();
const codeVerifier = Storage.getCodeVerifier();
const codeChallenge = Storage.getCodeChallenge();
const nonce = Storage.getNonce();

const urlParsed = new URLSearchParams({
client_id: configuration.clientId || '',
response_type: 'code',
start_page: 'login',
state: configuration.fakeState,
code_challenge: configuration.fakeCodeChallenge,
redirect_uri: configuration.redirectUri || '',
audience: '',
scope: configuration.scope || '',
prompt: 'login',
state: state,
nonce: nonce,
code_challenge: codeChallenge,
code_challenge_method: 'S256'
}).toString();

expect(InAppBrowser.openAuth).toHaveBeenCalledWith(
`${configuration.issuer}/oauth2/auth?${urlParmams}`,
`${configuration.issuer}/oauth2/auth?${urlParsed}`,
globalClient.redirectUri,
{
enableDefaultShare: false,
Expand All @@ -268,35 +299,29 @@ describe('KindeSDK', () => {
InAppBrowser.isAvailable = jest.fn().mockReturnValue(true);
await globalClient.register();

console.log(
new URLSearchParams({
client_id: configuration.clientId || '',
redirect_uri: configuration.redirectUri || '',
client_secret: configuration.clientSecret || '',
scope: configuration.scope || '',
grant_type: 'authorization_code',
response_type: 'code',
start_page: 'registration',
state: configuration.fakeState || '',
code_challenge: configuration.fakeCodeChallenge || '',
code_challenge_method: 'S256'
}).toString()
);
const state = Storage.getState();
const codeVerifier = Storage.getCodeVerifier();
const codeChallenge = Storage.getCodeChallenge();
const nonce = Storage.getNonce();

const urlParsed = new URLSearchParams({
client_id: configuration.clientId || '',
response_type: 'code',
start_page: 'registration',
redirect_uri: configuration.redirectUri || '',
audience: '',
scope: configuration.scope || '',
prompt: 'registration',
state: state,
nonce: nonce,
code_challenge: codeChallenge,
code_challenge_method: 'S256'
}).toString();

expect(InAppBrowser.openAuth).toHaveBeenCalledWith(
configuration.authorizationEndpoint +
'?' +
new URLSearchParams({
client_id: configuration.clientId || '',
redirect_uri: configuration.redirectUri || '',
client_secret: configuration.clientSecret || '',
scope: configuration.scope || '',
grant_type: 'authorization_code',
response_type: 'code',
start_page: 'registration',
state: configuration.fakeState || '',
code_challenge: configuration.fakeCodeChallenge || '',
code_challenge_method: 'S256'
}).toString(),
urlParsed.toString(),
globalClient.redirectUri,
{
enableDefaultShare: false,
Expand Down Expand Up @@ -361,17 +386,23 @@ describe('KindeSDK', () => {
InAppBrowser.isAvailable = jest.fn().mockReturnValue(true);
await globalClient.createOrg();

const state = Storage.getState();
const codeVerifier = Storage.getCodeVerifier();
const codeChallenge = Storage.getCodeChallenge();
const nonce = Storage.getNonce();

const urlParsed = new URLSearchParams({
client_id: configuration.clientId || '',
redirect_uri: configuration.redirectUri || '',
client_secret: configuration.clientSecret || '',
scope: configuration.scope || '',
grant_type: 'authorization_code',
response_type: 'code',
start_page: 'registration',
state: configuration.fakeState || '',
is_create_org: true,
code_challenge: configuration.fakeCodeChallenge || '',
redirect_uri: configuration.redirectUri || '',
audience: '',
scope: configuration.scope || '',
prompt: 'registration',
state: state,
nonce: nonce,
code_challenge: codeChallenge,
code_challenge_method: 'S256'
}).toString();

Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"bugs": "https://github.com/kinde-oss/kinde-react-native-sdk-0-7x",
"main": "dist/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
"build": "rimraf dist && npm run build:types && npm run build:js",
"build:types": "tsc --emitDeclarationOnly",
Expand Down Expand Up @@ -38,10 +39,12 @@
],
"dependencies": {
"@babel/runtime": "^7.19.4",
"@kinde/js-utils": "^0.5.1-5",
"crypto-js": "3.3.0",
"jwt-decode": "^3.1.2",
"react-native-inappbrowser-reborn": "^3.7.0",
"react-native-keychain": ">= 8.0"
"react-native-inappbrowser-reborn": ">= 3.7",
"react-native-keychain": ">= 8.0",
"url-parse": "^1.5.10"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
Expand Down Expand Up @@ -88,8 +91,8 @@
},
"peerDependencies": {
"react-native": ">= 0.70",
"react-native-keychain": ">= 8.0",
"react-native-inappbrowser-reborn": ">= 3.7"
"react-native-inappbrowser-reborn": ">= 3.7",
"react-native-keychain": ">= 8.0"
},
"files": [
"dist"
Expand Down Expand Up @@ -123,5 +126,6 @@
"transformIgnorePatterns": [
"/node_modules/(?!(@react-native|react-native)/).*/"
]
}
},
"packageManager": "[email protected]+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}
13 changes: 0 additions & 13 deletions src/ApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
/**
* Kinde Management API
* Provides endpoints to manage your Kinde Businesses
*
* The version of the OpenAPI document: 1
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*
*/

import Storage from './SDK/Storage';
export const BASE_PATH = 'https://your_kinde_domain.kinde.com/api/v1'.replace(
/\/+$/,
Expand Down
19 changes: 0 additions & 19 deletions src/SDK/Enums/TokenType.enum.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
/**
* Kinde Management API
* Provides endpoints to manage your Kinde Businesses
*
* The version of the OpenAPI document: 1
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*
*/

/**
* The Enums SDK module.
* @module SDK/Enums
* @version 1.2.2
*/

export enum TokenType {
ACCESS_TOKEN = 'access_token',
ID_TOKEN = 'id_token'
Expand Down
Loading
Loading