Files
threshold/internal/requests/requests.go
T
shadowdaoandClaude Sonnet 5.5 962d708fdd Rename Go module paths to repo.anhonesthost.net/CyberCoveLLC/threshold
Rename root, api and sdk module paths (imports, go.mod, go_package,
ldflags, build scripts, buf configs) and regenerate protobuf/gateway
code with the project's pinned toolchain. Wire identifiers unchanged.
api gets a local replace for sdk; root/api/sdk require v0.1.0.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 10:33:41 -07:00

77 lines
2.4 KiB
Go

// Copyright IBM Corp. 2020, 2026
// SPDX-License-Identifier: BUSL-1.1
package requests
import (
"context"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/perms"
)
// ContextRequestInforation is a type used solely for context keys -- see the
// variable below
type ContextRequestInformation struct{}
// ContextRequestInformationKey is a value to keep linters from complaining
// about clashing identifiers
var ContextRequestInformationKey ContextRequestInformation
// RequestContext is used to propagate request information. It can be updated at
// various points, e.g. UserId would be updated via the result of auth.Verify.
//
// It serves as a struct to gather information we learn about the request as it
// goes along. This is distinct from options handling; none of the fields here
// are "optional" and should all eventually be populated as the request moves
// through the system.
type RequestContext struct {
// Method is the method of the request
Method string
// Path is the path of the request
Path string
// UserId contains the final discovered user ID
UserId string
// OutputFields is the set of fields authorized for output for the
// authorized action, if not the default
OutputFields *perms.OutputFields
}
// NewRequestContext returns a derived context with a new RequestContext value
// added in.
func NewRequestContext(parent context.Context, opt ...Option) context.Context {
opts := getOpts(opt...)
ret := &RequestContext{
UserId: opts.withUserId,
}
return context.WithValue(parent, ContextRequestInformationKey, ret)
}
// RequestContextFromCtx pulls out RequestContext and returns it and an
// indication it was found. If it's not found, nil will be returned and the bool
// will be false.
func RequestContextFromCtx(ctx context.Context) (*RequestContext, bool) {
reqCtxRaw := ctx.Value(ContextRequestInformationKey)
if reqCtxRaw == nil {
return nil, false
}
reqCtx, ok := reqCtxRaw.(*RequestContext)
if !ok {
return nil, false
}
return reqCtx, true
}
// OutputFields returns output fields from the given context and calls
// SelfOrDefaults on it. If the context does not contain a RequestContext,
// this will return nil, false.
func OutputFields(ctx context.Context) (*perms.OutputFields, bool) {
reqCtx, ok := RequestContextFromCtx(ctx)
if !ok {
return nil, false
}
return reqCtx.OutputFields.SelfOrDefaults(reqCtx.UserId), true
}