-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
49 lines (42 loc) · 1.23 KB
/
validate.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/kataras/jwt"
)
func validate(w http.ResponseWriter, r *http.Request) {
/* Get token */
token := r.URL.Query().Get("token")
result := ValidationResult{"", false}
log.Println("Received request: ", r.URL)
/* Check if token was provided, otherwise return an error message */
if len(token) > 0 {
/* Check if provided token is valid, otherwise return an error message */
_, err := jwt.Verify(jwt.HS256, []byte(jwtKey), []byte(token))
if err != nil {
errorMessage := "Failed to validate handed over JWT token"
log.Println(errorMessage)
result.Error = errorMessage
} else {
result.Valid = true
}
} else {
errorMessage := "No token provided"
http.Error(w, errorMessage, http.StatusBadRequest)
result.Error = errorMessage
}
/* Decode and send result */
decodedResult, err := json.Marshal(result)
if err != nil {
http.Error(w, "Could not decode result", http.StatusInternalServerError)
log.Println("Could not decode result: ", err)
} else {
/* Set result headers */
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
/* Print result */
fmt.Fprint(w, string(decodedResult))
}
}