mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-12 23:56:36 +08:00
fix: token parsing
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+6
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user