Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var JwtAuthentication = func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { noAuthPath := []string{"/api/user/register"} noAuthPath = append(noAuthPath, "/api/user/login") requestPath := r.URL.Path for _, path := range noAuthPath { if path == requestPath { next.ServeHTTP(w, r) return } } tokenHeader := r.Header.Get("Authorization") if tokenHeader == "" { rsp := map[string]interface{}{"status": "invalid", "message": "Token is not Present ;"} w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(rsp) return } headerAuthorizationString := strings.Split(tokenHeader, " ") if len(headerAuthorizationString) != 2 { rsp := map[string]interface{}{"status": "invalid", "message": "Invalid/Format Auth Token ;"} w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(rsp) return } barier := headerAuthorizationString[0] if barier != "Bearer" { rsp := map[string]interface{}{"status": "invalid", "message": "Token is not Barier ;"} w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(rsp) return } tk := &models.Token{} tokenValue := headerAuthorizationString[1] token, err := jwt.ParseWithClaims(tokenValue, tk, func(token *jwt.Token) (interface{}, error) { return []byte(viper.GetString("api.secret_key")), nil }) if err != nil { rsp := map[string]interface{}{"status": "invalid", "message": "Malformed Authentication Token Please Login Again;"} w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(rsp) return } diff := tk.TimeExp.Sub(time.Now()) if diff < 0 { rsp := map[string]interface{}{"status": "invalid", "message": "Time Expired, please login again;"} w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(rsp) return } if !token.Valid { rsp := map[string]interface{}{"status": "invalid", "message": "Invalid/Format Auth Token ;"} w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(rsp) return } ctx := context.WithValue(r.Context(), "user", tk.UserID) r = r.WithContext(ctx) next.ServeHTTP(w, r) }) }
JwtAuthentication for JWT
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.