all repos — honk @ 2657bd242c04c2e5f572e488547e9c7a4eb170fc

my fork of honk

toys/gettoken.go (view raw)

 1package main
 2
 3import (
 4	"flag"
 5	"fmt"
 6	"io/ioutil"
 7	"log"
 8	"net/http"
 9	"net/url"
10	"os"
11	"strings"
12)
13
14var debugMode = false
15
16func main() {
17	server := ""
18	username := ""
19	password := ""
20
21	flag.StringVar(&server, "server", server, "server to connnect")
22	flag.StringVar(&username, "username", username, "username to use")
23	flag.StringVar(&password, "password", password, "password to use")
24	flag.BoolVar(&debugMode, "debug", debugMode, "debug mode")
25	flag.Parse()
26
27	if server == "" || username == "" || password == "" {
28		flag.Usage()
29		os.Exit(1)
30	}
31
32	form := make(url.Values)
33	form.Add("username", username)
34	form.Add("password", password)
35	form.Add("gettoken", "1")
36	loginurl := fmt.Sprintf("https://%s/dologin", server)
37	req, err := http.NewRequest("POST", loginurl, strings.NewReader(form.Encode()))
38	if err != nil {
39		log.Fatal(err)
40	}
41	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
42
43	client := http.DefaultClient
44	if debugMode {
45		client = debugClient
46	}
47
48	resp, err := client.Do(req)
49	if err != nil {
50		log.Fatal(err)
51	}
52	defer resp.Body.Close()
53	answer, err := ioutil.ReadAll(resp.Body)
54	if err != nil {
55		log.Fatal(err)
56	}
57	if resp.StatusCode != 200 {
58		log.Fatalf("status: %d: %s", resp.StatusCode, answer)
59	}
60	fmt.Println(string(answer))
61}