Skip to content

Commit af7a03b

Browse files
committed
实现Ticket登录
1 parent 1658077 commit af7a03b

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

internal/rpc/rpc_client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ func (this *RPCClient) LoginSessionRPC() pb.LoginSessionServiceClient {
385385
return pb.NewLoginSessionServiceClient(this.pickConn())
386386
}
387387

388+
func (this *RPCClient) LoginTicketRPC() pb.LoginTicketServiceClient {
389+
return pb.NewLoginTicketServiceClient(this.pickConn())
390+
}
391+
388392
func (this *RPCClient) NodeTaskRPC() pb.NodeTaskServiceClient {
389393
return pb.NewNodeTaskServiceClient(this.pickConn())
390394
}

internal/web/actions/default/login/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ func init() {
99
server.
1010
Prefix("/login").
1111
GetPost("/validate", new(ValidateAction)).
12+
Get("/ticket", new(TicketAction)).
1213
EndAll()
1314
})
1415
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2024 GoEdge CDN [email protected]. All rights reserved. Official site: https://goedge.cn .
2+
3+
package login
4+
5+
import (
6+
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
7+
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/index/loginutils"
8+
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
9+
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
10+
"github.com/iwind/TeaGo/rands"
11+
)
12+
13+
type TicketAction struct {
14+
actionutils.ParentAction
15+
}
16+
17+
func (this *TicketAction) Init() {
18+
this.Nav("", "", "")
19+
}
20+
21+
func (this *TicketAction) RunGet(params struct {
22+
Ticket string
23+
Redirect string
24+
Auth *helpers.UserShouldAuth
25+
}) {
26+
this.Data["redirect"] = params.Redirect
27+
var errorMsg string
28+
29+
defer func() {
30+
this.Data["errorMsg"] = errorMsg
31+
this.Show()
32+
}()
33+
34+
if len(params.Ticket) == 0 {
35+
errorMsg = "invalid ticket: wrong format"
36+
return
37+
}
38+
39+
// TODO 对于错误尝试太多的IP进行处罚
40+
41+
resp, err := this.RPC().LoginTicketRPC().FindLoginTicketWithValue(this.AdminContext(), &pb.FindLoginTicketWithValueRequest{Value: params.Ticket})
42+
if err != nil {
43+
this.ErrorPage(err)
44+
return
45+
}
46+
47+
if resp.LoginTicket == nil {
48+
errorMsg = "invalid ticket: not found"
49+
return
50+
}
51+
52+
if resp.LoginTicket.AdminId <= 0 {
53+
errorMsg = "invalid ticket: invalid admin id"
54+
return
55+
}
56+
57+
var currentIP = loginutils.RemoteIP(&this.ActionObject)
58+
if len(resp.LoginTicket.Ip) > 0 && resp.LoginTicket.Ip != currentIP {
59+
errorMsg = "invalid ticket: wrong client ip"
60+
return
61+
}
62+
63+
var localSid = rands.HexString(32)
64+
this.Data["localSid"] = localSid
65+
this.Data["ip"] = currentIP
66+
67+
params.Auth.StoreAdmin(resp.LoginTicket.AdminId, false, localSid)
68+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="zh">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5+
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0">
6+
{$TEA.VUE}
7+
{$TEA.SEMANTIC}
8+
</head>
9+
<body>
10+
11+
<div class="ui message warning" v-if="errorMsg.length > 0">ERROR: {{errorMsg}}</div>
12+
13+
</body>
14+
</html>

web/views/@default/login/ticket.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Tea.context(function () {
2+
// store information to local
3+
localStorage.setItem("sid", this.localSid)
4+
localStorage.setItem("ip", this.ip)
5+
6+
if (this.errorMsg.length == 0) {
7+
if (this.redirect.length > 0) {
8+
window.location = this.redirect
9+
} else {
10+
window.location = "/dashboard"
11+
}
12+
}
13+
})

0 commit comments

Comments
 (0)