Skip to content

Commit

Permalink
tools: use bearer token
Browse files Browse the repository at this point in the history
  • Loading branch information
gernest committed Mar 4, 2024
1 parent 3de3504 commit 921f3fb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"net/http/httputil"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/dop251/goja"
"github.com/urfave/cli/v3"
v1 "github.com/vinceanalytics/vince/gen/go/vince/v1"
"github.com/vinceanalytics/vince/internal/cluster/auth"
"github.com/vinceanalytics/vince/internal/ref"
"github.com/vinceanalytics/vince/ua"
"google.golang.org/protobuf/encoding/protojson"
Expand All @@ -40,13 +38,20 @@ func CMD() *cli.Command {
Name: "limit",
Value: 10,
},
&cli.StringFlag{
Name: "authToken",
Usage: "Bearer token to authenticate api calls",
Required: true,
Sources: cli.EnvVars("VINCE_AUTH_TOKEN"),
},
},
Action: func(ctx context.Context, c *cli.Command) error {
vince := c.String("vince")
program := &Program{
Agents: Agents(),
Referrer: Referrer(),
Vince: vince,
Auth: c.String("authToken"),
}
vm := goja.New()
vm.Set("createSession", program.NewSession)
Expand Down Expand Up @@ -81,6 +86,7 @@ type Program struct {
Referrer []string
Agents []string
Vince string
Auth string
}

func (p *Program) NewSession(website string) (*Session, error) {
Expand All @@ -90,6 +96,7 @@ func (p *Program) NewSession(website string) (*Session, error) {
}
domain, _, _ := strings.Cut(u.Host, ":")
return &Session{
Auth: p.Auth,
Ua: p.Agents[rand.Intn(len(p.Agents))],
Referrer: p.Referrer[rand.Intn(len(p.Referrer))],
Domain: domain,
Expand All @@ -99,6 +106,7 @@ func (p *Program) NewSession(website string) (*Session, error) {
}

type Session struct {
Auth string
Ua string
Referrer string
Domain string
Expand Down Expand Up @@ -131,7 +139,7 @@ func (s *Session) send(name, path string, dump bool) error {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", auth.CreateBasicAuth("staples", ""))
req.Header.Set("Authorization", "Bearer "+s.Auth)
if dump {
b, _ := httputil.DumpRequestOut(req, true)
fmt.Println(string(b))
Expand All @@ -142,8 +150,7 @@ func (s *Session) send(name, path string, dump bool) error {
}
defer res.Body.Close()
if res.StatusCode != http.StatusAccepted {
data, _ = io.ReadAll(res.Body)
return errors.New(string(data))
return errors.New(res.Status)
}
return nil
}
2 changes: 2 additions & 0 deletions tools/view
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export VINCE_AUTH_TOKEN=xxxx

./bin/vince load internal/load/testdata/page_view.js

0 comments on commit 921f3fb

Please sign in to comment.