From ef211d6521f3af227d71b1957c7a44b2a630a2c3 Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Sun, 26 Jan 2020 10:53:13 -0600 Subject: rough integration code --- .gitignore | 1 + test/integration/sipcli/main.go | 82 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 test/integration/sipcli/main.go diff --git a/.gitignore b/.gitignore index 2f043b4..1c7d6bd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ deploy/* *.swo vpnweb public/* +test/integration/sipcli/sipcli diff --git a/test/integration/sipcli/main.go b/test/integration/sipcli/main.go new file mode 100644 index 0000000..163d21d --- /dev/null +++ b/test/integration/sipcli/main.go @@ -0,0 +1,82 @@ +package main + +import ( + "0xacab.org/leap/vpnweb/pkg/auth/sip2" + "encoding/json" + "flag" + "fmt" + "io/ioutil" + "log" + "net/http" + "strings" +) + +const authURI string = "http://%s:%s/3/auth" +const certURI string = "http://%s:%s/3/cert" + +func formatCredentials(user, pass string) (string, error) { + c := sip2.Credentials{user, pass} + credJson, err := json.Marshal(c) + if err != nil { + return "", err + } + return string(credJson), nil +} + +func getToken(credJson, host, port string) string { + resp, err := http.Post(fmt.Sprintf(authURI, host, port), "text/json", strings.NewReader(credJson)) + if err != nil { + log.Fatal("Error on auth request: ", err) + } + defer resp.Body.Close() + if resp.StatusCode == 401 { + log.Println("401 UNAUTHORIZED") + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal("Cannot read response body") + } + return string(body) +} + +func getCert(token, host, port string) string { + req, err := http.NewRequest("POST", fmt.Sprintf(certURI, host, port), strings.NewReader("")) + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token)) + resp, err := http.DefaultClient.Do(req) + if err != nil { + log.Fatal("cannot read response body") + } + defer resp.Body.Close() + if resp.StatusCode == 401 { + log.Println("401 UNAUTHORIZED") + } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal("Cannot read response body") + } + return string(body) +} + +func doAuthenticate(user, pass, host, port string) { + credJson, err := formatCredentials(user, pass) + if err != nil { + log.Fatal("Cannot encode credentials: ", err) + } + token := getToken(credJson, host, port) + log.Println("token:", token) + cert := getCert(token, host, port) + log.Println(cert) +} + +func main() { + var host, port, user, pass string + flag.StringVar(&host, "host", "localhost", "Server to connect") + flag.StringVar(&port, "port", "8000", "port to connect") + flag.StringVar(&user, "user", "", "sip user to authenticate") + flag.StringVar(&pass, "pass", "", "sip password to authenticate") + flag.Parse() + + log.Println("connect to", host, port, "with credentials", user, ":", pass) + doAuthenticate(user, pass, host, port) + +} -- cgit v1.2.3