-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathcallback.go
More file actions
121 lines (108 loc) · 4.49 KB
/
Copy pathcallback.go
File metadata and controls
121 lines (108 loc) · 4.49 KB
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
package authflow
// callback.go implements the connector callback mechanism: the return leg of
// redirect-based connectors (OAuth2 callback and SAML POST binding).
import (
"errors"
"net/http"
"net/url"
"github.com/gorilla/mux"
"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/server/tokens"
"github.com/dexidp/dex/storage"
)
func (h *Handler) handleConnectorCallback(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var authID string
switch r.Method {
case http.MethodGet: // OAuth2 callback
if authID = r.URL.Query().Get("state"); authID == "" {
h.renderError(r, w, http.StatusBadRequest, "User session error.")
return
}
case http.MethodPost: // SAML POST binding
if authID = r.PostFormValue("RelayState"); authID == "" {
h.renderError(r, w, http.StatusBadRequest, "User session error.")
return
}
default:
h.renderError(r, w, http.StatusBadRequest, "Method not supported")
return
}
authReq, err := h.Storage.GetAuthRequest(ctx, authID)
if err != nil {
if err == storage.ErrNotFound {
h.Logger.ErrorContext(r.Context(), "invalid 'state' parameter provided", "err", err)
h.renderError(r, w, http.StatusBadRequest, "Requested resource does not exist.")
return
}
h.Logger.ErrorContext(r.Context(), "failed to get auth request", "err", err)
h.renderError(r, w, http.StatusInternalServerError, "Database error.")
return
}
connID, err := url.PathUnescape(mux.Vars(r)["connector"])
if err != nil {
h.Logger.ErrorContext(r.Context(), "failed to parse connector", "err", err)
h.renderError(r, w, http.StatusBadRequest, "Requested resource does not exist.")
return
} else if connID != "" && connID != authReq.ConnectorID {
h.Logger.ErrorContext(r.Context(), "connector mismatch: callback triggered for different connector than authentication start", "authentication_start_connector_id", authReq.ConnectorID, "connector_id", connID)
h.renderError(r, w, http.StatusBadRequest, "Requested resource does not exist.")
return
}
conn, err := h.Connectors.Get(ctx, authReq.ConnectorID)
if err != nil {
h.Logger.ErrorContext(r.Context(), "failed to get connector", "connector_id", authReq.ConnectorID, "err", err)
h.renderError(r, w, http.StatusInternalServerError, "Requested resource does not exist.")
return
}
var identity connector.Identity
switch conn := conn.Connector.(type) {
case connector.CallbackConnector:
if r.Method != http.MethodGet {
h.Logger.ErrorContext(r.Context(), "SAML request mapped to OAuth2 connector")
h.renderError(r, w, http.StatusBadRequest, "Invalid request")
return
}
identity, err = conn.HandleCallback(tokens.ParseScopes(authReq.Scopes), authReq.ConnectorData, r)
case connector.SAMLConnector:
if r.Method != http.MethodPost {
h.Logger.ErrorContext(r.Context(), "OAuth2 request mapped to SAML connector")
h.renderError(r, w, http.StatusBadRequest, "Invalid request")
return
}
identity, err = conn.HandlePOST(tokens.ParseScopes(authReq.Scopes), r.PostFormValue("SAMLResponse"), authReq.ID)
default:
h.renderError(r, w, http.StatusInternalServerError, "Requested resource does not exist.")
return
}
if err != nil {
h.Logger.ErrorContext(r.Context(), "failed to authenticate", "err", err)
var groupsErr *connector.UserNotInRequiredGroupsError
if errors.As(err, &groupsErr) {
h.renderError(r, w, http.StatusForbidden, ErrMsgNotInRequiredGroups)
} else {
h.renderError(r, w, http.StatusInternalServerError, ErrMsgAuthenticationFailed)
}
return
}
authReq, err = h.finalizeLogin(ctx, identity, authReq, conn.Connector)
if err != nil {
h.Logger.ErrorContext(r.Context(), "failed to finalize login", "err", err)
if errors.Is(err, storage.ErrNotFound) {
// The auth request is gone from storage, most likely because an earlier,
// still-in-flight submission already finalized it (e.g. a
// double-submitted callback).
h.renderError(r, w, http.StatusBadRequest, ErrMsgRequestAlreadyCompleted)
return
}
h.renderError(r, w, http.StatusInternalServerError, "Login error.")
return
}
// Connector callbacks don't render the remember_me checkbox, so we use the server default.
// The password login handler reads r.FormValue("remember_me") from the submitted form instead.
rememberMe := h.Sessions.RememberMeDefault()
if err := h.Sessions.CreateOrUpdateAuthSession(ctx, r, w, authReq, rememberMe != nil && *rememberMe); err != nil {
h.Logger.ErrorContext(ctx, "failed to create/update auth session", "err", err)
}
http.Redirect(w, r, h.buildContinueURL(authReq), http.StatusSeeOther)
}