-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathutil.go
48 lines (42 loc) · 943 Bytes
/
util.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 sessions
import (
"net/http"
"github.com/keratin/authn-server/app"
"github.com/keratin/authn-server/app/models"
"github.com/keratin/authn-server/app/tokens/sessions"
)
func Get(r *http.Request) *sessions.Claims {
fn, ok := r.Context().Value(sessionKey(0)).(func() *sessions.Claims)
if ok {
return fn()
}
return nil
}
func GetAccountID(r *http.Request) int {
fn, ok := r.Context().Value(accountIDKey(0)).(func() int)
if ok {
return fn()
}
return 0
}
func Set(cfg *app.Config, w http.ResponseWriter, val string) {
cookie := &http.Cookie{
Name: cfg.SessionCookieName,
Value: val,
Path: cfg.MountedPath,
Secure: cfg.ForceSSL,
HttpOnly: true,
}
if val == "" {
cookie.MaxAge = -1
}
http.SetCookie(w, cookie)
}
func GetRefreshToken(r *http.Request) *models.RefreshToken {
claims := Get(r)
if claims != nil {
token := models.RefreshToken(claims.Subject)
return &token
}
return nil
}