forked from goharbor/harbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessionchecker.go
30 lines (26 loc) · 1.06 KB
/
sessionchecker.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
package filter
import (
"context"
beegoctx "github.com/astaxie/beego/context"
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/core/config"
"net/http"
)
// SessionReqKey is the key in the context of a request to mark the request carries session when reaching the backend
const SessionReqKey ContextValueKey = "harbor_with_session_req"
// SessionCheck is a filter to mark the requests that carries a session id, it has to be registered as
// "beego.BeforeStatic" because beego will modify the request after execution of these filters, all requests will
// appear to have a session id cookie.
func SessionCheck(ctx *beegoctx.Context) {
req := ctx.Request
_, err := req.Cookie(config.SessionCookieName)
if err == nil {
ctx.Request = req.WithContext(context.WithValue(req.Context(), SessionReqKey, true))
log.Debug("Mark the request as no-session")
}
}
// ReqCarriesSession verifies if the request carries session when
func ReqCarriesSession(req *http.Request) bool {
r, ok := req.Context().Value(SessionReqKey).(bool)
return ok && r
}