docs/utils/context.go

72 lines
2.0 KiB
Go

package utils
import (
"github.com/endophage/go-tuf/signed"
"net/http"
)
// IContext defines an interface for managing authorizations.
type IContext interface {
// TODO: define a set of standard getters. Using getters
// will allow us to easily and transparently cache
// fields or load them on demand. Using this interface
// will allow people to define their own context struct
// that may handle things like caching and lazy loading
// differently.
// Resource return the QDN of the resource being accessed
Resource() string
// Authorized returns a boolean indicating whether the user
// has been successfully authorized for this request.
Authorization() IAuthorization
// SetAuthStatus should be called to change the authorization
// status of the context (and therefore the request)
SetAuthorization(IAuthorization)
// Trust returns the trust service to be used
Trust() signed.TrustService
}
// IContextFactory creates a IContext from an http request.
type IContextFactory func(*http.Request, signed.TrustService) IContext
// Context represents an authorization context for a resource.
type Context struct {
resource string
authorization IAuthorization
trust signed.TrustService
}
// ContextFactory creates a new authorization context with the
// given HTTP request path as the resource.
func ContextFactory(r *http.Request, trust signed.TrustService) IContext {
return &Context{
resource: r.URL.Path,
trust: trust,
}
}
// Resource returns the resource value for the context.
func (ctx *Context) Resource() string {
return ctx.resource
}
// Authorization returns an IAuthorization implementation for
// the context.
func (ctx *Context) Authorization() IAuthorization {
return ctx.authorization
}
// SetAuthorization allows setting an IAuthorization for
// the context.
func (ctx *Context) SetAuthorization(authzn IAuthorization) {
ctx.authorization = authzn
}
// Trust returns the instantiated TrustService for the context
func (ctx *Context) Trust() signed.TrustService {
return ctx.trust
}