There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.
TypeScript appcrdteditorelectronknowledge-basemarkdownmironotesnotionnotion-alternativerustrust-langrust-languagetabletableviewwhiteboardwikiworkspace
fix: token parsing
Peng Xiao9c2c8aed6c9a
diff --git a/packages/data-center/package.json b/packages/data-center/package.jsonindex 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.tsnew file mode 100644index 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: '[email protected]',+ 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.tsindex 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<LoginResponse> =>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.yamlindex 982bdde6b4..59402862e9 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -142,6 +142,7 @@ importers:fake-indexeddb: 4.0.1firebase: ^9.15.0idb-keyval: ^6.2.0+ js-base64: ^3.7.5ky: ^0.33.0ky-universal: ^0.11.0lib0: ^0.2.58@@ -157,6 +158,7 @@ importers:encoding: 0.1.13firebase: [email protected]idb-keyval: 6.2.0+ js-base64: 3.7.5ky: 0.33.1ky-universal: [email protected]lib0: 0.2.58@@ -6315,6 +6317,10 @@ packages:supports-color: 8.1.1dev: 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