forked from pote/philote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess_key.go
55 lines (43 loc) · 913 Bytes
/
access_key.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
package main
import (
"errors"
"github.com/dgrijalva/jwt-go"
)
type AccessKey struct {
Read []string `json:"read"`
Write []string `json:"write"`
jwt.StandardClaims
}
func NewAccessKey(auth string) (*AccessKey, error) {
ak := AccessKey{}
verifyFunc := func(t *jwt.Token) (interface{}, error) {
return Config.jwtSecret, nil
}
token, err := jwt.ParseWithClaims(auth, &ak, verifyFunc)
if err != nil {
return &ak, err
}
if claims, ok := token.Claims.(*AccessKey); ok && token.Valid {
ak.Read = claims.Read
ak.Write = claims.Write
return &ak, nil
} else {
return &ak, errors.New("invalid token")
}
}
func (ak *AccessKey) CanWrite(channel string) bool {
for _, c := range ak.Write {
if c == channel {
return true
}
}
return false
}
func (ak *AccessKey) CanRead(channel string) bool {
for _, c := range ak.Read {
if c == channel {
return true
}
}
return false
}