|
| 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 | +} |
0 commit comments