-
Notifications
You must be signed in to change notification settings - Fork 10
/
auth.go
66 lines (53 loc) · 1.24 KB
/
auth.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
package auth
import (
"errors"
"net/http"
"time"
"github.com/Zenika/MARCEL/auth-backend/conf"
jwt "github.com/dgrijalva/jwt-go"
)
var (
key = []byte("ThisIsTheSecret")
config *conf.Config
)
func SetConfig(c *conf.Config) {
config = c
}
func getVerifiedClaims(tokenString string, sampleClaims jwt.Claims) (jwt.Claims, error) {
token, err := jwt.ParseWithClaims(
tokenString,
sampleClaims,
func(token *jwt.Token) (interface{}, error) { return key, nil },
)
if err != nil || !token.Valid {
return nil, errors.New("Invlaid token")
}
return token.Claims, nil
}
func createTokenCookie(claims jwt.Claims, name string, path string, expiration time.Time) (*http.Cookie, error) {
token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(key)
if err != nil {
return nil, err
}
cookie := &http.Cookie{
Name: name,
Value: token,
Expires: expiration,
Domain: config.Domain,
Secure: config.SecuredCookies,
HttpOnly: true,
Path: path,
}
return cookie, nil
}
func deleteCookie(name, path string) *http.Cookie {
cookie := &http.Cookie{
Name: name,
Expires: time.Now(),
Domain: config.Domain,
Path: path,
Secure: config.SecuredCookies,
HttpOnly: true,
}
return cookie
}