-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (108 loc) · 2.56 KB
/
main.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/atecce/auth/alert"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"github.com/kr/pretty"
)
const (
cat = "kitjs/AuthKey_"
alg = "ES256"
iss = "Y82E2K77P5"
)
var (
etc = os.Getenv("ETC")
kids = map[string]string{
"music": "CUG44HA5T5",
"map": "YKVC29UG5H",
}
keys = make(map[string]*ecdsa.PrivateKey)
)
// loads keys
func init() {
for svc, kid := range kids {
path := etc + "/" + svc + cat + kid + ".p8"
fmt.Println("loading key from path: " + path)
b, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal("reading p8 file:", err)
}
block, _ := pem.Decode(b)
var key *ecdsa.PrivateKey
tmp, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
log.Fatal("parsing block:", err)
}
key = tmp.(*ecdsa.PrivateKey)
keys[svc] = key
}
}
// TODO create different middleware for different endpoint
func middleware(w http.ResponseWriter, r *http.Request) bool {
fmt.Println(r.Method + " " + r.URL.Path + " " + r.RemoteAddr + " " + r.Host)
w.Header().Set("Access-Control-Allow-Origin", "*")
// TODO will double count hits (but allow music route)
if r.Method != http.MethodGet || r.Host != "auth.atec.pub" || (r.URL.Path != "/" && r.URL.Path != "/music" && r.URL.Path != "/map") {
w.WriteHeader(http.StatusBadRequest)
return false
}
return true
}
func hit(w http.ResponseWriter, r *http.Request) {
if ok := middleware(w, r); !ok {
return
}
err := alert.Send(r)
if err != nil {
fmt.Println("alerting:", err)
}
w.WriteHeader(http.StatusOK)
}
func sign(w http.ResponseWriter, r *http.Request) {
if ok := middleware(w, r); !ok {
return
}
svc := mux.Vars(r)["svc"]
if svc != "music" && svc != "map" {
w.WriteHeader(http.StatusNotFound)
return
}
pretty.Println("[DEBUG] signing key for svc:", svc)
t := time.Now().Unix()
jwtToken := &jwt.Token{
Header: map[string]interface{}{
"alg": alg,
"kid": kids[svc],
},
Claims: jwt.MapClaims{
"iss": iss,
"iat": t,
"exp": t + 3000,
},
Method: jwt.SigningMethodES256,
}
bearer, err := jwtToken.SignedString(keys[svc])
if err != nil {
fmt.Println("signing bearer:", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(bearer))
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", hit)
r.HandleFunc("/{svc}", sign)
authEtc := etc + "auth"
fmt.Println(http.ListenAndServeTLS(":443", authEtc+"/server.crt", authEtc+"/server.key", r))
}