-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jwt): add jwt auth to protected endpoints (#131)
Co-authored-by: Leland Garofalo <[email protected]>
- Loading branch information
Showing
162 changed files
with
18,102 additions
and
746 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
FROM golang:1.22 | ||
|
||
FROM golang:alpine AS builder | ||
WORKDIR /app | ||
ADD . /app | ||
RUN go build -o /sheltertech-go ./cmd/sheltertech-go | ||
|
||
EXPOSE 3001 | ||
|
||
CMD [ "/app/tmp/sheltertech-go" ] | ||
FROM golang:alpine | ||
COPY --from=builder /sheltertech-go /sheltertech-go | ||
CMD ["/sheltertech-go"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// middleware/jwt.go | ||
|
||
package auth | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"time" | ||
|
||
jwtmiddleware "github.com/auth0/go-jwt-middleware/v2" | ||
"github.com/auth0/go-jwt-middleware/v2/jwks" | ||
"github.com/auth0/go-jwt-middleware/v2/validator" | ||
) | ||
|
||
// CustomClaims contains custom data we want from the token. | ||
type CustomClaims struct { | ||
Scope string `json:"scope"` | ||
} | ||
|
||
// Validate does nothing for this example, but we need | ||
// it to satisfy validator.CustomClaims interface. | ||
func (c CustomClaims) Validate(ctx context.Context) error { | ||
return nil | ||
} | ||
|
||
// EnsureValidToken is a middleware that will check the validity of our JWT. | ||
func EnsureValidToken() func(next http.Handler) http.Handler { | ||
issuerURL, err := url.Parse("https://" + os.Getenv("AUTH0_DOMAIN") + "/") | ||
if err != nil { | ||
log.Fatalf("Failed to parse the issuer url: %v", err) | ||
} | ||
|
||
provider := jwks.NewCachingProvider(issuerURL, 5*time.Minute) | ||
|
||
jwtValidator, err := validator.New( | ||
provider.KeyFunc, | ||
validator.RS256, | ||
issuerURL.String(), | ||
[]string{os.Getenv("AUTH0_AUDIENCE")}, | ||
validator.WithCustomClaims( | ||
func() validator.CustomClaims { | ||
return &CustomClaims{} | ||
}, | ||
), | ||
validator.WithAllowedClockSkew(time.Minute), | ||
) | ||
if err != nil { | ||
log.Fatalf("Failed to set up the jwt validator") | ||
} | ||
|
||
errorHandler := func(w http.ResponseWriter, r *http.Request, err error) { | ||
log.Printf("Encountered error while validating JWT: %v", err) | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusUnauthorized) | ||
w.Write([]byte(`{"message":"Failed to validate JWT."}`)) | ||
} | ||
|
||
middleware := jwtmiddleware.New( | ||
jwtValidator.ValidateToken, | ||
jwtmiddleware.WithErrorHandler(errorHandler), | ||
) | ||
|
||
return func(next http.Handler) http.Handler { | ||
return middleware.CheckJWT(next) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.