2021-10-06 22:28:13 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-12-01 21:25:12 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// Package token provides a handler for the OIDC token endpoint.
|
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/ory/fosite"
|
|
|
|
|
|
|
|
"go.pinniped.dev/internal/httputil/httperr"
|
2020-12-04 15:06:55 +00:00
|
|
|
"go.pinniped.dev/internal/oidc"
|
2020-12-01 21:25:12 +00:00
|
|
|
"go.pinniped.dev/internal/plog"
|
2021-10-06 22:28:13 +00:00
|
|
|
"go.pinniped.dev/internal/psession"
|
2020-12-01 21:25:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewHandler(
|
|
|
|
oauthHelper fosite.OAuth2Provider,
|
|
|
|
) http.Handler {
|
|
|
|
return httperr.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
2021-10-06 22:28:13 +00:00
|
|
|
session := psession.NewPinnipedSession()
|
|
|
|
accessRequest, err := oauthHelper.NewAccessRequest(r.Context(), r, session)
|
2020-12-01 21:25:12 +00:00
|
|
|
if err != nil {
|
2020-12-04 15:06:55 +00:00
|
|
|
plog.Info("token request error", oidc.FositeErrorForLog(err)...)
|
2020-12-01 21:25:12 +00:00
|
|
|
oauthHelper.WriteAccessError(w, accessRequest, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
accessResponse, err := oauthHelper.NewAccessResponse(r.Context(), accessRequest)
|
|
|
|
if err != nil {
|
2020-12-04 15:06:55 +00:00
|
|
|
plog.Info("token response error", oidc.FositeErrorForLog(err)...)
|
2020-12-01 21:25:12 +00:00
|
|
|
oauthHelper.WriteAccessError(w, accessRequest, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
oauthHelper.WriteAccessResponse(w, accessRequest, accessResponse)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|