Add a listAliases muxrpc endpoint (#320)

* Add a listAliases muxrpc endpoint

* Remove unwanted error handling
This commit is contained in:
Filip Borkiewicz 2022-08-19 12:47:33 -07:00 committed by GitHub
parent 0535ba14e0
commit 92681a9c49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 114 additions and 0 deletions

View File

@ -147,3 +147,43 @@ func (h Handler) Revoke(ctx context.Context, req *muxrpc.Request) (interface{},
return true, nil
}
func (h Handler) List(ctx context.Context, req *muxrpc.Request) (interface{}, error) {
var args []string
err := json.Unmarshal(req.RawArgs, &args)
if err != nil {
return nil, fmt.Errorf("listAlias: bad request: %w", err)
}
if n := len(args); n != 1 {
return nil, fmt.Errorf("listAlias: expected one argument got %d", n)
}
ref, err := refs.ParseFeedRef(args[0])
if err != nil {
return nil, fmt.Errorf("listAlias: invalid feed ref: %w", err)
}
allAliases, err := h.db.List(ctx)
if err != nil {
return nil, fmt.Errorf("listAlias: could not list aliases: %w", err)
}
var filteredAliases []roomdb.Alias
for _, alias := range allAliases {
if alias.Feed.Equal(ref) {
filteredAliases = append(filteredAliases, alias)
}
}
return aliasesToListOfAliasStrings(filteredAliases), nil
}
func aliasesToListOfAliasStrings(aliases []roomdb.Alias) []string {
result := make([]string, 0)
for _, alias := range aliases {
result = append(result, alias.Name)
}
return result
}

View File

@ -123,3 +123,75 @@ func TestAliasRegister(t *testing.T) {
}
cancel()
}
func TestListAliases(t *testing.T) {
testInit(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
r := require.New(t)
a := assert.New(t)
// make a random test key
appKey := make([]byte, 32)
rand.Read(appKey)
netOpts := []roomsrv.Option{
roomsrv.WithAppKey(appKey),
roomsrv.WithContext(ctx),
}
theBots := []*testSession{}
session := makeNamedTestBot(t, "srv", ctx, netOpts)
theBots = append(theBots, session)
bobsKey, err := keys.NewKeyPair(nil)
r.NoError(err)
bobSession := makeNamedTestBot(t, "bob", ctx, append(netOpts,
roomsrv.WithKeyPair(bobsKey),
))
theBots = append(theBots, bobSession)
_, err = session.srv.Members.Add(ctx, bobSession.srv.Whoami(), roomdb.RoleMember)
r.NoError(err)
// allow bots to dial the remote
// side-effect of re-using a room-server as the client
_, err = bobSession.srv.Members.Add(ctx, session.srv.Whoami(), roomdb.RoleMember)
r.NoError(err)
// should work (we allowed A)
err = bobSession.srv.Network.Connect(ctx, session.srv.Network.GetListenAddr())
r.NoError(err, "connect A to the Server")
t.Log("letting handshaking settle..")
time.Sleep(1 * time.Second)
clientForServer, ok := bobSession.srv.Network.GetEndpointFor(session.srv.Whoami())
r.True(ok)
var testReg aliases.Registration
testReg.Alias = "bob"
testReg.RoomID = session.srv.Whoami()
testReg.UserID = bobSession.srv.Whoami()
confirmation := testReg.Sign(bobsKey.Pair.Secret)
sig := base64.StdEncoding.EncodeToString(confirmation.Signature) + ".sig.ed25519"
var response string
err = clientForServer.Async(ctx, &response, muxrpc.TypeString, muxrpc.Method{"room", "listAliases"}, bobsKey.Feed.Ref())
r.NoError(err)
a.Equal("[]", response, "initially the list of aliases should be empty")
err = clientForServer.Async(ctx, &response, muxrpc.TypeString, muxrpc.Method{"room", "registerAlias"}, "bob", sig)
r.NoError(err)
a.NotEqual("", response, "response isn't empty")
err = clientForServer.Async(ctx, &response, muxrpc.TypeString, muxrpc.Method{"room", "listAliases"}, bobsKey.Feed.Ref())
r.NoError(err)
a.Equal("[\"bob\"]", response, "new alias should be in the list")
}

View File

@ -61,6 +61,7 @@ func (s *Server) initHandlers() {
var method = muxrpc.Method{"room"}
mux.RegisterAsync(append(method, "registerAlias"), typemux.AsyncFunc(aliasHandler.Register))
mux.RegisterAsync(append(method, "revokeAlias"), typemux.AsyncFunc(aliasHandler.Revoke))
mux.RegisterAsync(append(method, "listAliases"), typemux.AsyncFunc(aliasHandler.List))
method = muxrpc.Method{"httpAuth"}
mux.RegisterAsync(append(method, "invalidateAllSolutions"), typemux.AsyncFunc(siwssbHandler.InvalidateAllSolutions))

View File

@ -41,6 +41,7 @@ const manifest manifestHandler = `
"room": {
"registerAlias": "async",
"revokeAlias": "async",
"listAliases": "async",
"connect": "duplex",
"attendants": "source",