-
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.
Grabs the issuer after parsing the token and tries to verify it using the issuer's well known JWKS.
- Loading branch information
Showing
5 changed files
with
84 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/MicahParks/keyfunc" | ||
"github.com/gin-gonic/gin" | ||
"github.com/golang-jwt/jwt/v4" | ||
) | ||
|
||
func JWKS() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
auth := c.GetHeader("Authorization") | ||
encodedToken := auth[len("Bearer "):] | ||
p := jwt.Parser{} | ||
t, _, err := p.ParseUnverified(encodedToken, &jwt.RegisteredClaims{}) | ||
if err != nil { | ||
log.Printf("failed parsing token: %s", err) | ||
c.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
iss := t.Claims.(*jwt.RegisteredClaims).Issuer | ||
jwksURL, err := url.Parse(iss + "/.well-known/jwks.json") | ||
if err != nil { | ||
log.Printf("invalid issuer URL: %s", err) | ||
c.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = validateToken(encodedToken, jwksURL.String()) | ||
if err != nil { | ||
log.Printf("could not validate token: %s", err) | ||
c.AbortWithStatus(http.StatusUnauthorized) | ||
} | ||
} | ||
} | ||
|
||
func validateToken(encToken string, jwksURL string) error { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
options := keyfunc.Options{ | ||
Ctx: ctx, | ||
RefreshErrorHandler: func(err error) { | ||
log.Printf("refreshing JWKS failed: %s", err) | ||
}, | ||
RefreshInterval: time.Hour, | ||
RefreshRateLimit: time.Minute * 5, | ||
RefreshTimeout: time.Second * 10, | ||
RefreshUnknownKID: true, | ||
} | ||
|
||
jwks, err := keyfunc.Get(jwksURL, options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var token *jwt.Token | ||
if token, err = jwt.Parse(encToken, jwks.Keyfunc); err != nil { | ||
return err | ||
} | ||
|
||
if !token.Valid { | ||
return fmt.Errorf("invalid jwt token") | ||
} | ||
log.Println("The token is valid.") | ||
|
||
return nil | ||
} |
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