Skip to content

Commit

Permalink
Merge pull request #105 from ssb-ngi-pointer/update-invites
Browse files Browse the repository at this point in the history
Add support for invite consumption via JSON
  • Loading branch information
cryptix authored Mar 30, 2021
2 parents a67063a + 377f351 commit 6409c79
Show file tree
Hide file tree
Showing 41 changed files with 492 additions and 355 deletions.
14 changes: 7 additions & 7 deletions cmd/insert-user/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ func main() {
check(err)

var (
name string
login string
pubKey *refs.FeedRef
role roomdb.Role = roomdb.RoleAdmin
repoPath string
)

flag.StringVar(&name, "name", "", "username (used when logging into the room's web ui)")
flag.StringVar(&login, "login", "", "username (used when logging into the room's web ui)")
flag.Func("key", "the public key of the user, format: @<base64-encoded public-key>.ed25519", func(val string) error {
if len(val) == 0 {
return fmt.Errorf("the public key is required. if you are just testing things out, generate one by running 'cmd/insert-user/generate-fake-id.sh'\n")
Expand Down Expand Up @@ -73,8 +73,8 @@ func main() {
cliMissingArguments("please provide the default arguments -name and -key")
}

if name == "" {
cliMissingArguments("please provide a username with -name <username>")
if login == "" {
cliMissingArguments("please provide a username with -login <username>")
}

if pubKey == nil {
Expand All @@ -101,13 +101,13 @@ func main() {
}

ctx := context.Background()
mid, err := db.Members.Add(ctx, name, *pubKey, role)
mid, err := db.Members.Add(ctx, *pubKey, role)
check(err)

err = db.AuthFallback.Create(ctx, mid, name, bytePassword)
err = db.AuthFallback.Create(ctx, mid, login, bytePassword)
check(err)

fmt.Fprintf(os.Stderr, "Created member %s (%s) with ID %d\n", name, role, mid)
fmt.Fprintf(os.Stderr, "Created member %s (%s) with ID %d\n", login, role, mid)
}

func cliMissingArguments(message string) {
Expand Down
4 changes: 3 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func runroomsrv() error {
if !development {
return fmt.Errorf("https-domain can't be empty. See '%s -h' for a full list of options", os.Args[0])
}
httpsDomain = "dev.testing.local"
httpsDomain = "localhost"
}

// validate listen addresses to bail out on invalid flag input before doing anything else
Expand Down Expand Up @@ -254,6 +254,8 @@ func runroomsrv() error {
PortHTTPS: uint(portHTTP),
PortMUXRPC: uint(portMUXRPC),
RoomID: roomsrv.Whoami(),

Development: development,
},
roomsrv.StateManager,
roomsrv.Network,
Expand Down
2 changes: 2 additions & 0 deletions internal/network/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type ServerEndpointDetails struct {
RoomID refs.FeedRef

Domain string

Development bool
}

// MultiserverAddress returns net:domain:muxport~shs:roomPubKeyInBase64
Expand Down
4 changes: 2 additions & 2 deletions muxrpc/test/go/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ func TestAliasRegister(t *testing.T) {
})

// adds
_, err = srvMembers.Add(ctx, "bob", bob.Whoami(), roomdb.RoleMember)
_, err = srvMembers.Add(ctx, bob.Whoami(), roomdb.RoleMember)
r.NoError(err)

// allow bots to dial the remote
// side-effect of re-using a room-server as the client
_, err = bobsMembers.Add(ctx, "the-room", serv.Whoami(), roomdb.RoleMember)
_, err = bobsMembers.Add(ctx, serv.Whoami(), roomdb.RoleMember)
r.NoError(err)

// should work (we allowed A)
Expand Down
16 changes: 8 additions & 8 deletions muxrpc/test/go/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ func TestTunnelServerSimple(t *testing.T) {
botB := theBots[2].Server

// allow both clients
theBots[0].Members.Add(ctx, "botA", botA.Whoami(), roomdb.RoleMember)
theBots[0].Members.Add(ctx, "botB", botB.Whoami(), roomdb.RoleMember)
theBots[0].Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
theBots[0].Members.Add(ctx, botB.Whoami(), roomdb.RoleMember)

// allow bots to dial the remote
theBots[1].Members.Add(ctx, "srv", serv.Whoami(), roomdb.RoleMember)
theBots[2].Members.Add(ctx, "srv", serv.Whoami(), roomdb.RoleMember)
theBots[1].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
theBots[2].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)

// dial up B->A and C->A

Expand Down Expand Up @@ -154,12 +154,12 @@ func TestRoomAnnounce(t *testing.T) {
botB := theBots[2].Server

// allow both clients
theBots[0].Members.Add(ctx, "botA", botA.Whoami(), roomdb.RoleMember)
theBots[0].Members.Add(ctx, "botB", botB.Whoami(), roomdb.RoleMember)
theBots[0].Members.Add(ctx, botA.Whoami(), roomdb.RoleMember)
theBots[0].Members.Add(ctx, botB.Whoami(), roomdb.RoleMember)

// allow bots to dial the remote
theBots[1].Members.Add(ctx, "srv", serv.Whoami(), roomdb.RoleMember)
theBots[2].Members.Add(ctx, "srv", serv.Whoami(), roomdb.RoleMember)
theBots[1].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)
theBots[2].Members.Add(ctx, serv.Whoami(), roomdb.RoleMember)

// should work (we allowed A)
err := botA.Network.Connect(ctx, serv.Network.GetListenAddr())
Expand Down
2 changes: 1 addition & 1 deletion muxrpc/test/nodejs/aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestGoServerJSClientAliases(t *testing.T) {
var aliasesDB = &mockdb.FakeAliasesService{}
srv := ts.startGoServer(membersDB, aliasesDB)
// allow all peers (there arent any we dont want to allow)
membersDB.GetByFeedReturns(roomdb.Member{Nickname: "free4all"}, nil)
membersDB.GetByFeedReturns(roomdb.Member{ID: 1234}, nil)

// setup mocks for this test
aliasesDB.RegisterReturns(nil)
Expand Down
4 changes: 2 additions & 2 deletions muxrpc/test/nodejs/tunnel_connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestGoServerLegacyJSClient(t *testing.T) {
var aliases = &mockdb.FakeAliasesService{}
srv := ts.startGoServer(membersDB, aliases)
// allow all peers (there arent any we dont want to allow)
membersDB.GetByFeedReturns(roomdb.Member{Nickname: "free4all"}, nil)
membersDB.GetByFeedReturns(roomdb.Member{ID: 1234}, nil)

alice := ts.startJSClient("alice", "./testscripts/legacy_client.js",
srv.Network.GetListenAddr(),
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestModernJSClient(t *testing.T) {
var membersDB = &mockdb.FakeMembersService{}
var aliasesDB = &mockdb.FakeAliasesService{}
srv := ts.startGoServer(membersDB, aliasesDB)
membersDB.GetByFeedReturns(roomdb.Member{Nickname: "free4all"}, nil)
membersDB.GetByFeedReturns(roomdb.Member{ID: 1234}, nil)

// allow all peers (there arent any we dont want to allow in this test)

Expand Down
4 changes: 2 additions & 2 deletions roomdb/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type AuthWithSSBService interface {
// MembersService stores and retreives the list of internal users (members, mods and admins).
type MembersService interface {
// Add adds a new member
Add(_ context.Context, nickName string, pubKey refs.FeedRef, r Role) (int64, error)
Add(_ context.Context, pubKey refs.FeedRef, r Role) (int64, error)

// GetByID returns the member if it exists
GetByID(context.Context, int64) (Member, error)
Expand Down Expand Up @@ -125,7 +125,7 @@ type InvitesService interface {
// Create creates a new invite for a new member. It returns the token or an error.
// createdBy is user ID of the admin or moderator who created it.
// aliasSuggestion is optional (empty string is fine) but can be used to disambiguate open invites. (See https://github.com/ssb-ngi-pointer/rooms2/issues/21)
Create(ctx context.Context, createdBy int64, aliasSuggestion string) (string, error)
Create(ctx context.Context, createdBy int64) (string, error)

// Consume checks if the passed token is still valid.
// If it is it adds newMember to the members of the room and invalidates the token.
Expand Down
18 changes: 8 additions & 10 deletions roomdb/mockdb/invites.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 12 additions & 14 deletions roomdb/mockdb/members.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion roomdb/sqlite/aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestAliases(t *testing.T) {
r.Error(err)

// allow the member
_, err = db.Members.Add(ctx, "flaky's nick", newMember, roomdb.RoleMember)
_, err = db.Members.Add(ctx, newMember, roomdb.RoleMember)
r.NoError(err)

err = db.Aliases.Register(ctx, testName, newMember, testSig)
Expand Down
22 changes: 4 additions & 18 deletions roomdb/sqlite/invites.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ type Invites struct {
// createdBy is user ID of the admin or moderator who created it.
// aliasSuggestion is optional (empty string is fine) but can be used to disambiguate open invites. (See https://github.com/ssb-ngi-pointer/rooms2/issues/21)
// The returned token is base64 URL encoded and has inviteTokenLength when decoded.
func (i Invites) Create(ctx context.Context, createdBy int64, aliasSuggestion string) (string, error) {
func (i Invites) Create(ctx context.Context, createdBy int64) (string, error) {
var newInvite = models.Invite{
CreatedBy: createdBy,
AliasSuggestion: aliasSuggestion,
CreatedBy: createdBy,
}

tokenBytes := make([]byte, inviteTokenLength)
Expand Down Expand Up @@ -106,12 +105,7 @@ func (i Invites) Consume(ctx context.Context, token string, newMember refs.FeedR
return err
}

memberNick := time.Now().Format("new-member 2006-01-02")
memberNick += "(invited by:" + entry.R.CreatedByMember.Nick + ")"
if entry.AliasSuggestion != "" {
memberNick = entry.AliasSuggestion
}
_, err = i.members.add(ctx, tx, memberNick, newMember, roomdb.RoleMember)
_, err = i.members.add(ctx, tx, newMember, roomdb.RoleMember)
if err != nil {
return err
}
Expand All @@ -125,10 +119,8 @@ func (i Invites) Consume(ctx context.Context, token string, newMember refs.FeedR

inv.ID = entry.ID
inv.CreatedAt = entry.CreatedAt
inv.AliasSuggestion = entry.AliasSuggestion
inv.CreatedBy.ID = entry.R.CreatedByMember.ID
inv.CreatedBy.Role = roomdb.Role(entry.R.CreatedByMember.Role)
inv.CreatedBy.Nickname = entry.R.CreatedByMember.Nick

return nil
})
Expand Down Expand Up @@ -162,7 +154,7 @@ func (i Invites) GetByToken(ctx context.Context, token string) (roomdb.Invite, e
}

entry, err := models.Invites(
qm.Where("active = true AND token = ?", ht),
qm.Where("active = true AND hashed_token = ?", ht),
qm.Load("CreatedByMember"),
).One(ctx, i.db)
if err != nil {
Expand All @@ -174,10 +166,8 @@ func (i Invites) GetByToken(ctx context.Context, token string) (roomdb.Invite, e

inv.ID = entry.ID
inv.CreatedAt = entry.CreatedAt
inv.AliasSuggestion = entry.AliasSuggestion
inv.CreatedBy.ID = entry.R.CreatedByMember.ID
inv.CreatedBy.Role = roomdb.Role(entry.R.CreatedByMember.Role)
inv.CreatedBy.Nickname = entry.R.CreatedByMember.Nick

return inv, nil
}
Expand All @@ -198,10 +188,8 @@ func (i Invites) GetByID(ctx context.Context, id int64) (roomdb.Invite, error) {

inv.ID = entry.ID
inv.CreatedAt = entry.CreatedAt
inv.AliasSuggestion = entry.AliasSuggestion
inv.CreatedBy.ID = entry.R.CreatedByMember.ID
inv.CreatedBy.Role = roomdb.Role(entry.R.CreatedByMember.Role)
inv.CreatedBy.Nickname = entry.R.CreatedByMember.Nick

return inv, nil
}
Expand All @@ -224,9 +212,7 @@ func (i Invites) List(ctx context.Context) ([]roomdb.Invite, error) {
var inv roomdb.Invite
inv.ID = e.ID
inv.CreatedAt = e.CreatedAt
inv.AliasSuggestion = e.AliasSuggestion
inv.CreatedBy.ID = e.R.CreatedByMember.ID
inv.CreatedBy.Nickname = e.R.CreatedByMember.Nick

invs[idx] = inv
}
Expand Down
Loading

0 comments on commit 6409c79

Please sign in to comment.