2020-11-13 17:31:39 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// Package callback provides a handler for the OIDC callback endpoint.
|
|
|
|
package callback
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-11-13 23:59:51 +00:00
|
|
|
"path"
|
2020-11-13 17:31:39 +00:00
|
|
|
|
|
|
|
"go.pinniped.dev/internal/httputil/httperr"
|
2020-11-13 23:59:51 +00:00
|
|
|
"go.pinniped.dev/internal/oidc"
|
2020-11-16 16:47:49 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/csrftoken"
|
2020-11-13 23:59:51 +00:00
|
|
|
"go.pinniped.dev/internal/oidc/provider"
|
2020-11-13 17:31:39 +00:00
|
|
|
)
|
|
|
|
|
2020-11-16 16:47:49 +00:00
|
|
|
// Decoder is the decoding side of the securecookie.Codec interface.
|
|
|
|
type Decoder interface {
|
|
|
|
Decode(name, value string, into interface{}) error
|
|
|
|
}
|
|
|
|
|
2020-11-13 23:59:51 +00:00
|
|
|
func NewHandler(
|
|
|
|
idpListGetter oidc.IDPListGetter,
|
2020-11-16 16:47:49 +00:00
|
|
|
cookieDecoder Decoder,
|
2020-11-13 23:59:51 +00:00
|
|
|
) http.Handler {
|
2020-11-13 17:31:39 +00:00
|
|
|
return httperr.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
return httperr.Newf(http.StatusMethodNotAllowed, "%s (try GET)", r.Method)
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:47:49 +00:00
|
|
|
_, err := readCSRFCookie(r, cookieDecoder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-13 23:59:51 +00:00
|
|
|
if r.FormValue("code") == "" {
|
|
|
|
return httperr.New(http.StatusBadRequest, "code param not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.FormValue("state") == "" {
|
|
|
|
return httperr.New(http.StatusBadRequest, "state param not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if findUpstreamIDPConfig(r, idpListGetter) == nil {
|
|
|
|
return httperr.New(http.StatusUnprocessableEntity, "upstream provider not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return httperr.New(http.StatusBadRequest, "state param not valid")
|
2020-11-13 17:31:39 +00:00
|
|
|
})
|
|
|
|
}
|
2020-11-13 23:59:51 +00:00
|
|
|
|
|
|
|
func findUpstreamIDPConfig(r *http.Request, idpListGetter oidc.IDPListGetter) *provider.UpstreamOIDCIdentityProvider {
|
|
|
|
_, lastPathComponent := path.Split(r.URL.Path)
|
|
|
|
for _, p := range idpListGetter.GetIDPList() {
|
|
|
|
if p.Name == lastPathComponent {
|
|
|
|
return &p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-16 16:47:49 +00:00
|
|
|
|
|
|
|
func readCSRFCookie(r *http.Request, cookieDecoder Decoder) (csrftoken.CSRFToken, error) {
|
|
|
|
receivedCSRFCookie, err := r.Cookie(oidc.CSRFCookieName)
|
|
|
|
if err != nil {
|
|
|
|
// Error means that the cookie was not found
|
|
|
|
return "", httperr.Wrap(http.StatusForbidden, "unauthorized request", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var csrfFromCookie csrftoken.CSRFToken
|
|
|
|
err = cookieDecoder.Decode(oidc.CSRFCookieEncodingName, receivedCSRFCookie.Value, &csrfFromCookie)
|
|
|
|
if err != nil {
|
|
|
|
return "", httperr.Wrap(http.StatusForbidden, "unauthorized request", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return csrfFromCookie, nil
|
|
|
|
}
|