summaryrefslogtreecommitdiff
path: root/pkg/auth/sip2/auth.go
blob: 1d3f309d7232b73a7ffea339016aa25d62f74038 (plain)
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
package sip2

import (
	"encoding/json"
	jwt "github.com/dgrijalva/jwt-go"
	"log"
	"net/http"
	"time"

	"0xacab.org/leap/vpnweb/pkg/config"
)

const LibraryLocation string = "testlibrary"
const SipUser string = "leap"
const SipPasswd string = "Kohapassword1!"

// XXX duplicated, pass in opts
var jwtSigningSecret = []byte("thesingingkey")

type Credentials struct {
	User     string
	Password string
}

func SipAuthenticator(opts *config.Opts) http.HandlerFunc {
	log.Println("Initializing sip2 authenticator...")

	/* TODO -- should pass specific SIP options as a secondary struct */
	/* TODO -- catch connection errors */

	sip := NewClient("localhost", "6001", LibraryLocation)

	ok, err := sip.Connect()
	if err != nil {
		log.Fatal("cannot connect sip client")
	}
	ok = sip.Login(SipUser, SipPasswd)
	if !ok {
		log.Println("Error on SIP login")
	} else {
		log.Println("SIP login ok")
	}

	var authTokenHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var c Credentials

		err := json.NewDecoder(r.Body).Decode(&c)
		if err != nil {
			log.Println("Auth request did not send valid json")
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}

		if c.User == "" || c.Password == "" {
			log.Println("Auth request did not include user or password")
			http.Error(w, "missing user and/or password", http.StatusBadRequest)
			return
		}

		valid := sip.CheckCredentials(c.User, c.Password)
		if !valid {
			log.Println("Wrong auth for user", c.User)
			http.Error(w, "wrong user and/or password", http.StatusUnauthorized)
			return
		}

		log.Println("Valid auth for user", c.User)
		token := jwt.New(jwt.SigningMethodHS256)
		claims := token.Claims.(jwt.MapClaims)
		/* maybe no uid at all */
		claims["uid"] = "user"
		claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
		tokenString, _ := token.SignedString(jwtSigningSecret)
		w.Write([]byte(tokenString))
	})
	return authTokenHandler
}