mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 15:37:33 +08:00
generate API token in frontend page
This commit is contained in:
@@ -19,6 +19,10 @@ export function isUserForgetPasswordEnabled(): boolean {
|
||||
return getServerSetting('f') === 1;
|
||||
}
|
||||
|
||||
export function isGenerateAPITokenEnabled(): boolean {
|
||||
return getServerSetting('t') === 1;
|
||||
}
|
||||
|
||||
export function isUserVerifyEmailEnabled(): boolean {
|
||||
return getServerSetting('v') === 1;
|
||||
}
|
||||
|
||||
@@ -112,8 +112,10 @@ import type {
|
||||
TransactionTemplateInfoResponse
|
||||
} from '@/models/transaction_template.ts';
|
||||
import type {
|
||||
TokenGenerateAPIRequest,
|
||||
TokenGenerateMCPRequest,
|
||||
TokenRevokeRequest,
|
||||
TokenGenerateAPIResponse,
|
||||
TokenGenerateMCPResponse,
|
||||
TokenRefreshResponse,
|
||||
TokenInfoResponse
|
||||
@@ -345,6 +347,9 @@ export default {
|
||||
getTokens: (): ApiResponsePromise<TokenInfoResponse[]> => {
|
||||
return axios.get<ApiResponse<TokenInfoResponse[]>>('v1/tokens/list.json');
|
||||
},
|
||||
generateAPIToken: (req: TokenGenerateAPIRequest): ApiResponsePromise<TokenGenerateAPIResponse> => {
|
||||
return axios.post<ApiResponse<TokenGenerateAPIResponse>>('v1/tokens/generate/api.json', req);
|
||||
},
|
||||
generateMCPToken: (req: TokenGenerateMCPRequest): ApiResponsePromise<TokenGenerateMCPResponse> => {
|
||||
return axios.post<ApiResponse<TokenGenerateMCPResponse>>('v1/tokens/generate/mcp.json', req);
|
||||
},
|
||||
|
||||
+26
-20
@@ -1,9 +1,10 @@
|
||||
import uaParser from 'ua-parser-js';
|
||||
|
||||
import {
|
||||
TOKEN_TYPE_MCP,
|
||||
TOKEN_CLI_USER_AGENT,
|
||||
type TokenInfoResponse,
|
||||
TOKEN_TYPE_API,
|
||||
TOKEN_TYPE_MCP,
|
||||
SessionDeviceType,
|
||||
SessionInfo
|
||||
} from '@/models/token.ts';
|
||||
|
||||
@@ -43,10 +44,6 @@ function parseUserAgent(ua: string): UserAgentInfo {
|
||||
};
|
||||
}
|
||||
|
||||
function isSessionUserAgentCreatedByCli(ua: string): boolean {
|
||||
return ua === TOKEN_CLI_USER_AGENT;
|
||||
}
|
||||
|
||||
function parseDeviceInfo(uaInfo: UserAgentInfo): string {
|
||||
if (!uaInfo) {
|
||||
return '';
|
||||
@@ -86,30 +83,39 @@ function parseDeviceInfo(uaInfo: UserAgentInfo): string {
|
||||
}
|
||||
|
||||
export function parseSessionInfo(token: TokenInfoResponse): SessionInfo {
|
||||
const isCreateForAPI = token.tokenType === TOKEN_TYPE_API;
|
||||
const isCreateForMCP = token.tokenType === TOKEN_TYPE_MCP;
|
||||
const isCreatedByCli = isSessionUserAgentCreatedByCli(token.userAgent);
|
||||
const uaInfo = parseUserAgent(token.userAgent);
|
||||
let deviceType = '';
|
||||
let deviceType: SessionDeviceType = SessionDeviceType.Default;
|
||||
let deviceName: string = 'Other Device';
|
||||
|
||||
if (isCreateForMCP) {
|
||||
deviceType = 'mcp';
|
||||
} else if (isCreatedByCli) {
|
||||
deviceType = 'cli';
|
||||
if (isCreateForAPI) {
|
||||
deviceType = SessionDeviceType.Api;
|
||||
deviceName = 'API Token';
|
||||
} else if (isCreateForMCP) {
|
||||
deviceType = SessionDeviceType.MCP;
|
||||
deviceName = 'MCP Token';
|
||||
} else {
|
||||
if (uaInfo && uaInfo.device) {
|
||||
if (uaInfo.device.type === 'mobile') {
|
||||
deviceType = 'phone';
|
||||
deviceType = SessionDeviceType.Phone;
|
||||
} else if (uaInfo.device.type === 'wearable') {
|
||||
deviceType = 'wearable';
|
||||
deviceType = SessionDeviceType.Wearable;
|
||||
} else if (uaInfo.device.type === 'tablet') {
|
||||
deviceType = 'tablet';
|
||||
deviceType = SessionDeviceType.Tablet;
|
||||
} else if (uaInfo.device.type === 'smarttv') {
|
||||
deviceType = 'tv';
|
||||
deviceType = SessionDeviceType.TV;
|
||||
} else {
|
||||
deviceType = 'default';
|
||||
deviceType = SessionDeviceType.Default;
|
||||
}
|
||||
} else {
|
||||
deviceType = 'default';
|
||||
deviceType = SessionDeviceType.Default;
|
||||
}
|
||||
|
||||
if (token.isCurrent) {
|
||||
deviceName = 'Current';
|
||||
} else {
|
||||
deviceName = 'Other Device';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,8 +123,8 @@ export function parseSessionInfo(token: TokenInfoResponse): SessionInfo {
|
||||
token.tokenId,
|
||||
token.isCurrent,
|
||||
deviceType,
|
||||
isCreateForMCP || isCreatedByCli ? token.userAgent : parseDeviceInfo(uaInfo),
|
||||
isCreatedByCli,
|
||||
isCreateForAPI || isCreateForMCP ? token.userAgent : parseDeviceInfo(uaInfo),
|
||||
deviceName,
|
||||
token.lastSeen
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user