Skip to content

Commit 8b00680

Browse files
Merge pull request #29 from CS3219-AY2425S1/solomon/add-jwt-authentication
Solomon/add jwt authentication
2 parents e801419 + b7888d2 commit 8b00680

File tree

5 files changed

+155
-47
lines changed

5 files changed

+155
-47
lines changed

apps/question-service/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/go-chi/cors v1.2.1
2626
github.com/go-logr/logr v1.4.2 // indirect
2727
github.com/go-logr/stdr v1.2.2 // indirect
28-
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
28+
github.com/golang-jwt/jwt/v4 v4.5.0
2929
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3030
github.com/golang/protobuf v1.5.4 // indirect
3131
github.com/google/s2a-go v0.1.8 // indirect

apps/question-service/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"question-service/handlers"
11+
mymiddleware "question-service/middleware"
1112
"question-service/utils"
1213
"time"
1314

@@ -65,6 +66,7 @@ func main() {
6566
r := chi.NewRouter()
6667
r.Use(middleware.Logger)
6768
r.Use(middleware.Timeout(60 * time.Second))
69+
r.Use(mymiddleware.VerifyJWT)
6870

6971
r.Use(cors.Handler(cors.Options{
7072
// AllowedOrigins: []string{"http://localhost:3000"}, // Use this to allow specific origin hosts
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package middleware
2+
3+
import (
4+
"github.com/golang-jwt/jwt/v4"
5+
"net/http"
6+
"os"
7+
"strings"
8+
)
9+
10+
func VerifyJWT(next http.Handler) http.Handler {
11+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12+
// Get the token from the Authorization header
13+
authHeader := r.Header.Get("Authorization")
14+
if authHeader == "" {
15+
http.Error(w, "Authorization header is missing", http.StatusUnauthorized)
16+
return
17+
}
18+
19+
// Split the header to get the token
20+
tokenString := strings.Split(authHeader, " ")[1]
21+
22+
// Retrieve the JWT secret from environment variables
23+
jwtSecret := []byte(os.Getenv("JWT_SECRET"))
24+
if jwtSecret == nil {
25+
http.Error(w, "JWT secret is not set", http.StatusInternalServerError)
26+
return
27+
}
28+
29+
// Parse the token
30+
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
31+
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
32+
return nil, http.ErrNotSupported
33+
}
34+
return jwtSecret, nil
35+
})
36+
37+
if err != nil || !token.Valid {
38+
http.Error(w, "Invalid token", http.StatusUnauthorized)
39+
return
40+
}
41+
42+
// Optionally, you can extract claims from the token and attach them to the request context
43+
next.ServeHTTP(w, r)
44+
})
45+
}

0 commit comments

Comments
 (0)