|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + // Packages |
| 8 | + tablewriter "github.com/djthorpe/go-tablewriter" |
| 9 | + client "github.com/mutablelogic/go-client" |
| 10 | + auth "github.com/mutablelogic/go-server/pkg/handler/auth/client" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + authClient *auth.Client |
| 15 | + authName = "tokenauth" |
| 16 | + authDuration time.Duration |
| 17 | +) |
| 18 | + |
| 19 | +func authRegister(flags *Flags) { |
| 20 | + // Register flags |
| 21 | + flags.String(authName, "tokenauth-endpoint", "${TOKENAUTH_ENDPOINT}", "tokenauth endpoint (ie, http://host/api/auth/)") |
| 22 | + flags.String(authName, "tokenauth-token", "${TOKENAUTH_TOKEN}", "tokenauth token") |
| 23 | + flags.Duration(authName, "expiry", 0, "token expiry duration") |
| 24 | + |
| 25 | + // Register commands |
| 26 | + flags.Register(Cmd{ |
| 27 | + Name: authName, |
| 28 | + Description: "Manage token authentication", |
| 29 | + Parse: authParse, |
| 30 | + Fn: []Fn{ |
| 31 | + // Default caller |
| 32 | + {Call: authList, Description: "List authentication tokens"}, |
| 33 | + {Name: "list", Call: authList, Description: "List authentication tokens"}, |
| 34 | + {Name: "create", Call: authCreate, Description: "Create a token", MinArgs: 1}, |
| 35 | + {Name: "delete", Call: authDelete, Description: "Delete a token", MinArgs: 1, MaxArgs: 1}, |
| 36 | + }, |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +func authParse(flags *Flags, opts ...client.ClientOpt) error { |
| 41 | + endpoint := flags.GetString("tokenauth-endpoint") |
| 42 | + if token := flags.GetString("tokenauth-token"); token != "" { |
| 43 | + opts = append(opts, client.OptReqToken(client.Token{ |
| 44 | + Scheme: "Bearer", |
| 45 | + Value: token, |
| 46 | + })) |
| 47 | + } |
| 48 | + |
| 49 | + if duration := flags.GetString("expiry"); duration != "" { |
| 50 | + if d, err := time.ParseDuration(duration); err != nil { |
| 51 | + return err |
| 52 | + } else { |
| 53 | + authDuration = d |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if client, err := auth.New(endpoint, opts...); err != nil { |
| 58 | + return err |
| 59 | + } else { |
| 60 | + authClient = client |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func authList(_ context.Context, w *tablewriter.Writer, _ []string) error { |
| 66 | + tokens, err := authClient.List() |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + return w.Write(tokens) |
| 71 | +} |
| 72 | + |
| 73 | +func authCreate(_ context.Context, w *tablewriter.Writer, params []string) error { |
| 74 | + name := params[0] |
| 75 | + scopes := params[1:] |
| 76 | + token, err := authClient.Create(name, authDuration, scopes...) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + return w.Write(token) |
| 81 | +} |
| 82 | + |
| 83 | +func authDelete(ctx context.Context, w *tablewriter.Writer, params []string) error { |
| 84 | + name := params[0] |
| 85 | + err := authClient.Delete(name) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + return authList(ctx, w, nil) |
| 90 | +} |
0 commit comments