Skip to content

Commit 7222418

Browse files
committed
improvement: JWT
- upgraded github.com/golang-jwt/jwt from v4.5.0 to v5.2.1 - store key/value pair for all RegisteredClaims in the context
1 parent 896edae commit 7222418

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"strings"
1515
"time"
1616

17-
"github.com/golang-jwt/jwt/v4"
17+
"github.com/golang-jwt/jwt/v5"
1818
"github.com/joho/godotenv"
1919
"golang.org/x/crypto/sha3"
2020

example/controller/playgroundBasicAuth.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller
22

33
import (
4+
"fmt"
45
"net/http"
56

67
"github.com/gin-gonic/gin"
@@ -10,5 +11,10 @@ import (
1011

1112
// AccessResource - can be accessed by basic auth
1213
func AccessResource(c *gin.Context) {
14+
// print all claims in JWT
15+
for k, v := range c.Keys {
16+
fmt.Println("key:", k, "|", "value:", v)
17+
}
18+
1319
grenderer.Render(c, gin.H{"message": "access granted!"}, http.StatusOK)
1420
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/flosch/pongo2/v6 v6.0.0
77
github.com/gin-gonic/gin v1.10.0
88
github.com/go-sql-driver/mysql v1.8.1
9-
github.com/golang-jwt/jwt/v4 v4.5.0
9+
github.com/golang-jwt/jwt/v5 v5.2.1
1010
github.com/google/uuid v1.6.0
1111
github.com/joho/godotenv v1.5.1
1212
github.com/mediocregopher/radix/v4 v4.1.4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv
3838
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
3939
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
4040
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
41-
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
42-
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
41+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
42+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
4343
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
4444
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
4545
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=

lib/middleware/jwt.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/gin-gonic/gin"
17-
"github.com/golang-jwt/jwt/v4"
17+
"github.com/golang-jwt/jwt/v5"
1818
"github.com/google/uuid"
1919
)
2020

@@ -119,6 +119,17 @@ func JWT() gin.HandlerFunc {
119119
c.Set("expAccess", claims.ExpiresAt.Unix()) // in UTC
120120
c.Set("iatAccess", claims.IssuedAt.Unix()) // in UTC
121121
c.Set("jtiAccess", claims.ID)
122+
123+
// set values from RegisteredClaims
124+
//
125+
// token issuer
126+
c.Set("iss", claims.Issuer)
127+
//
128+
// token subject
129+
c.Set("sub", claims.Subject)
130+
//
131+
// token audience
132+
c.Set("aud", claims.Audience)
122133
}
123134

124135
c.Next()
@@ -189,6 +200,17 @@ func RefreshJWT() gin.HandlerFunc {
189200
c.Set("expRefresh", claims.ExpiresAt.Unix()) // in UTC
190201
c.Set("iatRefresh", claims.IssuedAt.Unix()) // in UTC
191202
c.Set("jtiRefresh", claims.ID)
203+
204+
// set values from RegisteredClaims
205+
//
206+
// token issuer
207+
c.Set("iss", claims.Issuer)
208+
//
209+
// token subject
210+
c.Set("sub", claims.Subject)
211+
//
212+
// token audience
213+
c.Set("aud", claims.Audience)
192214
}
193215

194216
c.Next()

lib/middleware/jwt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111

1212
"github.com/gin-gonic/gin"
13-
"github.com/golang-jwt/jwt/v4"
13+
"github.com/golang-jwt/jwt/v5"
1414
"github.com/pilinux/gorest/lib/middleware"
1515
)
1616

0 commit comments

Comments
 (0)