/
token.go
173 lines (153 loc) · 4.29 KB
/
token.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Copyright © 2023 Louis Lefebvre <louislefebvre1999@gmail.com>
*/
package cmd
import (
"fmt"
"log"
"slices"
"strings"
. "github.com/louislef299/aws-sso/internal/envs"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
ACTIVE_TOKEN_ID = "active"
INACTIVE_TOKEN_ID = "inactive"
)
// tokenCmd represents the token command
var tokenCmd = &cobra.Command{
Use: "token",
Aliases: []string{"tok", "to", "ken"},
Short: "Manage multiple SSO access tokens",
Long: `Manages multiple cached SSO tokens for reuse. Beneficial
when dealing with multiple AWS accounts.`,
}
// tokensCmd represents the list command
var tokensCmd = &cobra.Command{
Use: "tokens",
Hidden: true,
Short: "List your tokens.",
Run: func(cmd *cobra.Command, args []string) {
listTokens()
},
}
// tokenListCmd represents the list command
var tokenListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List your tokens",
Run: func(cmd *cobra.Command, args []string) {
listTokens()
},
}
// tokenAddCmd represents the add command
var tokenAddCmd = &cobra.Command{
Use: "add",
Short: "Add a token.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
addToken(args[0])
log.Printf("successfully added token %s!\n", args[0])
},
}
// tokenCurrentCmd represents the current command
var tokenCurrentCmd = &cobra.Command{
Use: "current",
Aliases: []string{"cur", "curr"},
Short: "List the current token.",
Run: func(cmd *cobra.Command, args []string) {
log.Println("the current token is", getCurrentToken())
},
}
// tokenRemoveCmd represents the remove command
var tokenRemoveCmd = &cobra.Command{
Use: "remove",
Aliases: []string{"rm"},
Short: "Remove a token.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
t := getToken(args[0])
if t == "" {
log.Printf("token '%s' was not found\n", args[0])
return
} else if strings.Compare(t, ACTIVE_TOKEN_ID) == 0 {
log.Printf("token '%s' is currently active, removing an active token is not allowed\n", args[0])
return
}
removeToken(args[0])
log.Printf("successfully removed token %s!\n", args[0])
},
}
// tokenUseCmd represents the use command
var tokenUseCmd = &cobra.Command{
Use: "use",
Short: "Use a token.",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if !doesTokenExist(args[0]) {
log.Printf("token '%s' was not found\n", args[0])
return
}
setToken(args[0])
log.Printf("using token %s\n", args[0])
},
}
func init() {
rootCmd.AddCommand(tokenCmd)
rootCmd.AddCommand(tokensCmd)
tokenCmd.AddCommand(tokenListCmd)
tokenCmd.AddCommand(tokenAddCmd)
tokenCmd.AddCommand(tokenRemoveCmd)
tokenCmd.AddCommand(tokenCurrentCmd)
tokenCmd.AddCommand(tokenUseCmd)
}
func setToken(name string) {
if c := getCurrentToken(); c != "" {
viper.Set(fmt.Sprintf("%s.%s", TOKEN_HEADER, c), INACTIVE_TOKEN_ID)
}
if name == "-" {
name = DEFAULT_TOKEN_NAME
}
viper.Set(fmt.Sprintf("%s.%s", TOKEN_HEADER, name), ACTIVE_TOKEN_ID)
viper.Set(SESSION_TOKEN, name)
if err := viper.WriteConfig(); err != nil {
log.Fatal("couldn't write config:", err)
}
}
// Quick check to make sure the session token is set
func checkToken() {
if !viper.IsSet(SESSION_TOKEN) || getCurrentToken() == "" {
setToken(DEFAULT_TOKEN_NAME)
}
}
// Returns the current session token
func addToken(name string) {
deepSet(fmt.Sprintf("%s.%s", TOKEN_HEADER, name), ACTIVE_TOKEN_ID)
setToken(name)
}
func getCurrentToken() string { return viper.GetString(SESSION_TOKEN) }
func doesTokenExist(name string) bool { return !(getToken(name) == "") }
func getToken(name string) string { return viper.GetString(fmt.Sprintf("%s.%s", TOKEN_HEADER, name)) }
func listTokens() {
tokens := viper.Sub(TOKEN_HEADER)
fmt.Println("Local Tokens:")
if tokens == nil {
// The default token will always exist
setToken(DEFAULT_TOKEN_NAME)
fmt.Printf("* %s\n", DEFAULT_TOKEN_NAME)
return
}
tokenList := tokens.AllKeys()
slices.Sort(tokenList)
for _, t := range tokenList {
if strings.Compare(tokens.GetString(t), ACTIVE_TOKEN_ID) == 0 {
fmt.Printf("* %s\n", t)
} else if strings.Compare(tokens.GetString(t), INACTIVE_TOKEN_ID) == 0 {
fmt.Printf(" %s\n", t)
}
}
}
func removeToken(name string) {
deepSet(fmt.Sprintf("%s.%s", TOKEN_HEADER, name), "")
}