/
configure_token.go
97 lines (82 loc) · 2.53 KB
/
configure_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
package cmd
import (
"errors"
"fmt"
"log"
"net/url"
"strings"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/pydio/cells-client/v4/rest"
cells_sdk "github.com/pydio/cells-sdk-go/v5"
)
var configurePersonalAccessTokenCmd = &cobra.Command{
Use: "token",
Short: "Configure Authentication using a Personal Access Token",
Long: `
DESCRIPTION
Configure your Cells Client to connect to your distant server using a Personal Acces Token.
A token can be generated with the 'token' command for a given user on the server side (not in this client),
see 'cells admin user token --help' for further details.
Please beware that the Personal Access Token will be stored in clear text if you do not have a **correctly configured and running** keyring on your client machine.
This is the preferred way to handle authentication between Cells and your client.
`,
Run: func(cmd *cobra.Command, args []string) {
newConf := rest.DefaultCecConfig()
newConf.AuthType = cells_sdk.AuthTypePat
newConf.SkipKeyring = skipKeyring
var err error
if token != "" && serverURL != "" {
// non interactive
newConf.IdToken = token
newConf.Url = serverURL
} else {
// interactive
p := promptui.Prompt{Label: "Server URL", Validate: rest.ValidURL}
newConf.Url, err = p.Run()
if err != nil {
if errors.Is(err, promptui.ErrInterrupt) {
log.Fatalf("operation aborted by user")
}
log.Fatalf("%s URL is not valid %s", promptui.IconBad, err.Error())
}
newConf.Url, err = rest.CleanURL(newConf.Url)
if err != nil {
log.Fatalf("%s %s", promptui.IconBad, err.Error())
}
u, e := url.Parse(newConf.Url)
if e != nil {
log.Fatal("", err)
}
if u.Scheme == "https" {
// PROMPT SKIP VERIFY
p2 := promptui.Select{Label: "Skip SSL Verification? (not recommended)", Items: []string{"No", "Yes"}}
if _, y, e := p2.Run(); y == "Yes" && e == nil {
newConf.SkipVerify = true
}
}
p = promptui.Prompt{Label: "Token", Validate: func(s string) error {
s = strings.TrimSpace(s)
if len(s) == 0 {
return fmt.Errorf("field cannot be empty")
}
return nil
}}
newConf.IdToken, err = p.Run()
if err != nil {
if errors.Is(err, promptui.ErrInterrupt) {
log.Fatalf("operation aborted by user")
}
log.Fatalf(err.Error())
}
}
err = persistConfig(newConf)
if err != nil {
log.Fatalf(err.Error())
}
},
}
func init() {
configureCmd.AddCommand(configurePersonalAccessTokenCmd)
configAddCmd.AddCommand(configurePersonalAccessTokenCmd)
}