Skip to content

Commit

Permalink
Merge pull request MicrosoftStudentChapter#8 from PreetinderSingh13/Auth
Browse files Browse the repository at this point in the history
Auth Updated
  • Loading branch information
PreetinderSinghBadesha authored Jun 19, 2024
2 parents c0e3085 + b7c731a commit c793dba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
5 changes: 2 additions & 3 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ func main() {
fmt.Println("Redis [PING]: ", res)

r := mux.NewRouter()

r.HandleFunc("/links/all", router.GetAllLinks).Methods(http.MethodOptions, http.MethodGet)
r.HandleFunc("/generate/jwt", auth.GenerateJWT).Methods(http.MethodOptions, http.MethodGet)
r.HandleFunc("/validate/jwt", auth.ValidateJWT).Methods(http.MethodOptions, http.MethodGet)
r.HandleFunc("/generate-token", auth.GenerateJWT).Methods(http.MethodOptions, http.MethodGet)
r.Handle("/login", auth.TokenRequired(http.HandlerFunc(auth.ProtectedRoute))).Methods(http.MethodOptions, http.MethodGet)
r.HandleFunc("/register", auth.Register).Methods(http.MethodOptions, http.MethodPost)
r.HandleFunc("/show/users", auth.ShowUsers).Methods(http.MethodOptions, http.MethodGet)
r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
Expand Down
47 changes: 27 additions & 20 deletions backend/pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,47 @@ func GenerateJWT(w http.ResponseWriter, r *http.Request) {
}

w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"token": "` + tokenString + `"}`))
// return tokenString
json.NewEncoder(w).Encode(map[string]string{"token": tokenString})
}

func ValidateJWT(w http.ResponseWriter, r *http.Request) {
tokenString := r.URL.Query().Get("token")
func ValidateJWT(tokenString string) (string, error) {
claims := &Claims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return jwtKey, nil
})

if err != nil {
if err == jwt.ErrSignatureInvalid {
http.Error(w, "Invalid token signature", http.StatusUnauthorized)
return
return "", http.ErrBodyNotAllowed
}
http.Error(w, "Invalid token", http.StatusBadRequest)
return
return "", err
}

if !token.Valid {
http.Error(w, "Invalid token", http.StatusUnauthorized)
return
return "", http.ErrBodyNotAllowed
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"username": claims.Username,
"id": claims.Id,
"issuer": claims.Issuer,
"expiresAt": claims.ExpiresAt,
"issuedAt": claims.IssuedAt,
return claims.Username, nil
}

func TokenRequired(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tokenString := r.Header.Get("Authorization")
if tokenString == "" {
http.Error(w, "Token is missing", http.StatusForbidden)
return
}

username, err := ValidateJWT(tokenString)
if err != nil {
http.Error(w, err.Error(), http.StatusForbidden)
return
}

r.Header.Set("username", username)
next.ServeHTTP(w, r)
})
}

// return claims, nil
func ProtectedRoute(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "http://localhost:4000/admin", http.StatusSeeOther)
}

func Register(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit c793dba

Please sign in to comment.