Paul преди 6 години
родител
ревизия
6f388c3542
променени са 3 файла, в които са добавени 14 реда и са изтрити 29 реда
  1. 2
    4
      middleware/auth/auth.go
  2. 2
    4
      middleware/auth/optional_auth.go
  3. 10
    21
      middleware/auth/session_redis.go

+ 2
- 4
middleware/auth/auth.go Целия файл

@@ -19,10 +19,8 @@ func Auth(authKey string, session Session) gin.HandlerFunc {
19 19
 		var tokenFromCookie, tokenFromHeader string
20 20
 
21 21
 		tokenFromCookie, err := ctx.Cookie(ctxRequestCookieAuthorization)
22
-		if err != nil {
23
-			if err == http.ErrNoCookie {
24
-				tokenFromHeader = ctx.Request.Header.Get(ctxRequestHeaderAuthorization)
25
-			}
22
+		if err == http.ErrNoCookie {
23
+			tokenFromHeader = ctx.Request.Header.Get(ctxRequestHeaderAuthorization)
26 24
 		}
27 25
 
28 26
 		if tokenFromHeader == "" {

+ 2
- 4
middleware/auth/optional_auth.go Целия файл

@@ -12,10 +12,8 @@ func OptionalAuth(authKey string) gin.HandlerFunc {
12 12
 		var tokenFromCookie, tokenFromHeader string
13 13
 
14 14
 		tokenFromCookie, err := ctx.Cookie(ctxRequestCookieAuthorization)
15
-		if err != nil {
16
-			if err == http.ErrNoCookie {
17
-				tokenFromHeader = ctx.Request.Header.Get(ctxRequestHeaderAuthorization)
18
-			}
15
+		if err == http.ErrNoCookie {
16
+			tokenFromHeader = ctx.Request.Header.Get(ctxRequestHeaderAuthorization)
19 17
 		}
20 18
 
21 19
 		if tokenFromHeader == "" {

+ 10
- 21
middleware/auth/session_redis.go Целия файл

@@ -12,18 +12,16 @@ type RedisSessionStore struct {
12 12
 }
13 13
 
14 14
 func NewRedisSessionStore(address string, password string) *RedisSessionStore {
15
-	session := &RedisSessionStore{once:&sync.Once{}}
16
-	session.once.Do(func() {
17
-		session.client = redis.NewClient(&redis.Options{
18
-			Addr:     address,
19
-			Password: password,
20
-		})
21
-
22
-		if err := session.client.Ping().Err(); err != nil {
23
-			panic(err)
24
-		}
15
+	session := &RedisSessionStore{once: &sync.Once{}}
16
+	session.client = redis.NewClient(&redis.Options{
17
+		Addr:     address,
18
+		Password: password,
25 19
 	})
26 20
 
21
+	if err := session.client.Ping().Err(); err != nil {
22
+		panic(err)
23
+	}
24
+
27 25
 	return session
28 26
 }
29 27
 
@@ -32,18 +30,9 @@ func (rss *RedisSessionStore) StoreJwtToken(key string, value string, timeout ti
32 30
 }
33 31
 
34 32
 func (rss *RedisSessionStore) IsExistsJwtToken(key string) bool {
35
-	if v := rss.client.Exists(key).Val(); v != 1 {
36
-		return false
37
-	}
38
-
39
-	return true
33
+	return rss.client.Exists(key).Val() == 1
40 34
 }
41 35
 
42
-// DeleteJwtToken delete jwt token
43 36
 func (rss *RedisSessionStore) DeleteJwtToken(key string) bool {
44
-	if v := rss.client.Del(key).Val(); v != 1 {
45
-		return false
46
-	}
47
-
48
-	return true
37
+	return rss.client.Del(key).Val() == 1
49 38
 }