-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainDevHelper.go
45 lines (36 loc) · 1.1 KB
/
mainDevHelper.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
package main
import (
"net/http"
"strconv"
jwt "github.com/dgrijalva/jwt-go"
)
// DevHelperGetToken sends a valid JWT token in dev mode
// /api/dev/token
func DevHelperGetToken(w http.ResponseWriter, req *http.Request) {
viewID := req.URL.Query().Get("viewId")
agentID := req.URL.Query().Get("agId")
caps := map[string]interface{}{
"produce": true,
"consume": true,
"createPOI": true,
"createAirBeacon": true,
"sendEvents": true,
"maxView": [2]float64{360, 180},
"maxAirBeacon": [2]float64{360, 180},
"http": true,
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"viewId": viewID,
"agentId": agentID,
"caps": caps,
})
tokenString, err := token.SignedString([]byte(SecretKey))
if err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}
w.Header().Set("Content-type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(len(tokenString)))
w.Write([]byte(tokenString))
log.Debugf("New JWT development token (agent: %s, view: %s): %s", agentID, viewID, tokenString)
}