-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.go
48 lines (39 loc) · 1.15 KB
/
utility.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
package main
import (
"errors"
"fmt"
"log"
"os"
"unsafe"
"github.com/tidwall/gjson"
"github.com/valyala/fasthttp"
)
func B2S(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func verifyIdentity(token string) (string, error) {
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer func() {
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
}()
req.SetRequestURI(os.Getenv("CTFD_URL") + "/api/v1/users/me")
req.Header.SetMethod("GET")
req.Header.SetContentType("application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
if err := fasthttp.Do(req, resp); err != nil {
log.Fatalf("[call-api-CTFd] error while pinging CTFd: %v\n", err)
}
if resp.StatusCode() == 200 {
user := gjson.Get(B2S(resp.Body()), "data.name")
email := gjson.Get(B2S(resp.Body()), "data.email")
log.Printf("[%s] Token OK\n", token)
log.Printf("[%s] Got identity: %s - %s", token, user.Str, email.Str)
return user.Str, nil
} else {
log.Printf("[%s] Token NOT OK\n", token)
log.Printf("[%s] Received error: %s", token, string(resp.Body()))
return "", errors.New("failed to validate identity")
}
}