/
a3s.go
71 lines (60 loc) · 1.66 KB
/
a3s.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package authcmd
import (
"context"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.aporeto.io/a3s/pkgs/authlib"
"go.aporeto.io/a3s/pkgs/permissions"
"go.aporeto.io/a3s/pkgs/token"
"go.aporeto.io/manipulate/manipcli"
)
func makeA3SCmd(mmaker manipcli.ManipulatorMaker, restrictions *permissions.Restrictions) *cobra.Command {
cmd := &cobra.Command{
Use: "a3s",
Short: "Use an A3S identity token.",
TraverseChildren: true,
RunE: func(cmd *cobra.Command, args []string) error {
fToken := viper.GetString("access-token")
fAudience := viper.GetStringSlice("audience")
fCloak := viper.GetStringSlice("cloak")
fQRCode := viper.GetBool("qrcode")
fCheck := viper.GetBool("check")
fValidity := viper.GetDuration("validity")
fRefresh := viper.GetBool("refresh")
if fToken == "" {
fToken = viper.GetString("token")
}
m, err := mmaker()
if err != nil {
return err
}
client := authlib.NewClient(m)
t, err := client.AuthFromA3S(
context.Background(),
fToken,
authlib.OptAudience(fAudience...),
authlib.OptCloak(fCloak...),
authlib.OptRestrictions(*restrictions),
authlib.OptValidity(fValidity),
authlib.OptRefresh(fRefresh),
)
if err != nil {
return err
}
return token.Fprint(
os.Stdout,
t,
token.PrintOptionDecoded(fCheck),
token.PrintOptionQRCode(fQRCode),
token.PrintOptionRaw(true),
)
},
}
cmd.Flags().String("access-token", "", "Valid a3s token. If , uses --token.")
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
_ = cmd.Flags().MarkHidden("namespace")
cmd.Parent().HelpFunc()(cmd, args)
})
return cmd
}