Add login to client
This commit is contained in:
@@ -9,10 +9,13 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/urfave/cli/v2"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -32,19 +35,23 @@ func main() {
|
||||
Name: "config",
|
||||
Usage: "Path to config-file.",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "url",
|
||||
Usage: "Base url of gpaste server",
|
||||
},
|
||||
},
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "upload",
|
||||
Usage: "Upload file(s)",
|
||||
ArgsUsage: "FILE [FILE]...",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "url",
|
||||
Usage: "Base url of gpaste server",
|
||||
},
|
||||
},
|
||||
Action: ActionUpload,
|
||||
Action: ActionUpload,
|
||||
},
|
||||
{
|
||||
Name: "login",
|
||||
Usage: "Login to gpaste server",
|
||||
ArgsUsage: "USERNAME",
|
||||
Action: ActionLogin,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -105,3 +112,71 @@ func ActionUpload(c *cli.Context) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ActionLogin(c *cli.Context) error {
|
||||
username := c.Args().First()
|
||||
if username == "" {
|
||||
return cli.Exit("USERNAME not supplied.", 1)
|
||||
}
|
||||
password, err := readPassword()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading password: %w", err)
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/api/login", c.String("url"))
|
||||
client := &http.Client{}
|
||||
// TODO: Change timeout
|
||||
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
body := new(bytes.Buffer)
|
||||
requestData := struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}{
|
||||
Username: username,
|
||||
Password: password,
|
||||
}
|
||||
encoder := json.NewEncoder(body)
|
||||
if err := encoder.Encode(&requestData); err != nil {
|
||||
return fmt.Errorf("error encoding response: %w", err)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to perform request: %s", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return cli.Exit("got non-ok response from server", 0)
|
||||
}
|
||||
|
||||
responseData := struct {
|
||||
Token string `json:"token"`
|
||||
}{}
|
||||
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
if err := decoder.Decode(&responseData); err != nil {
|
||||
return fmt.Errorf("unable to parse response: %s", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Token: %s", responseData.Token)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readPassword() (string, error) {
|
||||
fmt.Print("Enter Password: ")
|
||||
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
password := string(bytePassword)
|
||||
return strings.TrimSpace(password), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user