|
|
|
@ -67,9 +67,31 @@ func (s *AuthServer) GracefulStop() error { |
|
|
|
|
return s.db.Close() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Парсинг токена
|
|
|
|
|
func (s *AuthServer) parseToken(token string) (*jwt.Token, error) { |
|
|
|
|
return jwt.Parse(token, func(atoken *jwt.Token) (interface{}, error) { |
|
|
|
|
if _, ok := atoken.Method.(*jwt.SigningMethodHMAC); !ok { |
|
|
|
|
return nil, errors.New("there was an error in parsing") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return []byte(s.config.Auth.SecretKey), nil |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Проверка срока жизни токена
|
|
|
|
|
func (s *AuthServer) verifyExpirationToken(token *jwt.Token) error { |
|
|
|
|
now := float64(time.Now().Unix()) |
|
|
|
|
expiration := token.Claims.(jwt.MapClaims)["exp"].(float64) |
|
|
|
|
if expiration < now { |
|
|
|
|
return errors.New("the token has expired") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *AuthServer) requireAuth(next http.Handler) http.HandlerFunc { |
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
ctx := r.Context() |
|
|
|
|
// ctx := r.Context()
|
|
|
|
|
|
|
|
|
|
// Получим и проверим идентификатор сессии
|
|
|
|
|
authorizationHeader := r.Header.Get(HeaderAuthToken) |
|
|
|
@ -86,14 +108,7 @@ func (s *AuthServer) requireAuth(next http.Handler) http.HandlerFunc { |
|
|
|
|
|
|
|
|
|
return |
|
|
|
|
} else { |
|
|
|
|
token := authorizationHeaderAttributes[1] |
|
|
|
|
tokenParsed, err := jwt.Parse(token, func(atoken *jwt.Token) (interface{}, error) { |
|
|
|
|
if _, ok := atoken.Method.(*jwt.SigningMethodHMAC); !ok { |
|
|
|
|
return nil, errors.New("there was an error in parsing") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return []byte(s.config.Auth.SecretKey), nil |
|
|
|
|
}) |
|
|
|
|
tokenParsed, err := s.parseToken(authorizationHeaderAttributes[1]) |
|
|
|
|
if err != nil { |
|
|
|
|
s.logger.Error(err.Error()) |
|
|
|
|
|
|
|
|
@ -105,13 +120,8 @@ func (s *AuthServer) requireAuth(next http.Handler) http.HandlerFunc { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
now := float64(time.Now().Unix()) |
|
|
|
|
expiration := tokenParsed.Claims.(jwt.MapClaims)["exp"].(float64) |
|
|
|
|
|
|
|
|
|
if expiration < now { |
|
|
|
|
erf := errors.New("the token has expired") |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
erf := s.verifyExpirationToken(tokenParsed) |
|
|
|
|
if erf != nil { |
|
|
|
|
s.logger.Error(erf.Error()) |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
@ -122,22 +132,19 @@ func (s *AuthServer) requireAuth(next http.Handler) http.HandlerFunc { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
ctx = context.WithValue(ctx, ContextKey("email"), tokenParsed.Claims.(jwt.MapClaims)["email"]) |
|
|
|
|
ctx = context.WithValue(ctx, ContextKey(HeaderAuthToken), token) |
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx)) |
|
|
|
|
// next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
|
next.ServeHTTP(w, r) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func healthz(s *AuthServer) http.HandlerFunc { |
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
|
ctx := r.Context() |
|
|
|
|
// ctx := r.Context()
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
s.logger.Info(ctx.Value(ContextKey(HeaderAuthToken))) |
|
|
|
|
s.logger.Info(ctx.Value(ContextKey("email"))) |
|
|
|
|
// //
|
|
|
|
|
// s.logger.Info(ctx.Value(ContextKey(HeaderAuthToken)))
|
|
|
|
|
// s.logger.Info(ctx.Value(ContextKey("email")))
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
|
w.Write([]byte("ok")) |
|
|
|
|