-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_health.go
79 lines (64 loc) · 1.74 KB
/
http_health.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
package http
import (
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
)
func (app *App) Health(w http.ResponseWriter, req *http.Request) {
output := make(map[string]interface{})
defer logDebug(app.l, req, output)
cookie, err := req.Cookie(cookieJwtTokenName)
if err != nil {
if err == http.ErrNoCookie {
output["token"] = "no cookie"
} else {
output["token"] = "could not retrieve"
}
output["error"] = err.Error()
responseWithJSON(w, http.StatusUnauthorized, &output)
return
}
claims := &Claims{}
token, err := jwt.ParseWithClaims(cookie.Value, claims, func(t *jwt.Token) (interface{}, error) {
return app.jwtKey, nil
})
if err != nil {
output["token"] = "corrupted"
output["error"] = err.Error()
responseWithJSON(w, http.StatusUnauthorized, &output)
return
}
if err = token.Claims.Valid(); err != nil {
output["token"] = "corrupted claims"
output["error"] = err.Error()
responseWithJSON(w, http.StatusUnauthorized, &output)
return
}
if claims.StandardClaims.ExpiresAt <= time.Now().Unix() {
output["token"] = "expired"
output["error"] = "token expired"
responseWithJSON(w, http.StatusUnauthorized, &output)
return
}
output["token"] = "OK"
loggedUserID := claims.ID
ctx := req.Context()
user, err := app.userUsecases.GetUserByID(ctx, loggedUserID)
if err != nil {
output["DB"] = "error"
output["user"] = "could not retrieve"
output["error"] = err.Error()
responseWithJSON(w, http.StatusUnauthorized, &output)
return
}
output["DB"] = "OK"
if user == nil {
output["user"] = "user not found"
output["error"] = "user not found"
responseWithJSON(w, http.StatusUnauthorized, &output)
return
}
output["user"] = user
output["error"] = nil
responseWithJSON(w, http.StatusOK, &output)
}