This repository has been archived by the owner on Jul 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiv1.go
136 lines (110 loc) · 2.66 KB
/
div1.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"syscall"
"authentication"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/crypto/ssh/terminal"
)
const (
BaseUrl = "test.div1.io"
Path = "/test/1/exec"
)
func main() {
app := cli.NewApp()
app.Name = "Div1 Authentication"
app.Usage = "Check user credentials"
app.UsageText = "auth [-u username] [-p password]"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "username, u",
Value: "",
Usage: "`Username` of the account",
},
cli.StringFlag{
Name: "password, p",
Value: "",
Usage: "`Password` of the account [Not recommended, use stdin instead]",
},
}
app.Action = func(c *cli.Context) error {
username, err := usernameHandler(c)
if err != nil {
return err
}
password, err := passwordHandler(c)
if err != nil {
return err
}
auth := authentication.Auth{
BaseUrl: BaseUrl,
SSL: true,
Path: Path,
}
_, err = auth.Authenticate(username, password)
if err != nil {
fmt.Println("\n" + err.Error())
return err
}
fmt.Println(fmt.Sprintf("Hi %s, you are successfully authenticated", username))
return nil
}
app.Run(os.Args)
}
func usernameHandler(c *cli.Context) (string, error) {
var err error
username := ""
// Check that option is used
if c.IsSet("username") {
username = strings.TrimSpace(c.String("username"))
}
// If username is empty, we get it from stdin
if username == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Username: ")
username, err = reader.ReadString('\n')
if err != nil {
msg := "An error occurred reading your username"
fmt.Println(msg)
return "", errors.Wrap(err, msg)
}
if username == "" {
msg := "Username cannot be empty"
fmt.Println(msg)
return "", errors.Wrap(err, msg)
}
username = username[:len(username)-1]
}
return username, nil
}
func passwordHandler(c *cli.Context) (string, error) {
var err error
password := ""
if c.IsSet("password") {
fmt.Println("WARNING: It is not safe, and therefore not recommended to enter the password in command line arguments. Use stdin instead.")
password = c.String("password")
}
// If password is empty, we get it from stdin
if password == "" {
var bytePassword []byte
fmt.Print("Password: ")
// We want to hide the password in terminal
bytePassword, err = terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
msg := "An error occurred reading your password"
fmt.Println(msg)
return "", errors.Wrap(err, msg)
}
password = string(bytePassword)
if password == "" {
msg := "Password cannot be empty"
fmt.Println(msg)
return "", errors.Wrap(err, msg)
}
}
return password, nil
}