Skip to content

Commit 8574af4

Browse files
committed
feat(auth): implemant auth handler
1 parent cff9509 commit 8574af4

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

auth/jwt/http.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package jwt
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"io"
78
"net/http"
89

@@ -12,23 +13,27 @@ import (
1213
func (f *Factory) HTTPAuthHandler(proxy auth.Proxy) http.Handler {
1314
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
1415
credential, err := io.ReadAll(request.Body)
15-
if err != nil { //nolint:staticcheck,revive
16-
// ...
16+
if err != nil {
17+
http.Error(writer, fmt.Errorf("failed to read request body credential: %w", err).Error(), http.StatusBadRequest)
18+
return
1719
}
1820

1921
id, err := proxy.Authenticate(context.Background(), credential)
20-
if err != nil { //nolint:staticcheck,revive
21-
// ...
22+
if err != nil {
23+
http.Error(writer, fmt.Errorf("failed to authenticate: %w", err).Error(), http.StatusUnauthorized)
24+
return
2225
}
2326

2427
token, err := f.IssueJWT(id)
25-
if err != nil { //nolint:staticcheck,revive
26-
// ...
28+
if err != nil {
29+
http.Error(writer, fmt.Errorf("failed to issue JWT: %w", err).Error(), http.StatusInternalServerError)
30+
return
2731
}
2832

2933
writer.Header().Set("Content-Type", "application/json")
30-
if _, err := writer.Write([]byte(token)); err != nil { //nolint:staticcheck,revive
31-
// ...
34+
if _, err := writer.Write([]byte(token)); err != nil {
35+
http.Error(writer, fmt.Errorf("failed to write response: %w", err).Error(), http.StatusInternalServerError)
36+
return
3237
}
3338
})
3439
}
@@ -37,8 +42,7 @@ func (f *Factory) VerifyHTTPMiddleware(next auth.AuthenticatedHandler) http.Hand
3742
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
3843
id, err := f.VerifyHTTPRequest(request)
3944
if err != nil {
40-
writer.WriteHeader(http.StatusUnauthorized)
41-
writer.Write([]byte(err.Error())) //nolint:errcheck
45+
http.Error(writer, err.Error(), http.StatusUnauthorized)
4246
return
4347
}
4448

0 commit comments

Comments
 (0)