Skip to content

Commit

Permalink
fixup auth for invites command
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Apr 14, 2023
1 parent 62ac17d commit cb15837
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 10 deletions.
113 changes: 104 additions & 9 deletions cmd/gosky/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -276,6 +277,7 @@ var syncCmd = &cli.Command{
Subcommands: []*cli.Command{
syncGetRepoCmd,
syncGetRootCmd,
syncListReposCmd,
},
}

Expand Down Expand Up @@ -1011,7 +1013,7 @@ var getRecordCmd = &cli.Command{
}

var createInviteCmd = &cli.Command{
Name: "createInvite",
Name: "createInvites",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "admin-password",
Expand All @@ -1026,39 +1028,132 @@ var createInviteCmd = &cli.Command{
Name: "num",
Value: 1,
},
&cli.StringFlag{
Name: "bulk-dids",
},
},
Action: func(cctx *cli.Context) error {
xrpcc, err := cliutil.GetXrpcClient(cctx, false)
if err != nil {
return err
}

adminKey := cctx.String("admin-password")

count := cctx.Int("useCount")
num := cctx.Int("num")

if bulkfi := cctx.String("bulk-dids"); bulkfi != "" {
xrpcc.AdminToken = &adminKey
dids, err := readDids(bulkfi)
if err != nil {
return err
}

feeder := make(chan string)
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for d := range feeder {
did := d
resp, err := comatproto.ServerCreateInviteCodes(context.TODO(), xrpcc, &comatproto.ServerCreateInviteCodes_Input{
UseCount: int64(count),
ForAccount: &did,
CodeCount: int64(num),
})
if err != nil {
log.Error(err)
}
_ = resp
}
}()
}

for _, d := range dids {
feeder <- d
}

close(feeder)

wg.Wait()
return nil
}

var usrdid *string
if forUser := cctx.Args().Get(0); forUser != "" {
resp, err := comatproto.IdentityResolveHandle(context.TODO(), xrpcc, forUser)
if err != nil {
return err
return fmt.Errorf("resolving handle: %w", err)
}

usrdid = &resp.Did
}

adminKey := cctx.String("admin-password")
xrpcc.AdminToken = &adminKey
resp, err := comatproto.ServerCreateInviteCodes(context.TODO(), xrpcc, &comatproto.ServerCreateInviteCodes_Input{
UseCount: int64(count),
ForAccount: usrdid,
CodeCount: int64(num),
})
if err != nil {
return fmt.Errorf("creating codes: %w", err)
}

for i := 0; i < num; i++ {
resp, err := comatproto.ServerCreateInviteCode(context.TODO(), xrpcc, &comatproto.ServerCreateInviteCode_Input{
UseCount: int64(count),
ForAccount: usrdid,
})
for _, c := range resp.Codes {
fmt.Println(c)
}

return nil
},
}

func readDids(f string) ([]string, error) {
fi, err := os.Open(f)
if err != nil {
return nil, err
}

defer fi.Close()

scan := bufio.NewScanner(fi)
var out []string
for scan.Scan() {
out = append(out, scan.Text())
}

return out, nil
}

var syncListReposCmd = &cli.Command{
Name: "listRepos",
Action: func(cctx *cli.Context) error {
xrpcc, err := cliutil.GetXrpcClient(cctx, false)
if err != nil {
return err
}

var curs string
for {
out, err := comatproto.SyncListRepos(context.TODO(), xrpcc, curs, 1000)
if err != nil {
return err
}

fmt.Println(resp.Code)
if len(out.Repos) == 0 {
break
}

for _, r := range out.Repos {
fmt.Println(r.Did)
}

if out.Cursor == nil {
break
}

curs = *out.Cursor
}

return nil
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ require (
github.com/prometheus/procfs v0.9.0 // indirect
github.com/prometheus/statsd_exporter v0.23.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sashabaranov/go-openai v1.7.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,8 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sashabaranov/go-openai v1.7.0 h1:D1dBXoZhtf/aKNu6WFf0c7Ah2NM30PZ/3Mqly6cZ7fk=
github.com/sashabaranov/go-openai v1.7.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
2 changes: 1 addition & 1 deletion xrpc/xrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (c *Client) Do(ctx context.Context, kind XRPCRequestType, inpenc string, me
}

// use admin auth if we have it configured and are doing a request that requires it
if c.AdminToken != nil && (strings.HasPrefix(method, "com.atproto.admin.") || method == "com.atproto.account.createInviteCode") {
if c.AdminToken != nil && (strings.HasPrefix(method, "com.atproto.admin.") || method == "com.atproto.account.createInviteCode" || method == "com.atproto.server.createInviteCodes") {
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:"+*c.AdminToken)))
} else if c.Auth != nil {
req.Header.Set("Authorization", "Bearer "+c.Auth.AccessJwt)
Expand Down

0 comments on commit cb15837

Please sign in to comment.