-
Notifications
You must be signed in to change notification settings - Fork 388
/
auth.go
127 lines (102 loc) · 3.67 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
/*
Copyright 2018 The Doctl Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package commands
import (
"errors"
"fmt"
"os"
"strings"
"golang.org/x/crypto/ssh/terminal"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// ErrUnknownTerminal signifies an unknown terminal. It is returned when doit
// can't ascertain the current terminal type with requesting an auth token.
var ErrUnknownTerminal = errors.New("unknown terminal")
// retrieveUserTokenFromCommandLine is a function that can retrieve a token. By default,
// it will prompt the user. In test, you can replace this with code that returns the appropriate response.
func retrieveUserTokenFromCommandLine() (string, error) {
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
return "", ErrUnknownTerminal
}
fmt.Print("DigitalOcean access token: ")
passwdBytes, err := terminal.ReadPassword(0)
if err != nil {
return "", err
}
return string(passwdBytes), nil
}
// UnknownSchemeError signifies an unknown HTTP scheme.
type UnknownSchemeError struct {
Scheme string
}
var _ error = &UnknownSchemeError{}
func (use *UnknownSchemeError) Error() string {
return "unknown scheme: " + use.Scheme
}
// Auth creates auth commands for doctl.
func Auth() *Command {
cmd := &Command{
Command: &cobra.Command{
Use: "auth",
Short: "auth commands",
Long: "auth is used to access auth commands",
},
}
cmdBuilderWithInit(cmd, RunAuthInit(retrieveUserTokenFromCommandLine), "init", "initialize configuration", Writer, false, docCategories("auth"))
cmdBuilderWithInit(cmd, RunAuthSwitch, "switch", "writes the auth context permanently to config", Writer, false, docCategories("auth"))
return cmd
}
// RunAuthInit initializes the doctl config. Configuration is stored in $XDG_CONFIG_HOME/doctl. On Unix, if
// XDG_CONFIG_HOME is not set, use $HOME/.config. On Windows use %APPDATA%/doctl/config.
func RunAuthInit(retrieveUserTokenFunc func() (string, error)) func(c *CmdConfig) error {
return func(c *CmdConfig) error {
token := c.getContextAccessToken()
if token == "" {
in, err := retrieveUserTokenFunc()
if err != nil {
return fmt.Errorf("unable to read DigitalOcean access token: %s", err)
}
token = strings.TrimSpace(in)
} else {
fmt.Fprintf(c.Out, "Using token [%v]", token)
fmt.Fprintln(c.Out)
}
c.setContextAccessToken(string(token))
fmt.Fprintln(c.Out)
fmt.Fprint(c.Out, "Validating token... ")
// need to initial the godo client since we've changed the configuration.
if err := c.initServices(c); err != nil {
return fmt.Errorf("unable to initialize DigitalOcean API client with new token: %s", err)
}
if _, err := c.Account().Get(); err != nil {
fmt.Fprintln(c.Out, "invalid token")
fmt.Fprintln(c.Out)
return fmt.Errorf("unable to use supplied token to access API: %s", err)
}
fmt.Fprintln(c.Out, "OK")
fmt.Fprintln(c.Out)
return writeConfig()
}
}
// RunAuthSwitch changes the default context and writes it to the
// configuration.
func RunAuthSwitch(c *CmdConfig) error {
context := Context
if context == "" {
context = viper.GetString("context")
}
viper.Set("context", context)
fmt.Printf("Now using context [%s] by default\n", context)
return writeConfig()
}