forked from jumpserver/koko
/
login_confirm.go
157 lines (133 loc) · 3.74 KB
/
login_confirm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package auth
import (
"context"
"time"
"github.com/meowgen/koko/pkg/jms-sdk-go/model"
"github.com/meowgen/koko/pkg/jms-sdk-go/service"
"github.com/meowgen/koko/pkg/logger"
)
type connectionConfirmOption struct {
user *model.User
systemUser *model.SystemUserAuthInfo
targetType string
targetID string
}
func NewLoginConfirm(jmsService *service.JMService, opts ...ConfirmOption) LoginConfirmService {
var option connectionConfirmOption
for _, setter := range opts {
setter(&option)
}
return LoginConfirmService{option: &option, jmsService: jmsService}
}
type LoginConfirmService struct {
jmsService *service.JMService
option *connectionConfirmOption
checkReqInfo model.ReqInfo
cancelReqInfo model.ReqInfo
reviewers []string
ticketDetailUrl string
processor string // 此审批的处理人
ticketId string // 此工单 Id
}
func (c *LoginConfirmService) CheckIsNeedLoginConfirm() (bool, error) {
userID := c.option.user.ID
systemUserID := c.option.systemUser.ID
systemUsername := c.option.systemUser.Username
targetID := c.option.targetID
switch c.option.targetType {
case model.AppType:
return c.jmsService.CheckIfNeedAppConnectionConfirm(userID, targetID, systemUserID)
default:
res, err := c.jmsService.CheckIfNeedAssetLoginConfirm(userID, targetID,
systemUserID, systemUsername)
if err != nil {
return false, err
}
c.ticketId = res.TicketId
c.reviewers = res.Reviewers
c.checkReqInfo = res.CheckReq
c.cancelReqInfo = res.CloseReq
c.ticketDetailUrl = res.TicketDetailUrl
return res.NeedConfirm, nil
}
}
func (c *LoginConfirmService) WaitLoginConfirm(ctx context.Context) Status {
return c.waitConfirmFinish(ctx)
}
func (c *LoginConfirmService) GetReviewers() []string {
reviewers := make([]string, len(c.reviewers))
copy(reviewers, c.reviewers)
return reviewers
}
func (c *LoginConfirmService) GetTicketUrl() string {
return c.ticketDetailUrl
}
func (c *LoginConfirmService) GetProcessor() string {
return c.processor
}
func (c *LoginConfirmService) GetTicketId() string {
return c.ticketId
}
func (c *LoginConfirmService) waitConfirmFinish(ctx context.Context) Status {
// 10s 请求一次
t := time.NewTicker(10 * time.Second)
defer t.Stop()
for {
select {
case <-ctx.Done():
c.cancelConfirm()
return StatusCancel
case <-t.C:
statusRes, err := c.jmsService.CheckConfirmStatusByRequestInfo(c.checkReqInfo)
if err != nil {
logger.Errorf("Check confirm status err: %s", err.Error())
continue
}
switch statusRes.State {
case model.TicketOpen:
continue
case model.TicketApproved:
c.processor = statusRes.Processor
return StatusApprove
case model.TicketRejected, model.TicketClosed:
c.processor = statusRes.Processor
return StatusReject
default:
logger.Errorf("Receive unknown login confirm status %s",
statusRes.Status)
}
}
}
}
func (c *LoginConfirmService) cancelConfirm() {
if err := c.jmsService.CancelConfirmByRequestInfo(c.cancelReqInfo); err != nil {
logger.Errorf("Cancel confirm request err: %s", err.Error())
}
}
type Status int
const (
StatusApprove Status = iota + 1
StatusReject
StatusCancel
)
type ConfirmOption func(*connectionConfirmOption)
func ConfirmWithUser(user *model.User) ConfirmOption {
return func(option *connectionConfirmOption) {
option.user = user
}
}
func ConfirmWithSystemUser(sysUser *model.SystemUserAuthInfo) ConfirmOption {
return func(option *connectionConfirmOption) {
option.systemUser = sysUser
}
}
func ConfirmWithTargetType(targetType string) ConfirmOption {
return func(option *connectionConfirmOption) {
option.targetType = targetType
}
}
func ConfirmWithTargetID(targetID string) ConfirmOption {
return func(option *connectionConfirmOption) {
option.targetID = targetID
}
}