77 "log/slog"
88 "net/http"
99 "strings"
10+
11+ "github.com/samims/hcaas/services/url/internal/model"
1012)
1113
1214func AuthMiddleware (authServiceURL string , logger * slog.Logger ) func (http.Handler ) http.Handler {
@@ -20,7 +22,13 @@ func AuthMiddleware(authServiceURL string, logger *slog.Logger) func(http.Handle
2022 }
2123 token := strings .TrimPrefix (authHeader , "Bearer " )
2224
23- req , err := http .NewRequest (http .MethodGet , authServiceURL + "auth/validate" , nil )
25+ validateURL := authServiceURL + "auth/validate"
26+ logger .Debug ("Calling auth service validation endpoint" ,
27+ "url" , validateURL ,
28+ "method" , r .Method ,
29+ "path" , r .URL .Path )
30+
31+ req , err := http .NewRequest (http .MethodGet , validateURL , nil )
2432 if err != nil {
2533 logger .Error ("Failed to create request to auth service" , "error" , err )
2634 http .Error (w , "Unauthorized" , http .StatusUnauthorized )
@@ -43,16 +51,53 @@ func AuthMiddleware(authServiceURL string, logger *slog.Logger) func(http.Handle
4351 return
4452 }
4553
46- var data struct {
54+ bodyBytes , err := io .ReadAll (resp .Body )
55+ if err != nil {
56+ logger .Error ("Failed to read auth response body" , "error" , err )
57+ http .Error (w , "Unauthorized" , http .StatusUnauthorized )
58+ return
59+ }
60+
61+ logger .Debug ("Auth service response" , "body" , string (bodyBytes ))
62+
63+ var authResponse struct {
4764 UserID string `json:"user_id"`
65+ Email string `json:"email"`
4866 }
49- if err := json .NewDecoder (resp .Body ).Decode (& data ); err != nil {
50- logger .Error ("Failed to decode auth service response" , "error" , err )
67+
68+ if err := json .Unmarshal (bodyBytes , & authResponse ); err != nil {
69+ logger .Error ("Failed to decode auth service response" ,
70+ "error" , err ,
71+ "response" , string (bodyBytes ))
5172 http .Error (w , "Unauthorized" , http .StatusUnauthorized )
5273 return
5374 }
5475
55- ctx := context .WithValue (r .Context (), "userID" , data .UserID )
76+ if authResponse .UserID == "" {
77+ logger .Error ("No user identifier found in auth response" ,
78+ slog .String ("response" , string (bodyBytes )))
79+ http .Error (w , "Unauthorized" , http .StatusUnauthorized )
80+ return
81+ }
82+
83+ if authResponse .Email == "" {
84+ logger .Error ("No email found in auth response" , slog .String ("response" , string (bodyBytes )))
85+ }
86+
87+ ctx := context .WithValue (r .Context (), model .ContextUserIDKey , authResponse .UserID )
88+ ctx = context .WithValue (ctx , model .ContextEmailKey , authResponse .Email )
89+ logger .Info ("User authenticated" ,
90+ "user_id" , authResponse .UserID ,
91+ "method" , r .Method ,
92+ "path" , r .URL .Path )
93+
94+ // Verify context value is set correctly
95+ if ctx .Value (model .ContextUserIDKey ) == nil {
96+ logger .Error ("Failed to set user_id in context" )
97+ http .Error (w , "Internal Server Error" , http .StatusInternalServerError )
98+ return
99+ }
100+
56101 next .ServeHTTP (w , r .WithContext (ctx ))
57102 })
58103 }
0 commit comments