mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00:00
fix: token parsing
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user