-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.go
84 lines (67 loc) · 1.69 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package gocqrs
import (
"github.com/dgrijalva/jwt-go"
"github.com/diegogub/lib"
"time"
)
func AuthToken(t, secret string) (*SessionClaims, error) {
token, err := jwt.ParseWithClaims(t, &SessionClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if token == nil {
return nil, err
}
if claims, ok := token.Claims.(*SessionClaims); ok && token.Valid {
return claims, err
} else {
return nil, err
}
}
func BuildToken(u User) string {
claims := SessionClaims{
u.Username,
u.Role,
jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
ExpiresAt: time.Now().Add(runningApp.sduration).Unix(),
Issuer: runningApp.Name,
Id: lib.NewShortId(""),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Sign and get the complete encoded token as a string using the secret
tokenString, _ := token.SignedString([]byte(runningApp.Secret))
return tokenString
}
/*
import (
"github.com/diegogub/lib"
"time"
)
type Sessioner interface {
Save(s *Session) error
Valid(id string) (*Session, error)
}
type Session struct {
ID string `json:"id"`
Username string `json:"un"`
Role string `json:"role"`
ValidUntil time.Time `json:"ttl"`
Data map[string]interface{} `json:"session"`
}
func NewSession(u *User, validity string) *Session {
var s Session
s.ID = lib.NewLongId("S")
s.Username = u.Username
s.Role = u.Role
d, err := time.ParseDuration(validity)
if err != nil {
d = time.Duration(time.Minute * 10)
}
s.ValidUntil = time.Now().UTC().Add(d)
return &s
}
type ReadAuther interface {
AuthRead(e *Entity, username, role string) error
}
*/