summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkali kaneko (leap communications) <kali@leap.se>2020-01-26 10:53:13 -0600
committerkali kaneko (leap communications) <kali@leap.se>2020-01-26 10:53:13 -0600
commitef211d6521f3af227d71b1957c7a44b2a630a2c3 (patch)
tree6e8fcb48ee3ee963f270e959b91bdcd4bda9cfc1
parentd35f3e153496f21ff89bc0f08e0dc436766c48f0 (diff)
rough integration codesip-auth
-rw-r--r--.gitignore1
-rw-r--r--test/integration/sipcli/main.go82
2 files changed, 83 insertions, 0 deletions
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)
+
+}