-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ACT grantee management #37
Changes from 1 commit
0e4fa62
953ad35
4697a2e
8df33ec
5f219fe
17b09e4
e49e4a2
d0f9ddd
5ae2d9d
f7b7560
6fa9c4e
933c5f8
7960a49
81e3b04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,13 @@ package api | |
import ( | ||
"context" | ||
"crypto/ecdsa" | ||
"encoding/hex" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/btcsuite/btcd/btcec/v2" | ||
"github.com/ethersphere/bee/v2/pkg/crypto" | ||
"github.com/ethersphere/bee/v2/pkg/jsonhttp" | ||
"github.com/ethersphere/bee/v2/pkg/log" | ||
storer "github.com/ethersphere/bee/v2/pkg/storer" | ||
|
@@ -28,6 +33,16 @@ func setAddressInContext(ctx context.Context, address swarm.Address) context.Con | |
return context.WithValue(ctx, addressKey{}, address) | ||
} | ||
|
||
type GranteesPatchRequest struct { | ||
Addlist []string `json:"add"` | ||
Revokelist []string `json:"revoke"` | ||
} | ||
|
||
type GranteesPatch struct { | ||
Addlist []ecdsa.PublicKey | ||
Revokelist []ecdsa.PublicKey | ||
} | ||
|
||
// actDecryptionHandler is a middleware that looks up and decrypts the given address, | ||
// if the act headers are present | ||
func (s *Service) actDecryptionHandler() func(h http.Handler) http.Handler { | ||
|
@@ -113,3 +128,110 @@ func (s *Service) actEncryptionHandler( | |
|
||
return encryptedReference, nil | ||
} | ||
|
||
func (s *Service) actListGranteesHandler(w http.ResponseWriter, r *http.Request) { | ||
logger := s.logger.WithName("acthandler").Build() | ||
paths := struct { | ||
GranteesAddress swarm.Address `map:"address,resolve" validate:"required"` | ||
}{} | ||
if response := s.mapStructure(r.Header, &paths); response != nil { | ||
response("invalid path params", logger, w) | ||
return | ||
} | ||
grantees, err := s.dac.GetGrantees(r.Context(), paths.GranteesAddress) | ||
if err != nil { | ||
jsonhttp.NotFound(w, "grantee list not found") | ||
return | ||
} | ||
granteeSlice := make([]string, len(grantees)) | ||
for i, grantee := range grantees { | ||
granteeSlice[i] = hex.EncodeToString(crypto.EncodeSecp256k1PublicKey(grantee)) | ||
} | ||
jsonhttp.OK(w, granteeSlice) | ||
} | ||
|
||
func (s *Service) actGrantRevokeHandler(w http.ResponseWriter, r *http.Request) { | ||
logger := s.logger.WithName("acthandler").Build() | ||
|
||
if r.Body == http.NoBody { | ||
logger.Error(nil, "request has no body") | ||
jsonhttp.BadRequest(w, errInvalidRequest) | ||
return | ||
} | ||
|
||
paths := struct { | ||
GranteesAddress swarm.Address `map:"address,resolve" validate:"required"` | ||
}{} | ||
if response := s.mapStructure(r.Header, &paths); response != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace r.Header with mux.Vars |
||
response("invalid path params", logger, w) | ||
return | ||
} | ||
|
||
headers := struct { | ||
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"` | ||
Publisher *ecdsa.PublicKey `map:"Swarm-Act-Publisher" validate:"required"` | ||
HistoryAddress *swarm.Address `map:"Swarm-Act-History-Address"` | ||
}{} | ||
if response := s.mapStructure(r.Header, &headers); response != nil { | ||
response("invalid header params", logger, w) | ||
return | ||
} | ||
|
||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
if jsonhttp.HandleBodyReadError(err, w) { | ||
return | ||
} | ||
logger.Debug("read request body failed", "error", err) | ||
logger.Error(nil, "read request body failed") | ||
jsonhttp.InternalServerError(w, "cannot read request") | ||
return | ||
} | ||
|
||
gpr := GranteesPatchRequest{} | ||
if len(body) > 0 { | ||
err = json.Unmarshal(body, &gpr) | ||
if err != nil { | ||
logger.Debug("unmarshal body failed", "error", err) | ||
logger.Error(nil, "unmarshal body failed") | ||
jsonhttp.InternalServerError(w, "error unmarshaling request body") | ||
return | ||
} | ||
} | ||
|
||
grantees := GranteesPatch{} | ||
for _, g := range gpr.Addlist { | ||
h, _ := hex.DecodeString(g) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add Errror handling |
||
k, _ := btcec.ParsePubKey(h) | ||
grantees.Addlist = append(grantees.Addlist, *k.ToECDSA()) | ||
} | ||
for _, g := range gpr.Revokelist { | ||
h, _ := hex.DecodeString(g) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add Errror handling |
||
k, _ := btcec.ParsePubKey(h) | ||
grantees.Revokelist = append(grantees.Revokelist, *k.ToECDSA()) | ||
} | ||
|
||
tag, _ := s.getOrCreateSessionID(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe read headers.SwarmTag as tagid. |
||
|
||
ctx := r.Context() | ||
putter, _ := s.newStamperPutter(ctx, putterOptions{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add Errror handling |
||
BatchID: headers.BatchID, | ||
TagID: tag, | ||
Pin: false, | ||
Deferred: true, | ||
}) | ||
|
||
granteeref := paths.GranteesAddress | ||
granteeref, historyref, _ := s.dac.HandleGrantees(ctx, granteeref, *headers.HistoryAddress, headers.Publisher, convertToPointerSlice(grantees.Addlist), convertToPointerSlice(grantees.Revokelist)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add Errror handling |
||
putter.Done(granteeref) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add Errror handling |
||
putter.Done(historyref) | ||
jsonhttp.OK(w, nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall be .Created(granteeresponse) with historyref and granteeref in it. |
||
} | ||
|
||
func convertToPointerSlice(slice []ecdsa.PublicKey) []*ecdsa.PublicKey { | ||
pointerSlice := make([]*ecdsa.PublicKey, len(slice)) | ||
for i, key := range slice { | ||
pointerSlice[i] = &key | ||
} | ||
return pointerSlice | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace r.Header with mux.Vars