-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp_challenge.go
108 lines (89 loc) · 3.23 KB
/
http_challenge.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
package main
import (
"encoding/json"
"time"
"github.com/gofiber/fiber/v2"
)
func httpChallenge(c *fiber.Ctx, config *config) error {
ip := c.Locals(localVarIP).(string)
requestID := c.Locals(localVarRequestID).(string)
challengeType := c.Locals(localVarChallengeType).(string)
persistChecksum := c.Locals(localVarClientPersistChecksum).(string)
temporaryChecksum := c.Locals(localVarClientTemporaryChecksum).(string)
challengeEmoji := "⌛️"
waitSeconds := getConfigWaitSeconds(c)
timeoutSeconds := getConfigTimeoutSeconds(c)
supportedLanguages := getSupportedLanguages(c)
lang := getLanguage(c, config)
if !isSupportedLanguageConfig(lang, supportedLanguages) {
lang = config.defaultLanguage
supportedLanguages = []string{lang}
}
defer prometheusRequestChallenge.WithLabelValues(challengeType).Inc()
switch challengeType {
case challengeTypeBlock:
waitSeconds = 0
timeoutSeconds = 0
challengeEmoji = "⛔️"
case challengeTypeCaptcha:
challengeEmoji = getHumanEmoji()
case challengeTypeTOTP:
challengeEmoji = "🔐"
case challengeTypeLDAP:
challengeEmoji = "🛂"
}
var challenge *challenge
challengeToken := ""
if challengeType != challengeTypeBlock {
ttl := getConfigTTLSeconds(c)
challenge = newChallenge(lang, challengeType, temporaryChecksum, persistChecksum, waitSeconds, timeoutSeconds, ttl)
}
if challengeType == challengeTypeTOTP {
challenge.setTOTPSecret(c.Locals(localVarTOTPSecret).(string))
}
languageData := languagesData(supportedLanguages, lang)
supportedLanguagesJSON, _ := json.Marshal(supportedLanguages)
supportInfo, _ := json.Marshal(getConfigSupportInfo(c))
ipData, _ := json.Marshal(getClientProperties(c))
unixTime, _ := json.Marshal(time.Now().Unix())
defer config.getLogger().
Info().
Str(logType, logTypeChallengeGenerate).
Str(logPropertyIP, ip).
Str(logPropertyRequestID, requestID).
Str(logPropertyChallengeType, challengeType).
Send()
if challenge != nil {
challengeToken, _ = challenge.getChallengeToken(config.clientSecret)
}
// set header
c.Set(httpResponseChallengeTemporary, challengeToken)
// return nil
return c.Render("templates/"+challengeType, fiber.Map{
// html
"title": translateData[lang][challengeType],
"dir": getLanguageDirection(lang),
"staticURL": config.staticURL,
"i18n": translateData[lang],
"supportedLanguages": string(supportedLanguagesJSON),
"multiLanguage": len(config.supportedLanguages) > 1,
"languageData": languageData,
"challengeEmoji": challengeEmoji,
"organizationTitle": getConfigI18nOrganizationTitle(c, lang),
"organizationBrandIcon": getConfigI18nOrganizationBrandIcon(c),
"challengeType": challengeType,
"persistChecksum": persistChecksum,
"cdnStatic": config.cdnStatic,
"aasaamWebServer": config.aasaamWebServer,
// js variables
"lang": lang,
"unixTime": string(unixTime),
"challengeToken": challengeToken,
"ipData": string(ipData),
"protectedPath": getProtectedPath(c),
"supportInfo": string(supportInfo),
"waitSeconds": waitSeconds,
"timeoutSeconds": timeoutSeconds,
"baseURL": config.baseURL,
}, "templates/layouts/main")
}