-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
37 lines (31 loc) · 892 Bytes
/
jwt.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
package etc
import (
"net/http"
"os"
"time"
"github.com/nektro/go.etc/htp"
"github.com/nektro/go.etc/jwt"
. "github.com/nektro/go-util/alias"
)
// JWTSet sets a 'jwt' cookie on the provided ResponseWriter
func JWTSet(w http.ResponseWriter, sub string) {
n, _ := os.Hostname()
http.SetCookie(w, &http.Cookie{
Name: "jwt",
Value: jwt.Get("astheno."+AppID+"."+Version+"."+n, sub, JWTSecret, Epoch, time.Hour*24*30),
MaxAge: 0,
})
}
// JWTGetClaims reads the 'jwt' cookie and returns claims within it, if they are valid
func JWTGetClaims(c *htp.Controller, r *http.Request) jwt.MapClaims {
clms, err := jwt.VerifyRequest(r, JWTSecret)
c.Assert(err == nil, "403: "+F("%v", err))
return clms
}
// JWTDestroy tells the ResponseWriter to delete the 'jwt' cookie
func JWTDestroy(w http.ResponseWriter) {
http.SetCookie(w, &http.Cookie{
Name: "jwt",
MaxAge: -1,
})
}