-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add expiration time setting for API key #7584
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,15 @@ package middleware | |
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
"net" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" | ||
"github.com/1Panel-dev/1Panel/backend/app/repo" | ||
"github.com/1Panel-dev/1Panel/backend/constant" | ||
"github.com/1Panel-dev/1Panel/backend/global" | ||
"github.com/gin-gonic/gin" | ||
"net" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func SessionAuth() gin.HandlerFunc { | ||
|
@@ -25,6 +25,11 @@ func SessionAuth() gin.HandlerFunc { | |
if panelToken != "" || panelTimestamp != "" { | ||
if global.CONF.System.ApiInterfaceStatus == "enable" { | ||
clientIP := c.ClientIP() | ||
if !isValid1PanelTimestamp(panelTimestamp) { | ||
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrApiConfigKeyTimeInvalid, nil) | ||
return | ||
} | ||
|
||
if !isValid1PanelToken(panelToken, panelTimestamp) { | ||
helper.ErrorWithDetail(c, constant.CodeErrUnauthorized, constant.ErrApiConfigKeyInvalid, nil) | ||
return | ||
|
@@ -63,6 +68,23 @@ func SessionAuth() gin.HandlerFunc { | |
} | ||
} | ||
|
||
func isValid1PanelTimestamp(panelTimestamp string) bool { | ||
apiKeyValidityTime := global.CONF.System.ApiKeyValidityTime | ||
apiTime, err := strconv.Atoi(apiKeyValidityTime) | ||
if err != nil { | ||
return false | ||
} | ||
panelTime, err := strconv.ParseInt(panelTimestamp, 10, 64) | ||
if err != nil { | ||
return false | ||
} | ||
nowTime := time.Now().Unix() | ||
if panelTime > nowTime { | ||
return false | ||
} | ||
return apiTime == 0 || nowTime-panelTime <= int64(apiTime*60) | ||
} | ||
|
||
func isValid1PanelToken(panelToken string, panelTimestamp string) bool { | ||
system1PanelToken := global.CONF.System.ApiKey | ||
if panelToken == GenerateMD5("1panel"+system1PanelToken+panelTimestamp) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code Differences Analysis:Changes Identified:
Conclusion:The code overall maintains functionality with minor adjustments made to enhance readability and maintainability. The new |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,17 @@ | |
/> | ||
<span class="input-help">{{ $t('setting.ipWhiteListHelper') }}</span> | ||
</el-form-item> | ||
<el-form-item :label="$t('setting.apiKeyValidityTime')" prop="apiKeyValidityTime"> | ||
<el-input | ||
:placeholder="$t('setting.apiKeyValidityTimeEgs')" | ||
v-model="form.apiKeyValidityTime" | ||
> | ||
<template #append>{{ $t('commons.units.minute') }}</template> | ||
</el-input> | ||
<span class="input-help"> | ||
{{ $t('setting.apiKeyValidityTimeHelper') }} | ||
</span> | ||
</el-form-item> | ||
</el-col> | ||
</el-row> | ||
</el-form> | ||
|
@@ -103,17 +114,20 @@ const form = reactive({ | |
apiKey: '', | ||
ipWhiteList: '', | ||
apiInterfaceStatus: '', | ||
apiKeyValidityTime: 120, | ||
}); | ||
|
||
const rules = reactive({ | ||
ipWhiteList: [Rules.requiredInput, { validator: checkIPs, trigger: 'blur' }], | ||
apiKey: [Rules.requiredInput], | ||
apiKeyValidityTime: [Rules.requiredInput, Rules.integerNumberWith0], | ||
}); | ||
|
||
interface DialogProps { | ||
apiInterfaceStatus: string; | ||
apiKey: string; | ||
ipWhiteList: string; | ||
apiKeyValidityTime: number; | ||
} | ||
|
||
function checkIPs(rule: any, value: any, callback: any) { | ||
|
@@ -146,6 +160,7 @@ const acceptParams = async (params: DialogProps): Promise<void> => { | |
}); | ||
} | ||
form.ipWhiteList = params.ipWhiteList; | ||
form.apiKeyValidityTime = params.apiKeyValidityTime; | ||
drawerVisible.value = true; | ||
}; | ||
|
||
|
@@ -179,6 +194,7 @@ const onSave = async (formEl: FormInstance | undefined) => { | |
apiKey: form.apiKey, | ||
ipWhiteList: form.ipWhiteList, | ||
apiInterfaceStatus: form.apiInterfaceStatus, | ||
apiKeyValidityTime: form.apiKeyValidityTime, | ||
}; | ||
loading.value = true; | ||
await updateApiConfig(param) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code has a few minor improvements and clarifications, but there are no obvious irregularities or issues. Here is a brief review:
Overall, the changes make the form more user-friendly by providing clear instructions on how to input data and ensuring consistency across similar fields. If there are specific performance optimizations needed based on your application's use case, feel free to ask! |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code snippet you provided does not contain any major irregularities, potential issues, or significant optimizations to suggest. There are just minor formatting updates made between the third and fourth line of code comments. The changes include adding an ellipsis (
...
) at the end of each comment to align with the previous comments' format. These changes do not affect the functionality of the code and are considered stylistic adjustments rather than functional improvements.If there is anything else specific you would like checked regarding this or other code snippets, please let me know!