Skip to content

Commit 8398134

Browse files
committed
enhance: 2FA is no longer required for the first 3min of login
1 parent 53c57b2 commit 8398134

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

api/user/auth.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ const (
3232
)
3333

3434
type LoginResponse struct {
35-
Message string `json:"message"`
36-
Error string `json:"error,omitempty"`
37-
Code int `json:"code"`
38-
Token string `json:"token,omitempty"`
35+
Message string `json:"message"`
36+
Error string `json:"error,omitempty"`
37+
Code int `json:"code"`
38+
Token string `json:"token,omitempty"`
39+
SecureSessionID string `json:"secure_session_id,omitempty"`
3940
}
4041

4142
func Login(c *gin.Context) {
@@ -86,6 +87,8 @@ func Login(c *gin.Context) {
8687
}
8788

8889
// Check if the user enables 2FA
90+
var secureSessionID string
91+
8992
if u.EnabledOTP() {
9093
if json.OTP == "" && json.RecoveryCode == "" {
9194
c.JSON(http.StatusOK, LoginResponse{
@@ -104,6 +107,8 @@ func Login(c *gin.Context) {
104107
user.BanIP(clientIP)
105108
return
106109
}
110+
111+
secureSessionID = user.SetSecureSessionID(u.ID)
107112
}
108113

109114
// login success, clear banned record
@@ -119,9 +124,10 @@ func Login(c *gin.Context) {
119124
}
120125

121126
c.JSON(http.StatusOK, LoginResponse{
122-
Code: LoginSuccess,
123-
Message: "ok",
124-
Token: token,
127+
Code: LoginSuccess,
128+
Message: "ok",
129+
Token: token,
130+
SecureSessionID: secureSessionID,
125131
})
126132
}
127133

app/src/api/auth.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export interface AuthResponse {
77
message: string
88
token: string
99
code: number
10+
error: string
11+
secure_session_id: string
1012
}
1113

1214
const auth = {

app/src/lib/websocket/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function ws(url: string, reconnect: boolean = true): ReconnectingWebSocket | Web
1616
url, `?token=${btoa(token.value)}`, node_id)
1717

1818
if (reconnect)
19-
return new ReconnectingWebSocket(_url)
19+
return new ReconnectingWebSocket(_url, undefined, { maxRetries: 10 })
2020

2121
return new WebSocket(_url)
2222
}

app/src/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"2.0.0-beta.29","build_id":151,"total_build":355}
1+
{"version":"2.0.0-beta.29","build_id":152,"total_build":356}

app/src/views/other/Login.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
22
import { LockOutlined, UserOutlined } from '@ant-design/icons-vue'
33
import { Form, message } from 'ant-design-vue'
4+
import { useCookies } from '@vueuse/integrations/useCookies'
45
import { useUserStore } from '@/pinia'
56
import auth from '@/api/auth'
67
import install from '@/api/install'
@@ -46,19 +47,23 @@ const rulesRef = reactive({
4647
})
4748
4849
const { validate, validateInfos, clearValidate } = Form.useForm(modelRef, rulesRef)
49-
const { login } = useUserStore()
50+
const userStore = useUserStore()
51+
const { login } = userStore
52+
const { secureSessionId } = storeToRefs(userStore)
5053
5154
const onSubmit = () => {
5255
validate().then(async () => {
5356
loading.value = true
5457
5558
await auth.login(modelRef.username, modelRef.password, passcode.value, recoveryCode.value).then(async r => {
5659
const next = (route.query?.next || '').toString() || '/'
57-
60+
const cookies = useCookies(['nginx-ui-2fa'])
5861
switch (r.code) {
5962
case 200:
6063
message.success($gettext('Login successful'), 1)
6164
login(r.token)
65+
secureSessionId.value = r.secure_session_id
66+
cookies.set('secure_session_id', r.secure_session_id, { maxAge: 60 * 3 })
6267
await router.push(next)
6368
break
6469
case 199:

app/src/views/system/About.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<script setup lang="ts">
22
import GithubButton from 'vue-github-button'
33
import logo from '@/assets/img/logo.png'
4-
import version from '@/version.json'
4+
import ver from '@/version.json'
55
6-
const this_year = new Date().getFullYear()
6+
const thisYear = new Date().getFullYear()
77
</script>
88

99
<template>
@@ -19,7 +19,7 @@ const this_year = new Date().getFullYear()
1919
</div>
2020
<h2>Nginx UI</h2>
2121
<p>Yet another WebUI for Nginx</p>
22-
<p>Version: {{ version.version }} ({{ version.build_id || $gettext('Development Mode') }})</p>
22+
<p>Version: {{ ver.version }} ({{ ver.build_id || $gettext('Development Mode') }})</p>
2323
<div class="star-on-github">
2424
<GithubButton
2525
href="https://github.com/0xJacky/nginx-ui"
@@ -47,7 +47,7 @@ const this_year = new Date().getFullYear()
4747
{{ $gettext('License') }}
4848
</h3>
4949
<p>GNU General Public License v3.0</p>
50-
<p>Copyright © 2021 - {{ this_year }} Nginx UI Team</p>
50+
<p>Copyright © 2021 - {{ thisYear }} Nginx UI Team</p>
5151
</ACard>
5252
</template>
5353

0 commit comments

Comments
 (0)