Browse Source

encrypt session key

Paul 5 years ago
parent
commit
852aeee92a
4 changed files with 32 additions and 10 deletions
  1. 3
    3
      middleware/auth/session.go
  2. 1
    1
      middleware/auth/session_debug.go
  3. 14
    6
      middleware/auth/session_redis.go
  4. 14
    0
      utils/encrypt.go

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

@@ -2,11 +2,11 @@ package auth
2 2
 
3 3
 type Session interface {
4 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 7
 	// IsExistsJwtToken judge whether token is invalid
8
-	IsExistsJwtToken(key string) bool
8
+	IsExistsJwtToken(token string) bool
9 9
 
10 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,7 +6,7 @@ func NewDebugSessionStore() *DebugSessionStore {
6 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 10
 	return nil
11 11
 }
12 12
 

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

@@ -3,6 +3,7 @@ package auth
3 3
 import (
4 4
 	"time"
5 5
 
6
+	"git.links123.net/links123.com/pkg/utils"
6 7
 	"github.com/go-redis/redis"
7 8
 )
8 9
 
@@ -22,14 +23,21 @@ func NewRedisSessionStore(address string, password string) *RedisSessionStore {
22 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

@@ -0,0 +1,14 @@
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
+}