forked from TheThingsArchive/ttn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
138 lines (119 loc) · 3.38 KB
/
user.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
// Copyright © 2016 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/TheThingsNetwork/ttn/ttnctl/util"
"github.com/howeyc/gopass"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// userCmd represents the users command
var userCmd = &cobra.Command{
Use: "user",
Short: "Show the current user",
Long: `ttnctl user shows the current logged on user`,
Run: func(cmd *cobra.Command, args []string) {
t, err := util.LoadAuth(viper.GetString("ttn-account-server"))
if err != nil {
ctx.WithError(err).Fatal("Failed to load authentication token")
}
if t == nil {
ctx.Warn("No login found. Please login with ttnctl user login [e-mail]")
return
}
ctx.Infof("Logged on as %s", t.Email)
},
}
var userCreateCmd = &cobra.Command{
Use: "create [e-mail]",
Short: "Create a new user",
Long: `ttnctl user create allows you to create a new user`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Help()
return
}
email := args[0]
fmt.Print("Password: ")
password, err := gopass.GetPasswd()
if err != nil {
ctx.Fatal(err.Error())
}
fmt.Print("Confirm password: ")
password2, err := gopass.GetPasswd()
if err != nil {
ctx.Fatal(err.Error())
}
if !bytes.Equal(password, password2) {
ctx.Fatal("Passwords do not match")
}
uri := fmt.Sprintf("%s/register", viper.GetString("ttn-account-server"))
values := url.Values{
"email": {email},
"password": {string(password)},
}
req, err := http.NewRequest("POST", uri, strings.NewReader(values.Encode()))
if err != nil {
ctx.WithError(err).Fatalf("Failed to create request")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
ctx.WithError(err).Fatal("Registration failed")
}
if res.StatusCode != http.StatusCreated {
buf, _ := ioutil.ReadAll(res.Body)
ctx.Fatalf("Registration failed: %s (%v)", res.Status, string(buf))
}
ctx.Info("User created")
},
}
var userLoginCmd = &cobra.Command{
Use: "login [e-mail]",
Short: "Login",
Long: `ttnctl user login allows you to login`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Help()
return
}
email := args[0]
fmt.Print("Password: ")
password, err := gopass.GetPasswd()
if err != nil {
ctx.Fatal(err.Error())
}
server := viper.GetString("ttn-account-server")
_, err = util.Login(server, email, string(password))
if err != nil {
ctx.Info(fmt.Sprintf("Visit %s to register or to retrieve your account credentials.", server))
ctx.WithError(err).Fatal("Failed to login")
}
ctx.Infof("Logged in as %s and persisted token in %s", email, util.AuthsFileName)
},
}
var userLogoutCmd = &cobra.Command{
Use: "logout",
Short: "Logout the current user",
Long: `ttnctl user logout logs out the current user`,
Run: func(cmd *cobra.Command, args []string) {
if err := util.Logout(viper.GetString("ttn-account-server")); err != nil {
ctx.WithError(err).Fatal("Failed to log out")
}
ctx.Info("Logged out")
},
}
func init() {
RootCmd.AddCommand(userCmd)
userCmd.AddCommand(userCreateCmd)
userCmd.AddCommand(userLoginCmd)
userCmd.AddCommand(userLogoutCmd)
}