...
1 package bslauth
2
3 import (
4 "encoding/json"
5 "flag"
6 "fmt"
7 "os"
8 "testing"
9
10 "edge-infra.dev/test/f2"
11 "edge-infra.dev/test/f2/fctx"
12 )
13
14
15
16
17 type Identifier string
18
19
20 const (
21 BannerAdmin Identifier = "banner-admin"
22 )
23
24 type BSLAuth struct {
25 APIEndpoint string
26 users map[string]User
27 }
28
29
30 func (b *BSLAuth) User(id Identifier) User {
31 return b.users[string(id)]
32 }
33
34
35 type User struct {
36 Username string
37 Password string
38 Org string `json:"organization"`
39 }
40
41 func FromContext(ctx fctx.Context) (*BSLAuth, error) {
42 v := fctx.ValueFrom[BSLAuth](ctx)
43 if v == nil {
44 return nil, fmt.Errorf("%w: bslAuth extension", fctx.ErrNotFound)
45 }
46 return v, nil
47 }
48
49 func FromContextT(ctx fctx.Context, t *testing.T) *BSLAuth {
50 return fctx.ValueFromT[BSLAuth](ctx, t)
51 }
52
53 func New() *BSLAuth {
54 bslAuth := &BSLAuth{}
55 return bslAuth
56 }
57
58 func (b *BSLAuth) RegisterFns(f f2.Framework) {
59 f.Setup(func(ctx f2.Context) (f2.Context, error) {
60 creds, err := os.ReadFile("/mnt/bslcreds/bslcreds")
61
62 if err != nil {
63 return ctx, fmt.Errorf("Unable to read BSL credentials: %w", err)
64 }
65
66 err = json.Unmarshal(creds, &b.users)
67 return ctx, err
68 })
69 }
70
71
72 func (b *BSLAuth) BindFlags(fs *flag.FlagSet) {
73 fs.StringVar(&b.APIEndpoint,
74 "api-endpoint", "", "edge api endpoint",
75 )
76 }
77
78
79 func (b *BSLAuth) IntoContext(ctx fctx.Context) fctx.Context {
80 return fctx.ValueInto(ctx, b)
81 }
82
View as plain text