2020-12-01 21:25:12 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// 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"
|
|
|
|
"github.com/ory/fosite/handler/openid"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewHandler(
|
|
|
|
oauthHelper fosite.OAuth2Provider,
|
|
|
|
) http.Handler {
|
|
|
|
return httperr.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
var session openid.DefaultSession
|
|
|
|
accessRequest, err := oauthHelper.NewAccessRequest(r.Context(), r, &session)
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|