From 9c2c8aed6c9a908f9ecd479059dfff6e9710d5f2 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Wed, 8 Feb 2023 15:04:23 +0800 Subject: [PATCH] fix: token parsing --- packages/data-center/package.json | 5 +++-- .../affine/apis/__tests__/token.spec.ts | 21 +++++++++++++++++++ .../src/provider/affine/apis/token.ts | 15 ++++--------- pnpm-lock.yaml | 6 ++++++ 4 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 packages/data-center/src/provider/affine/apis/__tests__/token.spec.ts diff --git a/packages/data-center/package.json b/packages/data-center/package.json index 39086773d0..f2b7382a99 100644 --- a/packages/data-center/package.json +++ b/packages/data-center/package.json @@ -22,9 +22,9 @@ "@playwright/test": "^1.29.1", "@types/debug": "^4.1.7", "fake-indexeddb": "4.0.1", + "lit": "^2.6.1", "typescript": "^4.9.5", - "yjs": "^13.5.45", - "lit": "^2.6.1" + "yjs": "^13.5.45" }, "dependencies": { "@blocksuite/blocks": "0.4.0-alpha.2", @@ -33,6 +33,7 @@ "encoding": "^0.1.13", "firebase": "^9.15.0", "idb-keyval": "^6.2.0", + "js-base64": "^3.7.5", "ky": "^0.33.0", "ky-universal": "^0.11.0", "lib0": "^0.2.58", diff --git a/packages/data-center/src/provider/affine/apis/__tests__/token.spec.ts b/packages/data-center/src/provider/affine/apis/__tests__/token.spec.ts new file mode 100644 index 0000000000..739bae9e91 --- /dev/null +++ b/packages/data-center/src/provider/affine/apis/__tests__/token.spec.ts @@ -0,0 +1,21 @@ +import { test, expect } from '@playwright/test'; +import { Token } from '../token.js'; + +test.describe('class Token', () => { + test('parse tokens', () => { + const tokenString = `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2NzU2Nzk1MjAsImlkIjo2LCJuYW1lIjoidGVzdCIsImVtYWlsIjoidGVzdEBnbWFpbC5jb20iLCJhdmF0YXJfdXJsIjoiaHR0cHM6Ly90ZXN0LmNvbS9hdmF0YXIiLCJjcmVhdGVkX2F0IjoxNjc1Njc4OTIwMzU4fQ.R8GxrNhn3gNumtapthrP6_J5eQjXLV7i-LanSPqe7hw`; + expect(Token.parse(tokenString)).toEqual({ + avatar_url: 'https://test.com/avatar', + created_at: 1675678920358, + email: 'test@gmail.com', + exp: 1675679520, + id: 6, + name: 'test', + }); + }); + + test('parse invalid tokens', () => { + const tokenString = `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.aaa.R8GxrNhn3gNumtapthrP6_J5eQjXLV7i-LanSPqe7hw`; + expect(Token.parse(tokenString)).toEqual(null); + }); +}); diff --git a/packages/data-center/src/provider/affine/apis/token.ts b/packages/data-center/src/provider/affine/apis/token.ts index f002fedb9e..5b8e165929 100644 --- a/packages/data-center/src/provider/affine/apis/token.ts +++ b/packages/data-center/src/provider/affine/apis/token.ts @@ -1,6 +1,7 @@ import { initializeApp } from 'firebase/app'; import { getAuth, GoogleAuthProvider, signInWithPopup } from 'firebase/auth'; import type { User } from 'firebase/auth'; +import { decode } from 'js-base64'; import { getLogger } from '../../../logger.js'; import { bareClient } from './request.js'; @@ -31,7 +32,7 @@ type LoginResponse = { const login = (params: LoginParams): Promise => bareClient.post('api/user/token', { json: params }).json(); -class Token { +export class Token { private readonly _logger; private _accessToken!: string; private _refreshToken!: string; @@ -99,17 +100,9 @@ class Token { static parse(token: string): AccessTokenMessage | null { try { - return JSON.parse( - decodeURIComponent( - atob(token.split('.')[1]) - .split('') - .map(function (c) { - return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); - }) - .join('') - ) - ); + return JSON.parse(decode(token.split('.')[1])); } catch (error) { + // todo: log errors? return null; } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 982bdde6b4..59402862e9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -142,6 +142,7 @@ importers: fake-indexeddb: 4.0.1 firebase: ^9.15.0 idb-keyval: ^6.2.0 + js-base64: ^3.7.5 ky: ^0.33.0 ky-universal: ^0.11.0 lib0: ^0.2.58 @@ -157,6 +158,7 @@ importers: encoding: 0.1.13 firebase: 9.15.0_encoding@0.1.13 idb-keyval: 6.2.0 + js-base64: 3.7.5 ky: 0.33.1 ky-universal: 0.11.0_ky@0.33.1 lib0: 0.2.58 @@ -6315,6 +6317,10 @@ packages: supports-color: 8.1.1 dev: true + /js-base64/3.7.5: + resolution: {integrity: sha512-3MEt5DTINKqfScXKfJFrRbxkrnk2AxPWGBL/ycjz4dK8iqiSJ06UxD8jh8xuh6p10TX4t2+7FsBYVxxQbMg+qA==} + dev: false + /js-sdsl/4.2.0: resolution: {integrity: sha512-dyBIzQBDkCqCu+0upx25Y2jGdbTGxE9fshMsCdK0ViOongpV+n5tXRcZY9v7CaVQ79AGS9KA1KHtojxiM7aXSQ==} dev: true