mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-23 21:06:22 +08:00
fix: token parsing
This commit is contained in:
@@ -22,9 +22,9 @@
|
|||||||
"@playwright/test": "^1.29.1",
|
"@playwright/test": "^1.29.1",
|
||||||
"@types/debug": "^4.1.7",
|
"@types/debug": "^4.1.7",
|
||||||
"fake-indexeddb": "4.0.1",
|
"fake-indexeddb": "4.0.1",
|
||||||
|
"lit": "^2.6.1",
|
||||||
"typescript": "^4.9.5",
|
"typescript": "^4.9.5",
|
||||||
"yjs": "^13.5.45",
|
"yjs": "^13.5.45"
|
||||||
"lit": "^2.6.1"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@blocksuite/blocks": "0.4.0-alpha.2",
|
"@blocksuite/blocks": "0.4.0-alpha.2",
|
||||||
@@ -33,6 +33,7 @@
|
|||||||
"encoding": "^0.1.13",
|
"encoding": "^0.1.13",
|
||||||
"firebase": "^9.15.0",
|
"firebase": "^9.15.0",
|
||||||
"idb-keyval": "^6.2.0",
|
"idb-keyval": "^6.2.0",
|
||||||
|
"js-base64": "^3.7.5",
|
||||||
"ky": "^0.33.0",
|
"ky": "^0.33.0",
|
||||||
"ky-universal": "^0.11.0",
|
"ky-universal": "^0.11.0",
|
||||||
"lib0": "^0.2.58",
|
"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 { initializeApp } from 'firebase/app';
|
||||||
import { getAuth, GoogleAuthProvider, signInWithPopup } from 'firebase/auth';
|
import { getAuth, GoogleAuthProvider, signInWithPopup } from 'firebase/auth';
|
||||||
import type { User } from 'firebase/auth';
|
import type { User } from 'firebase/auth';
|
||||||
|
import { decode } from 'js-base64';
|
||||||
|
|
||||||
import { getLogger } from '../../../logger.js';
|
import { getLogger } from '../../../logger.js';
|
||||||
import { bareClient } from './request.js';
|
import { bareClient } from './request.js';
|
||||||
@@ -31,7 +32,7 @@ type LoginResponse = {
|
|||||||
const login = (params: LoginParams): Promise<LoginResponse> =>
|
const login = (params: LoginParams): Promise<LoginResponse> =>
|
||||||
bareClient.post('api/user/token', { json: params }).json();
|
bareClient.post('api/user/token', { json: params }).json();
|
||||||
|
|
||||||
class Token {
|
export class Token {
|
||||||
private readonly _logger;
|
private readonly _logger;
|
||||||
private _accessToken!: string;
|
private _accessToken!: string;
|
||||||
private _refreshToken!: string;
|
private _refreshToken!: string;
|
||||||
@@ -99,17 +100,9 @@ class Token {
|
|||||||
|
|
||||||
static parse(token: string): AccessTokenMessage | null {
|
static parse(token: string): AccessTokenMessage | null {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(
|
return JSON.parse(decode(token.split('.')[1]));
|
||||||
decodeURIComponent(
|
|
||||||
atob(token.split('.')[1])
|
|
||||||
.split('')
|
|
||||||
.map(function (c) {
|
|
||||||
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
|
||||||
})
|
|
||||||
.join('')
|
|
||||||
)
|
|
||||||
);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// todo: log errors?
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+6
@@ -142,6 +142,7 @@ importers:
|
|||||||
fake-indexeddb: 4.0.1
|
fake-indexeddb: 4.0.1
|
||||||
firebase: ^9.15.0
|
firebase: ^9.15.0
|
||||||
idb-keyval: ^6.2.0
|
idb-keyval: ^6.2.0
|
||||||
|
js-base64: ^3.7.5
|
||||||
ky: ^0.33.0
|
ky: ^0.33.0
|
||||||
ky-universal: ^0.11.0
|
ky-universal: ^0.11.0
|
||||||
lib0: ^0.2.58
|
lib0: ^0.2.58
|
||||||
@@ -157,6 +158,7 @@ importers:
|
|||||||
encoding: 0.1.13
|
encoding: 0.1.13
|
||||||
firebase: 9.15.0_encoding@0.1.13
|
firebase: 9.15.0_encoding@0.1.13
|
||||||
idb-keyval: 6.2.0
|
idb-keyval: 6.2.0
|
||||||
|
js-base64: 3.7.5
|
||||||
ky: 0.33.1
|
ky: 0.33.1
|
||||||
ky-universal: 0.11.0_ky@0.33.1
|
ky-universal: 0.11.0_ky@0.33.1
|
||||||
lib0: 0.2.58
|
lib0: 0.2.58
|
||||||
@@ -6315,6 +6317,10 @@ packages:
|
|||||||
supports-color: 8.1.1
|
supports-color: 8.1.1
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
|
/js-base64/3.7.5:
|
||||||
|
resolution: {integrity: sha512-3MEt5DTINKqfScXKfJFrRbxkrnk2AxPWGBL/ycjz4dK8iqiSJ06UxD8jh8xuh6p10TX4t2+7FsBYVxxQbMg+qA==}
|
||||||
|
dev: false
|
||||||
|
|
||||||
/js-sdsl/4.2.0:
|
/js-sdsl/4.2.0:
|
||||||
resolution: {integrity: sha512-dyBIzQBDkCqCu+0upx25Y2jGdbTGxE9fshMsCdK0ViOongpV+n5tXRcZY9v7CaVQ79AGS9KA1KHtojxiM7aXSQ==}
|
resolution: {integrity: sha512-dyBIzQBDkCqCu+0upx25Y2jGdbTGxE9fshMsCdK0ViOongpV+n5tXRcZY9v7CaVQ79AGS9KA1KHtojxiM7aXSQ==}
|
||||||
dev: true
|
dev: true
|
||||||
|
|||||||
Reference in New Issue
Block a user