-
Notifications
You must be signed in to change notification settings - Fork 15
/
2fa_bootstrapOTP.go
175 lines (169 loc) · 5.48 KB
/
2fa_bootstrapOTP.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
169
170
171
172
173
174
175
package main
import (
"crypto/sha512"
"crypto/subtle"
"encoding/json"
"net/http"
"time"
"github.com/Cloud-Foundations/keymaster/lib/instrumentedwriter"
"github.com/Cloud-Foundations/keymaster/lib/webapi/v0/proto"
)
const (
bootstrapOtpAuthPath = "/api/v0/bootstrapOtpAuth"
selfServiceBootstrapOtpLifetime = time.Minute * 5
)
func (state *RuntimeState) BootstrapOtpAuthHandler(w http.ResponseWriter,
r *http.Request) {
if state.sendFailureToClientIfLocked(w, r) {
return
}
if r.Method != "GET" && r.Method != "POST" {
state.writeFailureResponse(w, r, http.StatusMethodNotAllowed, "")
return
}
state.logger.Debugf(3, "Got client POST connection")
if err := r.ParseForm(); err != nil {
state.logger.Println(err)
state.writeFailureResponse(w, r, http.StatusInternalServerError,
"Error parsing form")
return
}
authData, err := state.checkAuth(w, r, AuthTypeAny)
if err != nil {
state.logger.Debugf(1, "%v", err)
return
}
w.(*instrumentedwriter.LoggingWriter).SetUsername(authData.Username)
var inputOtpHash [sha512.Size]byte
if val, ok := r.Form["OTP"]; !ok {
state.writeFailureResponse(w, r, http.StatusBadRequest,
"No OTP value provided")
state.logger.Printf("Bootstrap OTP login without OTP value")
return
} else {
if len(val) > 1 {
state.writeFailureResponse(w, r, http.StatusBadRequest,
"Just one OTP value allowed")
state.logger.Printf("Bootstrap OTP login with multiple OTP values")
return
}
inputOtpHash = sha512.Sum512([]byte(val[0]))
}
profile, _, fromCache, err := state.LoadUserProfile(authData.Username)
if err != nil {
state.logger.Printf("error loading user profile err=%s", err)
state.writeFailureResponse(w, r, http.StatusInternalServerError,
"Failure loading user profile")
return
}
if fromCache {
state.writeFailureResponse(w, r, http.StatusServiceUnavailable,
"Working in DB disconnected mode, try again later")
return
}
requiredOtpHash := state.userBootstrapOtpHash(profile, fromCache)
if len(requiredOtpHash) < 1 {
state.writeFailureResponse(w, r, http.StatusPreconditionFailed,
"No valid Bootstrap OTP hash saved")
return
}
if subtle.ConstantTimeCompare(inputOtpHash[:], requiredOtpHash) != 1 {
state.logger.Debugf(0, "Invalid Bootstrap OTP value for %s\n",
authData.Username)
var tmp [sha512.Size]byte
copy(tmp[:], requiredOtpHash)
state.logger.Debugf(4, " input: \"%v\" required: \"%v\"\n",
inputOtpHash, tmp)
state.writeFailureResponse(w, r, http.StatusUnauthorized,
"Invalid Bootstrap OTP")
return
}
profile.BootstrapOTP = bootstrapOTPData{}
if err := state.SaveUserProfile(authData.Username, profile); err != nil {
state.logger.Printf("error saving profile randr=%s", err)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "")
return
}
_, err = state.updateAuthCookieAuthlevel(w, r,
authData.AuthType|AuthTypeBootstrapOTP)
if err != nil {
logger.Printf("Auth Cookie NOT found ? %s", err)
state.writeFailureResponse(w, r, http.StatusInternalServerError,
"Failure when validating Boostrap OTP")
return
}
// eventNotifier.PublishBootstrapOtpAuthEvent(eventmon.AuthTypeBootstrapOTP,
// authData.Username)
// Now we send the user to the appropriate place
returnAcceptType := getPreferredAcceptType(r)
// TODO: The cert backend should depend also on per user preferences.
loginResponse := proto.LoginResponse{Message: "success"}
switch returnAcceptType {
case "text/html":
loginDestination := getLoginDestination(r)
eventNotifier.PublishWebLoginEvent(authData.Username)
state.logger.Debugf(0, "redirecting to: %s\n", loginDestination)
http.Redirect(w, r, loginDestination, 302)
default:
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(loginResponse)
}
}
func (state *RuntimeState) trySelfServiceGenerateBootstrapOTP(username string,
inputProfile *userProfile) bool {
profile := *inputProfile
if !state.Config.Base.AllowSelfServiceBootstrapOTP ||
len(profile.U2fAuthData) > 0 ||
len(profile.TOTPAuthData) > 0 ||
profile.UserHasRegistered2ndFactor ||
len(state.userBootstrapOtpHash(&profile, false)) > 0 ||
state.emailManager == nil {
return false
}
bootstrapOtpValue, err := genRandomString()
if err != nil {
state.logger.Printf("error generating Bootstrap OTP: %s", err)
return false
}
duration := selfServiceBootstrapOtpLifetime
bootstrapOtpHash := sha512.Sum512([]byte(bootstrapOtpValue))
bootstrapOTP := bootstrapOTPData{
ExpiresAt: time.Now().Add(duration),
Sha512Hash: bootstrapOtpHash[:],
}
profile.BootstrapOTP = bootstrapOTP
var fingerprint [4]byte
copy(fingerprint[:], bootstrapOtpHash[:4])
err = state.sendBootstrapOtpEmail(bootstrapOtpHash[:],
bootstrapOtpValue, duration, username, username)
if err != nil {
state.logger.Printf("error sending email: %s", err)
return false
}
err = state.SaveUserProfile(username, &profile)
if err != nil {
state.logger.Printf("error saving profile: %s", err)
return false
}
state.logger.Debugf(0,
"generated bootstrap OTP by/for: %s, duration: %s, hash: %x\n",
duration, username, bootstrapOtpHash)
*inputProfile = profile
return true
}
func (state *RuntimeState) userBootstrapOtpHash(profile *userProfile,
fromCache bool) []byte {
if len(profile.U2fAuthData) > 0 || len(profile.TOTPAuthData) > 0 {
return nil
}
if fromCache { // Since we will want to clear the OTP, require connection.
return nil
}
if len(profile.BootstrapOTP.Sha512Hash) < 1 {
return nil
}
if time.Since(profile.BootstrapOTP.ExpiresAt) >= 0 {
return nil
}
return profile.BootstrapOTP.Sha512Hash
}