-
Notifications
You must be signed in to change notification settings - Fork 339
/
oidc_login_handler.go
54 lines (49 loc) · 1.8 KB
/
oidc_login_handler.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
package api
import (
"net/http"
"github.com/gorilla/sessions"
nanoid "github.com/matoous/go-nanoid/v2"
"github.com/treeverse/lakefs/pkg/config"
"github.com/treeverse/lakefs/pkg/logging"
"golang.org/x/oauth2"
)
const (
IDTokenClaimsSessionKey = "id_token_claims"
StateSessionKey = "state"
OIDCAuthSessionName = "oidc_auth_session"
stateLength = 22
)
// NewOIDCLoginPageHandler returns a handler to redirect the user the OIDC provider's login page.
func NewOIDCLoginPageHandler(oidcConfig config.OIDC, sessionStore sessions.Store, oauthConfig *oauth2.Config, logger logging.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// clear internal authentication session
err := clearSession(w, r, sessionStore, InternalAuthSessionName)
if err != nil {
logger.WithError(err).Error("Failed to clear internal auth session")
}
state, err := nanoid.New(stateLength)
if err != nil {
logger.WithError(err).Error("failed to generate state for oidc")
writeError(w, http.StatusInternalServerError, "Failed to redirect to login page")
return
}
session, _ := sessionStore.Get(r, OIDCAuthSessionName)
session.Values[StateSessionKey] = state
if err := session.Save(r, w); err != nil {
logger.WithError(err).Error("failed to save oidc session")
writeError(w, http.StatusInternalServerError, "Failed to save auth session")
return
}
opts := make([]oauth2.AuthCodeOption, 0, len(r.URL.Query()))
for k, v := range r.URL.Query() {
if len(v) < 1 {
continue
}
opts = append(opts, oauth2.SetAuthURLParam(k, v[0]))
}
for k, v := range oidcConfig.AuthorizeEndpointQueryParameters {
opts = append(opts, oauth2.SetAuthURLParam(k, v))
}
http.Redirect(w, r, oauthConfig.AuthCodeURL(state, opts...), http.StatusTemporaryRedirect)
}
}