server: minor code fixes
This commit is contained in:
committed by
Michael Muré
parent
4c08b22c61
commit
1187674a24
@@ -16,7 +16,7 @@ import (
|
|||||||
"github.com/ucan-wg/go-ucan/token/delegation"
|
"github.com/ucan-wg/go-ucan/token/delegation"
|
||||||
"github.com/ucan-wg/go-ucan/token/invocation"
|
"github.com/ucan-wg/go-ucan/token/invocation"
|
||||||
|
|
||||||
bearer2 "github.com/INFURA/go-ucan-toolkit/server/bearer"
|
"github.com/INFURA/go-ucan-toolkit/server/bearer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ delegation.Loader = UcanCtx{}
|
var _ delegation.Loader = UcanCtx{}
|
||||||
@@ -32,8 +32,8 @@ type UcanCtx struct {
|
|||||||
meta *meta.Meta // all meta combined, with no overwriting
|
meta *meta.Meta // all meta combined, with no overwriting
|
||||||
|
|
||||||
// argument sources
|
// argument sources
|
||||||
http *bearer2.HttpBearer
|
http *bearer.HttpBearer
|
||||||
jsonrpc *bearer2.JsonRpcBearer
|
jsonrpc *bearer.JsonRpcBearer
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromContainer(cont container.Reader) (*UcanCtx, error) {
|
func FromContainer(cont container.Reader) (*UcanCtx, error) {
|
||||||
@@ -105,7 +105,7 @@ func (ctn UcanCtx) VerifyHttp(req *http.Request) error {
|
|||||||
if ctn.http == nil {
|
if ctn.http == nil {
|
||||||
panic("only use once per request context")
|
panic("only use once per request context")
|
||||||
}
|
}
|
||||||
ctn.http = bearer2.NewHttpBearer(ctn.policies, ctn.inv.Arguments(), req)
|
ctn.http = bearer.NewHttpBearer(ctn.policies, ctn.inv.Arguments(), req)
|
||||||
return ctn.http.Verify()
|
return ctn.http.Verify()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +116,7 @@ func (ctn UcanCtx) VerifyJsonRpc(req *jsonrpc.Request) error {
|
|||||||
if ctn.jsonrpc != nil {
|
if ctn.jsonrpc != nil {
|
||||||
panic("only use once per request context")
|
panic("only use once per request context")
|
||||||
}
|
}
|
||||||
ctn.jsonrpc = bearer2.NewJsonRpcBearer(ctn.policies, ctn.inv.Arguments(), req)
|
ctn.jsonrpc = bearer.NewJsonRpcBearer(ctn.policies, ctn.inv.Arguments(), req)
|
||||||
return ctn.jsonrpc.Verify()
|
return ctn.jsonrpc.Verify()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import (
|
|||||||
"github.com/ucan-wg/go-ucan/token/delegation"
|
"github.com/ucan-wg/go-ucan/token/delegation"
|
||||||
"github.com/ucan-wg/go-ucan/token/invocation"
|
"github.com/ucan-wg/go-ucan/token/invocation"
|
||||||
|
|
||||||
exectx2 "github.com/INFURA/go-ucan-toolkit/server/exectx"
|
"github.com/INFURA/go-ucan-toolkit/server/exectx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleContext() {
|
func ExampleContext() {
|
||||||
@@ -52,7 +52,7 @@ func ExampleContext() {
|
|||||||
|
|
||||||
// INVOCATION: the user leverages the delegation (power) to make a request.
|
// INVOCATION: the user leverages the delegation (power) to make a request.
|
||||||
|
|
||||||
inv, _ := invocation.New(user.DID(), service.DID(), cmd, []cid.Cid{dlgCid},
|
inv, _ := invocation.New(user.DID(), cmd, service.DID(), []cid.Cid{dlgCid},
|
||||||
invocation.WithExpirationIn(10*time.Minute),
|
invocation.WithExpirationIn(10*time.Minute),
|
||||||
invocation.WithArgument("myarg", "hello"), // we can specify invocation parameters
|
invocation.WithArgument("myarg", "hello"), // we can specify invocation parameters
|
||||||
)
|
)
|
||||||
@@ -84,10 +84,10 @@ func ExampleContext() {
|
|||||||
// Note: if an error occur, we'll want to return an HTTP 401 Unauthorized
|
// Note: if an error occur, we'll want to return an HTTP 401 Unauthorized
|
||||||
data := bytes.TrimPrefix([]byte(r.Header.Get("Authorization")), []byte("Bearer "))
|
data := bytes.TrimPrefix([]byte(r.Header.Get("Authorization")), []byte("Bearer "))
|
||||||
cont, _ := container.FromCborBase64(data)
|
cont, _ := container.FromCborBase64(data)
|
||||||
ucanCtx, _ := exectx2.FromContainer(cont)
|
ucanCtx, _ := exectx.FromContainer(cont)
|
||||||
|
|
||||||
// insert into the go context
|
// insert into the go context
|
||||||
req = req.WithContext(exectx2.AddUcanCtxToContext(req.Context(), ucanCtx))
|
req = req.WithContext(exectx.AddUcanCtxToContext(req.Context(), ucanCtx))
|
||||||
|
|
||||||
next.ServeHTTP(w, req)
|
next.ServeHTTP(w, req)
|
||||||
})
|
})
|
||||||
@@ -97,7 +97,7 @@ func ExampleContext() {
|
|||||||
|
|
||||||
httpMw := func(next http.Handler) http.Handler {
|
httpMw := func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ucanCtx, _ := exectx2.FromContext(req.Context())
|
ucanCtx, _ := exectx.FromContext(req.Context())
|
||||||
|
|
||||||
err := ucanCtx.VerifyHttp(r)
|
err := ucanCtx.VerifyHttp(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -114,7 +114,7 @@ func ExampleContext() {
|
|||||||
|
|
||||||
jsonrpcMw := func(next http.Handler) http.Handler {
|
jsonrpcMw := func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ucanCtx, _ := exectx2.FromContext(req.Context())
|
ucanCtx, _ := exectx.FromContext(req.Context())
|
||||||
|
|
||||||
var jrpc jsonrpc.Request
|
var jrpc jsonrpc.Request
|
||||||
_ = json.NewDecoder(r.Body).Decode(&jrpc)
|
_ = json.NewDecoder(r.Body).Decode(&jrpc)
|
||||||
@@ -131,7 +131,7 @@ func ExampleContext() {
|
|||||||
// SERVER: final handler
|
// SERVER: final handler
|
||||||
|
|
||||||
handler := func(w http.ResponseWriter, r *http.Request) {
|
handler := func(w http.ResponseWriter, r *http.Request) {
|
||||||
ucanCtx, _ := exectx2.FromContext(req.Context())
|
ucanCtx, _ := exectx.FromContext(req.Context())
|
||||||
|
|
||||||
if err := ucanCtx.ExecutionAllowed(); err != nil {
|
if err := ucanCtx.ExecutionAllowed(); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||||
@@ -148,15 +148,15 @@ func ExampleContext() {
|
|||||||
func TestGoCtx(t *testing.T) {
|
func TestGoCtx(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
ucanCtx, ok := exectx2.FromContext(ctx)
|
ucanCtx, ok := exectx.FromContext(ctx)
|
||||||
require.False(t, ok)
|
require.False(t, ok)
|
||||||
require.Nil(t, ucanCtx)
|
require.Nil(t, ucanCtx)
|
||||||
|
|
||||||
expected := &exectx2.UcanCtx{}
|
expected := &exectx.UcanCtx{}
|
||||||
|
|
||||||
ctx = exectx2.AddUcanCtxToContext(ctx, expected)
|
ctx = exectx.AddUcanCtxToContext(ctx, expected)
|
||||||
|
|
||||||
got, ok := exectx2.FromContext(ctx)
|
got, ok := exectx.FromContext(ctx)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, expected, got)
|
require.Equal(t, expected, got)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user