|
5 | 5 | package oidc
|
6 | 6 |
|
7 | 7 | import (
|
8 |
| - "github.com/go-chi/chi/v5" |
| 8 | + "context" |
| 9 | + "encoding/json" |
9 | 10 | "net/http"
|
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/gitpod-io/gitpod/common-go/log" |
| 14 | + "golang.org/x/oauth2" |
| 15 | + |
| 16 | + "github.com/go-chi/chi/v5" |
10 | 17 | )
|
11 | 18 |
|
12 |
| -func Router() *chi.Mux { |
13 |
| - router := chi.NewMux() |
| 19 | +func Router(oidcService *OIDCService) *chi.Mux { |
| 20 | + router := chi.NewRouter() |
14 | 21 |
|
15 |
| - router.HandleFunc("/start", func(writer http.ResponseWriter, request *http.Request) { |
16 |
| - writer.Write([]byte(`hello`)) |
| 22 | + router.Route("/start", func(r chi.Router) { |
| 23 | + r.Use(oidcService.clientConfigMiddleware()) |
| 24 | + r.Get("/", oidcService.getStartHandler()) |
| 25 | + }) |
| 26 | + router.Route("/callback", func(r chi.Router) { |
| 27 | + r.Use(oidcService.clientConfigMiddleware()) |
| 28 | + r.Use(OAuth2Middleware) |
| 29 | + r.Get("/", oidcService.getCallbackHandler()) |
17 | 30 | })
|
18 | 31 |
|
19 | 32 | return router
|
20 | 33 | }
|
| 34 | + |
| 35 | +type keyOIDCClientConfig struct{} |
| 36 | + |
| 37 | +const ( |
| 38 | + stateCookieName = "state" |
| 39 | + nonceCookieName = "nonce" |
| 40 | +) |
| 41 | + |
| 42 | +func (oidcService *OIDCService) getStartHandler() http.HandlerFunc { |
| 43 | + return func(rw http.ResponseWriter, r *http.Request) { |
| 44 | + log.Trace("at start handler") |
| 45 | + |
| 46 | + ctx := r.Context() |
| 47 | + config, ok := ctx.Value(keyOIDCClientConfig{}).(OIDCClientConfig) |
| 48 | + if !ok { |
| 49 | + http.Error(rw, "config not found", http.StatusInternalServerError) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + startParams, err := oidcService.GetStartParams(&config) |
| 54 | + if err != nil { |
| 55 | + http.Error(rw, "failed to start auth flow", http.StatusInternalServerError) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + http.SetCookie(rw, newCallbackCookie(r, nonceCookieName, startParams.Nonce)) |
| 60 | + http.SetCookie(rw, newCallbackCookie(r, stateCookieName, startParams.State)) |
| 61 | + |
| 62 | + http.Redirect(rw, r, startParams.AuthCodeURL, http.StatusTemporaryRedirect) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func newCallbackCookie(r *http.Request, name string, value string) *http.Cookie { |
| 67 | + return &http.Cookie{ |
| 68 | + Name: name, |
| 69 | + Value: value, |
| 70 | + MaxAge: int(10 * time.Minute.Seconds()), |
| 71 | + Secure: r.TLS != nil, |
| 72 | + SameSite: http.SameSiteLaxMode, |
| 73 | + HttpOnly: true, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// The config middleware is responsible to retrieve the client config suitable for request |
| 78 | +func (oidcService *OIDCService) clientConfigMiddleware() func(http.Handler) http.Handler { |
| 79 | + return func(next http.Handler) http.Handler { |
| 80 | + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 81 | + log.Trace("at config middleware") |
| 82 | + |
| 83 | + config, err := oidcService.GetClientConfigFromRequest(r) |
| 84 | + if err != nil { |
| 85 | + log.Warn("client config not found: " + err.Error()) |
| 86 | + http.Error(rw, "config not found", http.StatusNotFound) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + ctx := context.WithValue(r.Context(), keyOIDCClientConfig{}, config) |
| 91 | + next.ServeHTTP(rw, r.WithContext(ctx)) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// The OIDC callback handler depends on the state produced in the OAuth2 middleware |
| 97 | +func (oidcService *OIDCService) getCallbackHandler() http.HandlerFunc { |
| 98 | + return func(rw http.ResponseWriter, r *http.Request) { |
| 99 | + log.Trace("at callback handler") |
| 100 | + |
| 101 | + ctx := r.Context() |
| 102 | + config, ok := ctx.Value(keyOIDCClientConfig{}).(OIDCClientConfig) |
| 103 | + if !ok { |
| 104 | + http.Error(rw, "config not found", http.StatusInternalServerError) |
| 105 | + return |
| 106 | + } |
| 107 | + oauth2Result, ok := ctx.Value(keyOAuth2Result{}).(OAuth2Result) |
| 108 | + if !ok { |
| 109 | + http.Error(rw, "OIDC precondition failure", http.StatusInternalServerError) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + // nonce = number used once |
| 114 | + nonceCookie, err := r.Cookie(nonceCookieName) |
| 115 | + if err != nil { |
| 116 | + http.Error(rw, "nonce not found", http.StatusBadRequest) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + result, err := oidcService.Authenticate(ctx, &oauth2Result, |
| 121 | + config.Issuer, nonceCookie.Value) |
| 122 | + if err != nil { |
| 123 | + http.Error(rw, "OIDC authentication failed", http.StatusInternalServerError) |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + // TODO(at) given the result of OIDC authN, let's proceed with the redirect |
| 128 | + |
| 129 | + // For testing purposes, let's print out redacted results |
| 130 | + oauth2Result.OAuth2Token.AccessToken = "*** REDACTED ***" |
| 131 | + |
| 132 | + var claims map[string]interface{} |
| 133 | + err = result.IDToken.Claims(&claims) |
| 134 | + if err != nil { |
| 135 | + http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 136 | + return |
| 137 | + } |
| 138 | + resp := struct { |
| 139 | + OAuth2Token *oauth2.Token |
| 140 | + Claims map[string]interface{} |
| 141 | + }{oauth2Result.OAuth2Token, claims} |
| 142 | + |
| 143 | + data, err := json.MarshalIndent(resp, "", " ") |
| 144 | + if err != nil { |
| 145 | + http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 146 | + return |
| 147 | + } |
| 148 | + _, err = rw.Write(data) |
| 149 | + if err != nil { |
| 150 | + http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments