Browse Source

encrypt session key

Paul 6 years ago
parent
commit
852aeee92a

+ 3
- 3
middleware/auth/session.go View File

2
 
2
 
3
 type Session interface {
3
 type Session interface {
4
 	// StoreJwtToken store jwt token is redis, make it expired for feature
4
 	// StoreJwtToken store jwt token is redis, make it expired for feature
5
-	StoreJwtToken(key string, value string, timeout int64) error
5
+	StoreJwtToken(token string, uid int64, timeout int64) error
6
 
6
 
7
 	// IsExistsJwtToken judge whether token is invalid
7
 	// IsExistsJwtToken judge whether token is invalid
8
-	IsExistsJwtToken(key string) bool
8
+	IsExistsJwtToken(token string) bool
9
 
9
 
10
 	// DeleteJwtToken delete jwt token
10
 	// DeleteJwtToken delete jwt token
11
-	DeleteJwtToken(key string) bool
11
+	DeleteJwtToken(token string) bool
12
 }
12
 }

+ 1
- 1
middleware/auth/session_debug.go View File

6
 	return &DebugSessionStore{}
6
 	return &DebugSessionStore{}
7
 }
7
 }
8
 
8
 
9
-func (*DebugSessionStore) StoreJwtToken(key string, value string, timeout int64) error {
9
+func (*DebugSessionStore) StoreJwtToken(key string, uid int64, timeout int64) error {
10
 	return nil
10
 	return nil
11
 }
11
 }
12
 
12
 

+ 14
- 6
middleware/auth/session_redis.go View File

3
 import (
3
 import (
4
 	"time"
4
 	"time"
5
 
5
 
6
+	"git.links123.net/links123.com/pkg/utils"
6
 	"github.com/go-redis/redis"
7
 	"github.com/go-redis/redis"
7
 )
8
 )
8
 
9
 
22
 	return session
23
 	return session
23
 }
24
 }
24
 
25
 
25
-func (rss *RedisSessionStore) StoreJwtToken(key string, value string, timeout int64) error {
26
-	return rss.client.Set(key, value, time.Duration(timeout) * time.Second).Err()
26
+func (rss *RedisSessionStore) StoreJwtToken(token string, uid int64, timeout int64) error {
27
+	k := utils.Md5(token)
28
+	v := map[string]int64{"user_id": uid, "created": time.Now().Unix()}
29
+
30
+	return rss.client.Set(k, v, time.Duration(timeout)*time.Second).Err()
27
 }
31
 }
28
 
32
 
29
-func (rss *RedisSessionStore) IsExistsJwtToken(key string) bool {
30
-	return rss.client.Exists(key).Val() == 1
33
+func (rss *RedisSessionStore) IsExistsJwtToken(token string) bool {
34
+	k := utils.Md5(token)
35
+
36
+	return rss.client.Exists(k).Val() == 1
31
 }
37
 }
32
 
38
 
33
-func (rss *RedisSessionStore) DeleteJwtToken(key string) bool {
34
-	return rss.client.Del(key).Val() == 1
39
+func (rss *RedisSessionStore) DeleteJwtToken(token string) bool {
40
+	k := utils.Md5(token)
41
+
42
+	return rss.client.Del(k).Val() == 1
35
 }
43
 }

+ 14
- 0
utils/encrypt.go View File

1
+package utils
2
+
3
+import (
4
+	"crypto/md5"
5
+	"encoding/hex"
6
+)
7
+
8
+func Md5(src string) string {
9
+	h := md5.New()
10
+	h.Write([]byte(src))
11
+	d := h.Sum(nil)
12
+
13
+	return string(hex.EncodeToString(d))
14
+}