-
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(authentication): Could finally protect routes
Now routes are protected, added authentication functionality, made some changes in config. BREAKING CHANGE: Can now protect routes with Auth0 Referred issue: #30
- Loading branch information
1 parent
f353c0f
commit b278063
Showing
9 changed files
with
127 additions
and
30 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
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
package constants | ||
|
||
import "time" | ||
|
||
const EmptyString = "" | ||
const ProviderDuration = 5 * time.Minute |
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,72 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"rpl-service/constants" | ||
"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(_ 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, constants.ProviderDuration) | ||
|
||
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, _ *http.Request, err error) { | ||
log.Printf("Encountered error while validating JWT: %v", err) | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusUnauthorized) | ||
_, writingError := w.Write([]byte(`{"message":"Failed to validate JWT."}`)) | ||
if writingError != nil { | ||
return | ||
} | ||
} | ||
|
||
middleware := jwtmiddleware.New( | ||
jwtValidator.ValidateToken, | ||
jwtmiddleware.WithErrorHandler(errorHandler), | ||
) | ||
|
||
return func(next http.Handler) http.Handler { | ||
return middleware.CheckJWT(next) | ||
} | ||
} |
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