update types

This commit is contained in:
Denis Mishin 2025-04-23 22:42:31 -04:00
parent b4e762e70e
commit faf8619d08
4 changed files with 76 additions and 1400 deletions

View file

@ -1,12 +1,13 @@
package mcp
import (
"encoding/json"
"io"
"net/http"
"time"
"github.com/bufbuild/protovalidate-go"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"github.com/pomerium/pomerium/internal/log"
rfc7591v1 "github.com/pomerium/pomerium/internal/rfc7591"
@ -33,7 +34,7 @@ func (srv *Handler) RegisterClient(w http.ResponseWriter, r *http.Request) {
return
}
v := new(rfc7591v1.ClientRegistrationRequest)
v := new(rfc7591v1.ClientMetadata)
err = protojson.Unmarshal(data, v)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("failed to unmarshal request body")
@ -44,19 +45,26 @@ func (srv *Handler) RegisterClient(w http.ResponseWriter, r *http.Request) {
err = protovalidate.Validate(v)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("failed to validate request body")
clientRegistrationError(w, err, rfc7591v1.ErrorCode_ERROR_CODE_INVALID_CLIENT_METADATA)
clientRegistrationBadRequest(w, err)
return
}
resp, err := srv.storage.RegisterClient(ctx, v)
id, err := srv.storage.RegisterClient(ctx, v)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("failed to register client")
http.Error(w, "failed to register client", http.StatusInternalServerError)
}
data, err = protojson.MarshalOptions{
UseProtoNames: true,
}.Marshal(resp)
resp := struct {
*rfc7591v1.ClientMetadata
ClientID string `json:"client_id"`
ClientIDIssuedAt int64 `json:"client_id_issued_at"`
}{
ClientMetadata: v,
ClientID: id,
ClientIDIssuedAt: time.Now().Unix(),
}
data, err = json.Marshal(resp)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("failed to marshal response")
http.Error(w, "failed to marshal response", http.StatusInternalServerError)
@ -70,13 +78,18 @@ func (srv *Handler) RegisterClient(w http.ResponseWriter, r *http.Request) {
}
}
func clientRegistrationError(w http.ResponseWriter, err error, code rfc7591v1.ErrorCode) {
v := &rfc7591v1.ClientRegistrationErrorResponse{
Error: code,
ErrorDescription: proto.String(err.Error()),
func clientRegistrationBadRequest(w http.ResponseWriter, err error) {
v := struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description,omitempty"`
}{
Error: "invalid_client_metadata",
ErrorDescription: err.Error(),
}
data, _ := protojson.Marshal(v)
data, _ := json.Marshal(v)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Pragma", "no-cache")
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write(data)
}

View file

@ -2,10 +2,8 @@ package mcp
import (
"context"
"fmt"
"github.com/google/uuid"
"google.golang.org/protobuf/proto"
rfc7591v1 "github.com/pomerium/pomerium/internal/rfc7591"
"github.com/pomerium/pomerium/pkg/grpc/databroker"
@ -27,11 +25,11 @@ func NewStorage(
func (storage *Storage) RegisterClient(
ctx context.Context,
req *rfc7591v1.ClientRegistrationRequest,
) (*rfc7591v1.ClientInformationResponse, error) {
req *rfc7591v1.ClientMetadata,
) (string, error) {
data := protoutil.NewAny(req)
id := uuid.NewString()
rec, err := storage.client.Put(ctx, &databroker.PutRequest{
_, err := storage.client.Put(ctx, &databroker.PutRequest{
Records: []*databroker.Record{{
Id: id,
Data: data,
@ -39,44 +37,7 @@ func (storage *Storage) RegisterClient(
}},
})
if err != nil {
return nil, err
}
if len(rec.Records) == 0 {
return nil, fmt.Errorf("no records returned")
}
now := rec.Records[0].GetModifiedAt().Seconds
return getClientInformation(id, now, req), nil
}
func getClientInformation(
id string,
issuedAt int64,
req *rfc7591v1.ClientRegistrationRequest,
) *rfc7591v1.ClientInformationResponse {
return &rfc7591v1.ClientInformationResponse{
ClientId: id,
ClientIdIssuedAt: proto.Int64(issuedAt),
RedirectUris: req.RedirectUris,
TokenEndpointAuthMethod: req.TokenEndpointAuthMethod,
GrantTypes: req.GrantTypes,
ResponseTypes: req.ResponseTypes,
ClientName: req.ClientName,
ClientNameLocalized: req.ClientNameLocalized,
ClientUri: req.ClientUri,
ClientUriLocalized: req.ClientUriLocalized,
LogoUri: req.LogoUri,
LogoUriLocalized: req.LogoUriLocalized,
Scope: req.Scope,
Contacts: req.Contacts,
TosUri: req.TosUri,
TosUriLocalized: req.TosUriLocalized,
PolicyUri: req.PolicyUri,
PolicyUriLocalized: req.PolicyUriLocalized,
JwksUri: req.JwksUri,
Jwks: req.Jwks,
SoftwareId: req.SoftwareId,
SoftwareVersion: req.SoftwareVersion,
SoftwareStatement: req.SoftwareStatement,
return "", err
}
return id, nil
}