mirror of
https://github.com/labring/FastGPT.git
synced 2025-08-05 14:47:38 +00:00
monorepo packages (#344)
This commit is contained in:
38
projects/app/src/service/support/openapi/auth.ts
Normal file
38
projects/app/src/service/support/openapi/auth.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { ERROR_ENUM } from '@/service/errorCode';
|
||||
import { updateApiKeyUsedTime } from './index';
|
||||
import { OpenApi } from './schema';
|
||||
|
||||
export async function authOpenApiKey({ apikey }: { apikey: string }) {
|
||||
if (!apikey) {
|
||||
return Promise.reject(ERROR_ENUM.unAuthApiKey);
|
||||
}
|
||||
|
||||
try {
|
||||
const openApi = await OpenApi.findOne({ apiKey: apikey });
|
||||
if (!openApi) {
|
||||
return Promise.reject(ERROR_ENUM.unAuthApiKey);
|
||||
}
|
||||
const userId = String(openApi.userId);
|
||||
|
||||
// auth limit
|
||||
if (global.feConfigs?.isPlus) {
|
||||
if (openApi?.limit?.expiredTime && openApi.limit.expiredTime.getTime() < Date.now()) {
|
||||
return Promise.reject(`Key ${openApi.apiKey} is expired`);
|
||||
}
|
||||
|
||||
if (
|
||||
openApi?.limit?.credit &&
|
||||
openApi.limit.credit > -1 &&
|
||||
openApi.usage > openApi.limit.credit
|
||||
) {
|
||||
return Promise.reject(`Key ${openApi.apiKey} is over usage`);
|
||||
}
|
||||
}
|
||||
|
||||
updateApiKeyUsedTime(openApi._id);
|
||||
|
||||
return { apikey, userId, appId: openApi.appId };
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
18
projects/app/src/service/support/openapi/index.ts
Normal file
18
projects/app/src/service/support/openapi/index.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { OpenApi } from './schema';
|
||||
|
||||
export async function updateApiKeyUsedTime(id: string) {
|
||||
await OpenApi.findByIdAndUpdate(id, {
|
||||
lastUsedTime: new Date()
|
||||
});
|
||||
}
|
||||
|
||||
export async function updateApiKeyUsage({ apikey, usage }: { apikey: string; usage: number }) {
|
||||
await OpenApi.findOneAndUpdate(
|
||||
{ apiKey: apikey },
|
||||
{
|
||||
$inc: {
|
||||
usage
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
57
projects/app/src/service/support/openapi/schema.ts
Normal file
57
projects/app/src/service/support/openapi/schema.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { Schema, model, models, Model } from 'mongoose';
|
||||
import { OpenApiSchema } from '@/types/support/openapi';
|
||||
import { PRICE_SCALE } from '@fastgpt/common/bill/constants';
|
||||
import { formatPrice } from '@fastgpt/common/bill/index';
|
||||
|
||||
const OpenApiSchema = new Schema(
|
||||
{
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: 'user',
|
||||
required: true
|
||||
},
|
||||
apiKey: {
|
||||
type: String,
|
||||
required: true,
|
||||
get: (val: string) => `******${val.substring(val.length - 4)}`
|
||||
},
|
||||
createTime: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
},
|
||||
lastUsedTime: {
|
||||
type: Date
|
||||
},
|
||||
appId: {
|
||||
type: String,
|
||||
required: false
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
default: 'Api Key'
|
||||
},
|
||||
usage: {
|
||||
// total usage. value from bill total
|
||||
type: Number,
|
||||
default: 0,
|
||||
get: (val: number) => formatPrice(val)
|
||||
},
|
||||
limit: {
|
||||
expiredTime: {
|
||||
type: Date
|
||||
},
|
||||
credit: {
|
||||
// value from user settings
|
||||
type: Number,
|
||||
default: -1,
|
||||
set: (val: number) => val * PRICE_SCALE,
|
||||
get: (val: number) => formatPrice(val)
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
toObject: { getters: true }
|
||||
}
|
||||
);
|
||||
|
||||
export const OpenApi: Model<OpenApiSchema> = models['openapi'] || model('openapi', OpenApiSchema);
|
Reference in New Issue
Block a user