Files
zeekayandHanzo Dev 2b6378c4a0 zt: build against the hanzozt forks
github.com/hanzozt-incubator does not exist — cf is a third-party dependency the
rebrand should never have touched, and points at openziti-incubator/cf again.

A rename put a literal space inside identifiers (Hanzo ZTOrg, ClaimAudienceHanzo
ZT); only occurrences with an identifier character on one side are joined, the
brand name in prose stays.

foundation renamed errorz.ApiError.Code to AppCode and gave the old name to a
Code() int32 method, so a stale .Code resolved to the method instead of failing
loudly. rest_model.APIError.Code is a different field and keeps its name.

The foundation self-replace pinned v2.0.86, a tag that still declared the openziti
path, under a require of v2.0.92 that declares the hanzozt one.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-30 05:50:42 -07:00

119 lines
3.4 KiB
Go

/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package oidc_auth
import (
"encoding"
"errors"
"net/http"
"github.com/michaelquigley/pfxlog"
"github.com/hanzozt/edge-api/rest_model"
"github.com/hanzozt/foundation/v2/errorz"
)
// render will attempt to send a responses on the provided http.ResponseWriter. All error output will be directed to the
// http.ResponseWriter. The provided http.ResponseWriter will have had its header sent after calling this function.
func render(w http.ResponseWriter, contentType string, status int, data encoding.BinaryMarshaler) {
payload, err := data.MarshalBinary()
if err != nil {
pfxlog.Logger().WithError(err).Error("could not marshal data payload, attempting to respond with a marshalling error")
internalErr := &rest_model.APIError{
Code: errorz.UnhandledCode,
Message: "could not marshal, see cause",
Cause: &rest_model.APIErrorCause{
APIError: rest_model.APIError{
Code: "UNHANDLED",
Message: err.Error(),
},
},
}
// if there is an err here, we are giving up as we have already tried to recover.
internalErrPayload, internalErrMarshalErr := internalErr.MarshalBinary()
if err != nil {
pfxlog.Logger().WithError(internalErrMarshalErr).Error("could not write marshaling error, failed to marshal internal error, writing '{}'")
}
if len(internalErrPayload) == 0 {
internalErrPayload = []byte("{}")
}
w.Header().Set(ContentTypeHeader, contentType)
w.WriteHeader(http.StatusInternalServerError)
_, err = w.Write(internalErrPayload)
if err != nil {
pfxlog.Logger().WithError(err).WithField("internalErrPayload", internalErrPayload).Error("could not write the internal error payload, giving up")
}
return
}
w.Header().Set(ContentTypeHeader, contentType)
w.WriteHeader(status)
_, err = w.Write(payload)
if err != nil {
pfxlog.Logger().WithError(err).Error("error writing payload")
}
}
// renderJson will attempt to render the provided data as JSON.
func renderJson(w http.ResponseWriter, status int, data encoding.BinaryMarshaler) {
render(w, JsonContentType, status, data)
}
func renderJsonError(w http.ResponseWriter, err error) {
restErr, status := errorToRestApiError(err)
renderJson(w, status, restErr)
}
func renderJsonApiError(w http.ResponseWriter, err *errorz.ApiError) {
restErr, status := errorToRestApiError(err)
renderJson(w, status, restErr)
}
func errorToRestApiError(err error) (*rest_model.APIError, int) {
var typedErr *errorz.ApiError
switch {
case errors.As(err, &typedErr):
restErr := &rest_model.APIError{
Code: typedErr.AppCode,
Message: typedErr.Message,
}
if typedErr.Cause != nil {
causeErr, _ := errorToRestApiError(typedErr.Cause)
restErr.Cause = &rest_model.APIErrorCause{
APIError: *causeErr,
}
}
return restErr, typedErr.Status
default:
return &rest_model.APIError{
Code: "UNHANDLED",
Message: err.Error(),
}, http.StatusInternalServerError
}
}