mirror of
https://github.com/sonr-io/sonr.git
synced 2026-08-04 18:31:41 +00:00
feat: add DID-based authentication middleware
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package producer
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/onsonr/sonr/crypto/ucan"
|
||||
"github.com/onsonr/sonr/crypto/ucan/store"
|
||||
"github.com/onsonr/sonr/pkg/common/ipfs"
|
||||
)
|
||||
|
||||
type ProducerContext struct {
|
||||
echo.Context
|
||||
// TokenParser is the attentuations assigned to the producer service
|
||||
TokenParser *ucan.TokenParser
|
||||
|
||||
// TokenStore is the token store used to store and retrieve tokens
|
||||
TokenStore store.IPFSTokenStore
|
||||
|
||||
// IPFSClient is the IPFS client used to resolve the UCAN
|
||||
IPFSClient ipfs.Client
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package producer
|
||||
|
||||
import (
|
||||
"github.com/onsonr/sonr/crypto/mpc"
|
||||
"github.com/onsonr/sonr/crypto/ucan"
|
||||
"github.com/onsonr/sonr/crypto/ucan/store"
|
||||
"github.com/onsonr/sonr/pkg/common/ipfs"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Middleware returns middleware to spawn controllers and validate UCAN tokens
|
||||
func Middleware(ipfs ipfs.Client, perms ucan.Permissions) echo.MiddlewareFunc {
|
||||
// Setup token store and parser
|
||||
store := store.NewIPFSTokenStore(ipfs)
|
||||
parser := ucan.NewTokenParser(perms.GetConstructor(), store, store)
|
||||
|
||||
// Return middleware
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
ctx := ProducerContext{
|
||||
Context: c,
|
||||
IPFSClient: ipfs,
|
||||
TokenParser: parser,
|
||||
TokenStore: store,
|
||||
}
|
||||
return next(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewKeyset(c echo.Context) (mpc.Keyset, error) {
|
||||
ks, err := mpc.NewKeyset()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ks, nil
|
||||
}
|
||||
Reference in New Issue
Block a user