-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_challenge_post.go
168 lines (142 loc) · 4.78 KB
/
http_challenge_post.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
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"errors"
"time"
"github.com/gofiber/fiber/v2"
)
func httpChallengePost(c *fiber.Ctx, config *config) error {
ip := c.Locals(localVarIP).(string)
requestID := c.Locals(localVarRequestID).(string)
temporaryChecksum := c.Locals(localVarClientTemporaryChecksum).(string)
var chReq challengeRequest
if parseError := c.BodyParser(&chReq); parseError != nil {
errorMessage := "invalid request"
defer config.getLogger().
Warn().
Str(logType, logTypeChallengeFailed).
Str(logPropertyIP, ip).
Str(logPropertyError, parseError.Error()).
Str(logPropertyRequestID, requestID).
Msg(errorMessage)
return errors.New(errorMessage)
}
challenge, challengeParseErr := newChallengeFromString(chReq.ChallengeToken, config.clientSecret)
if challengeParseErr != nil {
errorMessage := "invalid challenge token for parse"
defer config.getLogger().
Warn().
Str(logPropertyChallengeType, challenge.ChallengeType).
Str(logType, logTypeChallengeFailed).
Str(logPropertyIP, ip).
Str(logPropertyError, challengeParseErr.Error()).
Str(logPropertyRequestID, requestID).
Msg(errorMessage)
return errors.New(errorMessage)
}
if !challenge.verify(temporaryChecksum) {
errorMessage := "token invalid, timeout or expired"
defer config.getLogger().
Info().
Str(logPropertyChallengeType, challenge.ChallengeType).
Str(logType, logTypeChallengeFailed).
Str(logPropertyIP, ip).
Str(logPropertyRequestID, requestID).
Msg(errorMessage)
return errors.New(errorMessage)
}
valid := false
var ldapCookie *fiber.Cookie = nil
userName := ""
switch challenge.ChallengeType {
case challengeTypeJS:
valid = challenge.verifyJSValue(chReq.JSValue)
case challengeTypeCaptcha:
valid = challenge.verifyJSValue(chReq.JSValue) && challenge.verifyCaptchaValue(chReq.CaptchaValue)
case challengeTypeLDAP:
preValidation := challenge.verifyJSValue(chReq.JSValue) && challenge.verifyCaptchaValue(chReq.CaptchaValue)
if preValidation {
ldapURL := c.Locals(localVarLDAPURL).(string)
ldapReadonlyUsername := c.Locals(localVarLDAPReadonlyUsername).(string)
ldapReadonlyPassword := c.Locals(localVarLDAPReadonlyPassword).(string)
ldapBaseDN := c.Locals(localVarLDAPBaseDN).(string)
ldapFilter := c.Locals(localVarLDAPFilter).(string)
ldapAttributes := c.Locals(localVarLDAPAttributes).(string)
success, loginErr, ldapConfigError := ldapLogin(
ldapURL,
ldapReadonlyUsername,
ldapReadonlyPassword,
ldapBaseDN,
ldapFilter,
ldapAttributes,
chReq.LDAPUsername,
chReq.LDAPPassword,
)
if ldapConfigError != nil {
defer config.getLogger().
Error().
Str(logType, logTypeLDAPError).
Str(logPropertyError, ldapConfigError.Error()).
Str(logPropertyChallengeType, challenge.ChallengeType).
Str(logPropertyIP, ip).
Str(logPropertyRequestID, requestID).
Send()
} else if loginErr != nil {
defer config.getLogger().
Warn().
Str(logType, logTypeLDAPError).
Str(logPropertyError, loginErr.Error()).
Str(logPropertyChallengeType, challenge.ChallengeType).
Str(logPropertyIP, ip).
Str(logPropertyRequestID, requestID).
Send()
} else {
valid = success
userName = chReq.LDAPUsername
ldapCookie = &fiber.Cookie{
Name: defaultUsernameCookieName,
Value: chReq.LDAPUsername,
HTTPOnly: true,
Path: "/",
Expires: time.Now().Add(time.Second * time.Duration(challenge.TTL)),
}
}
}
case challengeTypeTOTP:
valid = challenge.verifyJSValue(chReq.JSValue) && challenge.verifyTOTP(chReq.TOTPPassword)
}
if valid {
defer config.getLogger().
Info().
Str(logPropertyChallengeType, challenge.ChallengeType).
Str(logType, logTypeChallengeSuccess).
Str(logPropertyIP, ip).
Str(logPropertyRequestID, requestID).
Send()
defer prometheusRequestChallengeSuccess.WithLabelValues(challenge.ChallengeType).Inc()
persistToken := newPersistToken(challenge.ChallengeType, challenge.ClientPersistChecksum, userName, challenge.TTL)
tokenString := persistToken.generate(config.tokenSecret)
tokenCookie := &fiber.Cookie{
Name: c.Get(httpRequestHeaderConfigCookie, defaultCookieName),
Value: tokenString,
HTTPOnly: true,
Path: "/",
Expires: time.Now().Add(time.Second * time.Duration(challenge.TTL)),
}
c.Set(httpResponseChallengePersist, tokenString)
c.Cookie(tokenCookie)
if ldapCookie != nil {
c.Cookie(ldapCookie)
}
return c.JSON(tokenString)
}
defer prometheusRequestChallengeFailed.WithLabelValues(challenge.ChallengeType).Inc()
defer config.getLogger().
Warn().
Str(logType, logTypeChallengeFailed).
Str(logPropertyChallengeType, challenge.ChallengeType).
Str(logPropertyIP, ip).
Str(logPropertyRequestID, requestID).
Send()
c.SendStatus(403)
return c.JSON(false)
}