/
auth.go
245 lines (206 loc) · 5.19 KB
/
auth.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
import (
"encoding/json"
"io/ioutil"
"strconv"
"github.com/BurntSushi/toml"
"github.com/spf13/cobra"
"github.com/bobinette/papernet/auth"
"github.com/bobinette/papernet/auth/cayley"
"github.com/bobinette/papernet/auth/services"
"github.com/bobinette/papernet/jwt"
)
type AuthConfiguration struct {
Auth struct {
KeyPath string `toml:"key"`
Cayley struct {
Store string `toml:"store"`
} `toml:"cayley"`
} `toml:"auth"`
Bolt struct {
Store string `toml:"store"`
} `toml:"bolt"`
}
var (
// Configuration file
authConfig AuthConfiguration
// Other variables
userRepository auth.UserRepository
userService *services.UserService
teamService *services.TeamService
)
func init() {
AuthCommand.AddCommand(&AuthUserCommand)
AuthUserCommand.AddCommand(&AuthTokenCommand)
AuthUserCommand.AddCommand(&AuthAllUsersCommand)
AuthUserCommand.AddCommand(&AuthUpsertUserCommand)
AuthUserCommand.AddCommand(&AuthDeleteCommand)
inheritPersistentPreRun(&AuthCommand)
inheritPersistentPreRun(&AuthUserCommand)
inheritPersistentPreRun(&AuthTokenCommand)
inheritPersistentPreRun(&AuthUpsertUserCommand)
inheritPersistentPreRun(&AuthDeleteCommand)
inheritPersistentPreRun(&AuthAllUsersCommand)
RootCmd.AddCommand(&AuthCommand)
}
var AuthCommand = cobra.Command{
Use: "auth",
Short: "List all the auth command availables",
Long: "List all the auth command availales",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Read configuration file
data, err := ioutil.ReadFile(configFile)
if err != nil {
logger.Fatal("could not read configuration file:", err)
}
err = toml.Unmarshal(data, &authConfig)
if err != nil {
logger.Fatal("error unmarshalling configuration:", err)
}
// Read key file
keyData, err := ioutil.ReadFile(authConfig.Auth.KeyPath)
if err != nil {
logger.Fatal("could not open key file:", err)
}
// Create token encoder
var key struct {
Key string `json:"k"`
}
err = json.Unmarshal(keyData, &key)
if err != nil {
logger.Fatal("could not read key file:", err)
}
tokenEncoder := jwt.NewEncodeDecoder([]byte(key.Key))
// Create user repository
store, err := cayley.NewStore(authConfig.Auth.Cayley.Store)
if err != nil {
logger.Fatal("could not open user graph:", err)
}
userRepository = cayley.NewUserRepository(store)
teamRepository := cayley.NewTeamRepository(store)
// Create user service
userService = services.NewUserService(userRepository, tokenEncoder)
teamService = services.NewTeamService(teamRepository, userRepository)
},
}
var AuthUserCommand = cobra.Command{
Use: "user",
Short: "Retrieve a user based on its id",
Long: "Retrieve a user based on its id",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logger.Fatal("user 1 argument: the id of the user")
}
if args[0] == "help" {
cmd.Help()
return
}
userID, err := strconv.Atoi(args[0])
if err != nil {
logger.Fatal(err)
}
user, err := userService.Get(userID)
if err != nil {
logger.Fatal(err)
}
data, err := json.Marshal(user)
if err != nil {
logger.Fatal(err)
}
cmd.Println(string(data))
},
}
var AuthTokenCommand = cobra.Command{
Use: "token",
Short: "Craft a token for a user",
Long: "Craft a token for a user",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logger.Fatal("user token wants 1 argument: the id of the user")
}
if args[0] == "help" {
cmd.Help()
return
}
userID, err := strconv.Atoi(args[0])
if err != nil {
logger.Fatal(err)
}
token, err := userService.Token(userID)
if err != nil {
logger.Fatal(err)
}
logger.Print(token)
},
}
var AuthDeleteCommand = cobra.Command{
Use: "delete",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logger.Fatal("user token wants 1 argument: the id of the user")
}
if args[0] == "help" {
cmd.Help()
return
}
userID, err := strconv.Atoi(args[0])
if err != nil {
logger.Fatal(err)
}
err = userService.Delete(userID)
if err != nil {
logger.Fatal(err)
}
},
}
var AuthAllUsersCommand = cobra.Command{
Use: "all",
Short: "Retrieve all the users",
Long: "Retrieve all the users",
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 0 && args[0] == "help" {
cmd.Help()
return
}
users, err := userService.All()
if err != nil {
logger.Fatal(err)
}
data, err := json.Marshal(users)
if err != nil {
logger.Fatal(err)
}
cmd.Println(string(data))
},
}
var AuthUpsertUserCommand = cobra.Command{
Use: "upsert",
Short: "Upsert a user from a json",
Long: "Upsert a user from a json",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
logger.Fatalf("user upsert wants 1 argument: the json to upsert the user, got %v", args)
}
if args[0] == "help" {
cmd.Help()
return
}
var user auth.User
err := json.Unmarshal([]byte(args[0]), &user)
if err != nil {
logger.Fatal(err)
}
user, err = userService.Upsert(user)
if err != nil {
logger.Fatal(err)
}
data, err := json.Marshal(user)
if err != nil {
logger.Fatal(err)
}
cmd.Println("user upserted: ", string(data))
},
}