-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserinfo.go
67 lines (58 loc) · 1.58 KB
/
userinfo.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"slices"
"github.com/kataras/jwt"
"go.mongodb.org/mongo-driver/bson"
)
func userinfo(w http.ResponseWriter, r *http.Request) {
/* Find matching user document */
uuid := r.URL.Query().Get("uuid")
token := r.URL.Query().Get("token")
log.Println("Received request: ", r.URL)
// if token is set, get uuid from token
if len(token) > 0 {
verifiedToken, err := jwt.Verify(jwt.HS256, []byte(jwtKey), []byte(token))
if err != nil {
log.Println(err)
uuid = "" // this will cause the user not to be found, returning empty data
} else {
var claims RulesCustomClaims
err := verifiedToken.Claims(&claims)
if err != nil {
log.Println(err)
uuid = "" // see comment above
} else {
uuid = claims.UUID
}
}
}
var user User
var userinfo UserInfo
err := collection.FindOne(context.TODO(), bson.D{{Key: "uuid", Value: uuid}}).Decode(&user)
/* Check if user was found */
if err != nil {
userinfo.Exists = false
userinfo.IsAdmin = false
userinfo.UUID = ""
} else {
userinfo.Exists = true
userinfo.IsAdmin = slices.Contains(user.Tags, "admin")
userinfo.UUID = user.Uuid
}
/* Convert userinfo to JSON */
decodedUserinfo, err := json.Marshal(userinfo)
if err != nil {
http.Error(w, "Could not decode user info", http.StatusInternalServerError)
log.Println("Could not decode user info: ", err)
} else {
/* Set result headers */
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(decodedUserinfo))
}
}