Skip to content

Commit

Permalink
fix: unicode breaks with atob
Browse files Browse the repository at this point in the history
  • Loading branch information
coel committed Nov 17, 2023
1 parent 8e71bdf commit d69ee34
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
11 changes: 6 additions & 5 deletions lib/apis/ConnectedAppsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import {

export interface GetConnectedAppAuthUrlRequest {
keyCodeRef: string;
userId: string;
userId?: string;
orgCode?: string;
}

export interface GetConnectedAppTokenRequest {
Expand All @@ -58,10 +59,6 @@ export class ConnectedAppsApi extends runtime.BaseAPI {
throw new runtime.RequiredError('keyCodeRef','Required parameter requestParameters.keyCodeRef was null or undefined when calling getConnectedAppAuthUrl.');
}

if (requestParameters.userId === null || requestParameters.userId === undefined) {
throw new runtime.RequiredError('userId','Required parameter requestParameters.userId was null or undefined when calling getConnectedAppAuthUrl.');
}

const queryParameters: any = {};

if (requestParameters.keyCodeRef !== undefined) {
Expand All @@ -72,6 +69,10 @@ export class ConnectedAppsApi extends runtime.BaseAPI {
queryParameters['user_id'] = requestParameters.userId;
}

if (requestParameters.orgCode !== undefined) {
queryParameters['org_code'] = requestParameters.orgCode;
}

const headerParameters: runtime.HTTPHeaders = {};

if (this.configuration && this.configuration.accessToken) {
Expand Down
24 changes: 6 additions & 18 deletions lib/sdk/utilities/token-utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import { type JwtPayload, jwtDecode } from 'jwt-decode';
import type { TokenCollection, UserType, TokenType } from './types.js';
import { type SessionManager } from '../session-managers/index.js';

/**
* Parses a provided JWT token to extract the payload segment of said
* token.
* @param token {string}
* @returns {any}
*/
const getTokenPayload = (token: string): any => {
try {
return JSON.parse(atob(token.split('.')[1]));
} catch (e) {
return null;
}
};

/**
* Extracts the payload from the provided idToken and saves the extracted
Expand All @@ -26,13 +14,13 @@ const commitUserToMemoryFromToken = async (
sessionManager: SessionManager,
idToken: string
): Promise<void> => {
const idTokenPayload = getTokenPayload(idToken);
const idTokenPayload = jwtDecode<UserType&JwtPayload>(idToken);
const user: UserType = {
family_name: idTokenPayload.family_name,
given_name: idTokenPayload.given_name,
picture: idTokenPayload.picture ?? null,
email: idTokenPayload.email,
id: idTokenPayload.sub,
id: idTokenPayload.sub!,
};

await sessionManager.setSessionItem('user', user);
Expand All @@ -49,7 +37,7 @@ export const commitTokenToMemory = async (
token: string,
type: TokenType
): Promise<void> => {
const tokenPayload = getTokenPayload(token);
const tokenPayload = jwtDecode(token);
await sessionManager.setSessionItem(type, token);
if (type === 'access_token') {
await sessionManager.setSessionItem('access_token_payload', tokenPayload);
Expand Down Expand Up @@ -138,6 +126,6 @@ export const commitUserToMemory = async (
export const isTokenExpired = (token: string | null): boolean => {
if (!token) return true;
const currentUnixTime = Math.floor(Date.now() / 1000);
const tokenPayload = getTokenPayload(token);
return currentUnixTime >= tokenPayload.exp;
const tokenPayload = jwtDecode(token);
return currentUnixTime >= tokenPayload.exp!;
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kinde-oss/kinde-typescript-sdk",
"version": "2.2.3",
"version": "2.2.4",
"description": "Kinde Typescript SDK",
"main": "dist-cjs/index.js",
"module": "dist/index.js",
Expand All @@ -15,7 +15,7 @@
"types": "./dist-cjs/types/index.d.ts",
"default": "./dist-cjs/index.js"
},
"import":{
"import": {
"types": "./dist/types/index.d.ts",
"default": "./dist/index.js"
}
Expand Down Expand Up @@ -70,6 +70,7 @@
"typescript": "^5.0.4"
},
"dependencies": {
"jwt-decode": "^4.0.0",
"uncrypto": "^0.1.3"
}
}
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d69ee34

Please sign in to comment.