-
Notifications
You must be signed in to change notification settings - Fork 15
/
2fa_okta.go
165 lines (156 loc) · 5.71 KB
/
2fa_okta.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
package main
import (
"encoding/json"
"net/http"
"time"
"github.com/Cloud-Foundations/keymaster/lib/authenticators/okta"
"github.com/Cloud-Foundations/keymaster/lib/instrumentedwriter"
"github.com/Cloud-Foundations/keymaster/lib/webapi/v0/proto"
)
const okta2FAauthPath = "/api/v0/okta2FAAuth"
const oktaPushStartPath = "/api/v0/oktaPushStart"
const oktaPollCheckPath = "/api/v0/oktaPollCheck"
func (state *RuntimeState) Okta2FAuthHandler(w http.ResponseWriter, r *http.Request) {
logger.Debugf(3, "Top of Okta2FAuthHandler")
authUser, currentAuthLevel, otpValue, err := state.commonTOTPPostHandler(w, r, AuthTypeAny)
if err != nil {
//Common handler handles returning the right error response to caller
logger.Printf("Error in common Handler")
return
}
oktaAuth, ok := state.passwordChecker.(*okta.PasswordAuthenticator)
if !ok {
logger.Println("password authenticator is not okta")
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Apparent Misconfiguration")
return
}
start := time.Now()
valid, err := oktaAuth.ValidateUserOTP(authUser, otpValue)
if err != nil {
logger.Println(err)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Failure when validating Okta MFA token")
return
}
metricLogExternalServiceDuration("okta-otp", time.Since(start))
metricLogAuthOperation(getClientType(r), proto.AuthTypeOkta2FA, valid)
if !valid {
logger.Printf("Invalid OTP value login for %s", authUser)
// TODO if client is html then do a redirect back to 2FALoginPage
state.writeFailureResponse(w, r, http.StatusUnauthorized, "")
return
}
// OTP check was successful
logger.Debugf(1, "Successful Okta OTP auth for user: %s", authUser)
// TODO ADD okta events to eventmond
//eventNotifier.PublishVIPAuthEvent(eventmon.VIPAuthTypeOTP, authUser)
_, err = state.updateAuthCookieAuthlevel(w, r, currentAuthLevel|AuthTypeOkta2FA)
if err != nil {
logger.Printf("Auth Cookie NOT found ? %s", err)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Failure when validating Okta MFA token")
return
}
// Now we send to the appropriate place
returnAcceptType := getPreferredAcceptType(r)
// TODO: The cert backend should depend also on per user preferences.
loginResponse := proto.LoginResponse{Message: "success"} //CertAuthBackend: certBackends
switch returnAcceptType {
case "text/html":
loginDestination := getLoginDestination(r)
eventNotifier.PublishWebLoginEvent(authUser)
http.Redirect(w, r, loginDestination, 302)
default:
w.WriteHeader(200)
json.NewEncoder(w).Encode(loginResponse)
}
return
}
func (state *RuntimeState) oktaPushStartHandler(w http.ResponseWriter, r *http.Request) {
logger.Debugf(3, "top of oktaPushStartHandler")
if state.sendFailureToClientIfLocked(w, r) {
logger.Printf("Invalid state on oktaPushStartHandler (not unsealed)")
return
}
logger.Printf("oktaPushStartHandler post lock")
if r.Method != "POST" && r.Method != "GET" {
state.writeFailureResponse(w, r, http.StatusMethodNotAllowed, "")
return
}
authData, err := state.checkAuth(w, r, AuthTypeAny)
if err != nil {
logger.Debugf(1, "%v", err)
return
}
w.(*instrumentedwriter.LoggingWriter).SetUsername(authData.Username)
oktaAuth, ok := state.passwordChecker.(*okta.PasswordAuthenticator)
if !ok {
logger.Debugf(2, "oktaPushStartHandler: password authenticator is not okta is of type %T", oktaAuth)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Apperent Misconfiguration")
return
}
pushResponse, err := oktaAuth.ValidateUserPush(authData.Username)
if err != nil {
logger.Println(err)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Failure when validating OKTA push")
return
}
switch pushResponse {
case okta.PushResponseWaiting:
w.WriteHeader(http.StatusOK)
return
default:
state.writeFailureResponse(w, r, http.StatusPreconditionFailed, "Push already sent")
return
}
}
func (state *RuntimeState) oktaPollCheckHandler(w http.ResponseWriter, r *http.Request) {
if state.sendFailureToClientIfLocked(w, r) {
return
}
if r.Method != "POST" && r.Method != "GET" {
state.writeFailureResponse(w, r, http.StatusMethodNotAllowed, "")
return
}
authData, err := state.checkAuth(w, r, AuthTypeAny)
if err != nil {
logger.Debugf(1, "%v", err)
return
}
w.(*instrumentedwriter.LoggingWriter).SetUsername(authData.Username)
oktaAuth, ok := state.passwordChecker.(*okta.PasswordAuthenticator)
if !ok {
logger.Println("password authenticator is not okta")
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Apperent Misconfiguration")
return
}
pushResponse, err := oktaAuth.ValidateUserPush(authData.Username)
if err != nil {
logger.Println(err)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Failure when validating OKTA push")
return
}
switch pushResponse {
case okta.PushResponseApproved:
// TODO: add notification on eventmond
metricLogAuthOperation(getClientType(r), proto.AuthTypeOkta2FA, true)
_, err = state.updateAuthCookieAuthlevel(w, r,
authData.AuthType|AuthTypeOkta2FA)
if err != nil {
logger.Printf("Auth Cookie NOT found ? %s", err)
state.writeFailureResponse(w, r, http.StatusInternalServerError, "Failure when validating Okta token")
return
}
w.WriteHeader(http.StatusOK)
return
case okta.PushResponseWaiting:
state.writeFailureResponse(w, r, http.StatusPreconditionFailed, "Push already sent")
return
case okta.PushResponseRejected:
metricLogAuthOperation(getClientType(r), proto.AuthTypeOkta2FA, false)
state.writeFailureResponse(w, r, http.StatusForbidden, "Failure when validating OKTA push")
return
default:
// TODO better message here!
state.writeFailureResponse(w, r, http.StatusPreconditionFailed, "Push already sent")
return
}
}