Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
ea1681e1
authored
Apr 07, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: auth openapi key
parent
f6c4b4c9
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
37 additions
and
8 deletions
+37
-8
src/pages/api/openapi/chat/lafGpt.ts
+3
-3
src/pages/openapi/index.tsx
+5
-1
src/service/models/openapi.ts
+1
-2
src/service/utils/tools.ts
+26
-0
src/types/mongoSchema.d.ts
+1
-1
src/types/openapi.d.ts
+1
-1
No files found.
src/pages/api/openapi/lafGpt.ts
→
src/pages/api/openapi/
chat/
lafGpt.ts
View file @
ea1681e1
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'next'
;
import
{
connectToDatabase
,
Model
}
from
'@/service/mongo'
;
import
{
connectToDatabase
,
Model
}
from
'@/service/mongo'
;
import
{
getOpenAIApi
}
from
'@/service/utils/chat'
;
import
{
getOpenAIApi
}
from
'@/service/utils/chat'
;
import
{
auth
Token
}
from
'@/service/utils/tools'
;
import
{
auth
OpenApiKey
}
from
'@/service/utils/tools'
;
import
{
httpsAgent
,
openaiChatFilter
,
systemPromptFilter
}
from
'@/service/utils/tools'
;
import
{
httpsAgent
,
openaiChatFilter
,
systemPromptFilter
}
from
'@/service/utils/tools'
;
import
{
ChatCompletionRequestMessage
,
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
ChatCompletionRequestMessage
,
ChatCompletionRequestMessageRoleEnum
}
from
'openai'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
...
@@ -36,7 +36,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -36,7 +36,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
modelId
:
string
;
modelId
:
string
;
};
};
const
{
authorization
}
=
req
.
headers
;
if
(
!
prompt
)
{
if
(
!
prompt
)
{
throw
new
Error
(
'缺少参数'
);
throw
new
Error
(
'缺少参数'
);
}
}
...
@@ -46,7 +45,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -46,7 +45,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
let
startTime
=
Date
.
now
();
let
startTime
=
Date
.
now
();
/* 凭证校验 */
/* 凭证校验 */
const
userId
=
await
authToken
(
authorization
);
const
userId
=
await
authOpenApiKey
(
req
);
const
{
userApiKey
,
systemKey
}
=
await
getOpenApiKey
(
userId
);
const
{
userApiKey
,
systemKey
}
=
await
getOpenApiKey
(
userId
);
/* 查找数据库里的模型信息 */
/* 查找数据库里的模型信息 */
...
...
src/pages/openapi/index.tsx
View file @
ea1681e1
...
@@ -74,7 +74,11 @@ const OpenApi = () => {
...
@@ -74,7 +74,11 @@ const OpenApi = () => {
<
Tr
key=
{
id
}
>
<
Tr
key=
{
id
}
>
<
Td
>
{
apiKey
}
</
Td
>
<
Td
>
{
apiKey
}
</
Td
>
<
Td
>
{
dayjs
(
createTime
).
format
(
'YYYY/MM/DD HH:mm:ss'
)
}
</
Td
>
<
Td
>
{
dayjs
(
createTime
).
format
(
'YYYY/MM/DD HH:mm:ss'
)
}
</
Td
>
<
Td
>
{
dayjs
(
lastUsedTime
).
format
(
'YYYY/MM/DD HH:mm:ss'
)
}
</
Td
>
<
Td
>
{
lastUsedTime
?
dayjs
(
lastUsedTime
).
format
(
'YYYY/MM/DD HH:mm:ss'
)
:
'没有使用过'
}
</
Td
>
<
Td
>
<
Td
>
<
IconButton
<
IconButton
icon=
{
<
DeleteIcon
/>
}
icon=
{
<
DeleteIcon
/>
}
...
...
src/service/models/openapi.ts
View file @
ea1681e1
...
@@ -16,8 +16,7 @@ const OpenApiSchema = new Schema({
...
@@ -16,8 +16,7 @@ const OpenApiSchema = new Schema({
default
:
()
=>
new
Date
()
default
:
()
=>
new
Date
()
},
},
lastUsedTime
:
{
lastUsedTime
:
{
type
:
Date
,
type
:
Date
default
:
()
=>
new
Date
()
}
}
});
});
...
...
src/service/utils/tools.ts
View file @
ea1681e1
import
type
{
NextApiRequest
}
from
'next'
;
import
crypto
from
'crypto'
;
import
crypto
from
'crypto'
;
import
jwt
from
'jsonwebtoken'
;
import
jwt
from
'jsonwebtoken'
;
import
tunnel
from
'tunnel'
;
import
tunnel
from
'tunnel'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
ChatItemType
}
from
'@/types/chat'
;
import
{
encode
}
from
'gpt-token-utils'
;
import
{
encode
}
from
'gpt-token-utils'
;
import
{
OpenApi
}
from
'../mongo'
;
/* 密码加密 */
/* 密码加密 */
export
const
hashPassword
=
(
psw
:
string
)
=>
{
export
const
hashPassword
=
(
psw
:
string
)
=>
{
...
@@ -41,6 +43,30 @@ export const authToken = (token?: string): Promise<string> => {
...
@@ -41,6 +43,30 @@ export const authToken = (token?: string): Promise<string> => {
});
});
};
};
/* 校验 open api key */
export
const
authOpenApiKey
=
(
req
:
NextApiRequest
)
=>
{
return
new
Promise
<
string
>
(
async
(
resolve
,
reject
)
=>
{
const
{
apikey
:
apiKey
}
=
req
.
headers
;
if
(
!
apiKey
)
{
reject
(
'api key is empty'
);
return
;
}
try
{
const
openApi
=
await
OpenApi
.
findOne
({
apiKey
});
if
(
!
openApi
)
{
return
reject
(
'api key is error'
);
}
await
OpenApi
.
findByIdAndUpdate
(
openApi
.
_id
,
{
lastUsedTime
:
new
Date
()
});
resolve
(
String
(
openApi
.
userId
));
}
catch
(
error
)
{
reject
(
error
);
}
});
};
/* 代理 */
/* 代理 */
export
const
httpsAgent
=
export
const
httpsAgent
=
process
.
env
.
AXIOS_PROXY_HOST
&&
process
.
env
.
AXIOS_PROXY_PORT
process
.
env
.
AXIOS_PROXY_HOST
&&
process
.
env
.
AXIOS_PROXY_PORT
...
...
src/types/mongoSchema.d.ts
View file @
ea1681e1
...
@@ -153,6 +153,6 @@ export interface OpenApiSchema {
...
@@ -153,6 +153,6 @@ export interface OpenApiSchema {
_id
:
string
;
_id
:
string
;
userId
:
string
;
userId
:
string
;
createTime
:
Date
;
createTime
:
Date
;
lastUsedTime
:
Date
;
lastUsedTime
?
:
Date
;
apiKey
:
String
;
apiKey
:
String
;
}
}
src/types/openapi.d.ts
View file @
ea1681e1
...
@@ -2,5 +2,5 @@ export interface UserOpenApiKey {
...
@@ -2,5 +2,5 @@ export interface UserOpenApiKey {
id
:
string
;
id
:
string
;
apiKey
:
string
;
apiKey
:
string
;
createTime
:
Date
;
createTime
:
Date
;
lastUsedTime
:
Date
;
lastUsedTime
?
:
Date
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment