2024-10-15 14:31:19 -04:00
|
|
|
package ctx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
dwngen "github.com/onsonr/sonr/internal/dwn/gen"
|
|
|
|
)
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// ╭───────────────────────────────────────────────────────────╮
|
|
|
|
// │ DWNContext struct methods │
|
|
|
|
// ╰───────────────────────────────────────────────────────────╯
|
|
|
|
|
|
|
|
// DWNContext is the context for DWN endpoints.
|
2024-10-15 14:31:19 -04:00
|
|
|
type DWNContext struct {
|
|
|
|
echo.Context
|
|
|
|
|
|
|
|
// Defaults
|
|
|
|
id string // Generated ksuid http cookie; Initialized on first request
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// HasAuthorization returns true if the request has an Authorization header.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (s *DWNContext) HasAuthorization() bool {
|
|
|
|
v := ReadHeader(s.Context, HeaderAuthorization)
|
|
|
|
return v != ""
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// ID returns the ksuid http cookie.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (s *DWNContext) ID() string {
|
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// Address returns the sonr address from the cookies.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (s *DWNContext) Address() string {
|
|
|
|
v, err := ReadCookie(s.Context, CookieKeySonrAddr)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// IPFSGatewayURL returns the IPFS gateway URL from the headers.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (s *DWNContext) IPFSGatewayURL() string {
|
|
|
|
return ReadHeader(s.Context, HeaderIPFSGatewayURL)
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// ChainID returns the chain ID from the headers.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (s *DWNContext) ChainID() string {
|
|
|
|
return ReadHeader(s.Context, HeaderSonrChainID)
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// Schema returns the vault schema from the cookies.
|
2024-10-15 14:31:19 -04:00
|
|
|
func (s *DWNContext) Schema() *dwngen.Schema {
|
|
|
|
v, err := ReadCookie(s.Context, CookieKeyVaultSchema)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var schema dwngen.Schema
|
|
|
|
err = json.Unmarshal([]byte(v), &schema)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &schema
|
|
|
|
}
|
|
|
|
|
2024-10-21 11:30:52 -04:00
|
|
|
// GetDWNContext returns the DWNContext from the echo context.
|
2024-10-15 14:31:19 -04:00
|
|
|
func GetDWNContext(c echo.Context) (*DWNContext, error) {
|
|
|
|
ctx, ok := c.(*DWNContext)
|
|
|
|
if !ok {
|
|
|
|
return nil, echo.NewHTTPError(http.StatusInternalServerError, "DWN Context not found")
|
|
|
|
}
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HighwaySessionMiddleware establishes a Session Cookie.
|
|
|
|
func DWNSessionMiddleware(config *dwngen.Config) echo.MiddlewareFunc {
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
sessionID := GetSessionID(c)
|
|
|
|
injectConfig(c, config)
|
|
|
|
cc := &DWNContext{
|
|
|
|
Context: c,
|
|
|
|
id: sessionID,
|
|
|
|
}
|
|
|
|
return next(cc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|