@@ -9,6 +9,34 @@ import (
99 "net/http"
1010)
1111
12+ type Claims struct {
13+ Email string `json:"email"`
14+ Username string `json:"preferred_username"`
15+ Name string `json:"name"`
16+ UserId string `json:"sub"`
17+ Groups []string `json:"groups"`
18+ Wiis []string `json:"wiis"`
19+ WWFC []string `json:"wwfc"`
20+ Dominos map [string ]bool `json:"dominos"`
21+ JustEat map [string ]bool `json:"just_eat"`
22+ }
23+
24+ func GetClaims (verifier * oidc.IDTokenVerifier , tokenString string ) (* Claims , int ) {
25+ // Verify the OpenID Connect idToken.
26+ ctx := context .Background ()
27+ idToken , err := verifier .Verify (ctx , tokenString )
28+ if err != nil {
29+ return nil , http .StatusFound
30+ }
31+
32+ var claims Claims
33+ if err = idToken .Claims (& claims ); err != nil {
34+ return nil , http .StatusTemporaryRedirect
35+ }
36+
37+ return & claims , http .StatusOK
38+ }
39+
1240func AuthenticationMiddleware (verifier * oidc.IDTokenVerifier ) gin.HandlerFunc {
1341 return func (c * gin.Context ) {
1442 tokenString , err := c .Cookie ("token" )
@@ -18,27 +46,9 @@ func AuthenticationMiddleware(verifier *oidc.IDTokenVerifier) gin.HandlerFunc {
1846 return
1947 }
2048
21- // Verify the OpenID Connect idToken.
22- ctx := context .Background ()
23- idToken , err := verifier .Verify (ctx , tokenString )
24- if err != nil {
25- c .Redirect (http .StatusFound , "/login" )
26- c .Abort ()
27- return
28- }
29-
30- // Parse custom claims if needed.
31- var claims struct {
32- UserId string `json:"sub"`
33- Email string `json:"email"`
34- Username string `json:"preferred_username"`
35- Wiis []string `json:"wiis"`
36- WWFC []string `json:"wwfc"`
37- Dominos map [string ]bool `json:"dominos"`
38- }
39-
40- if err = idToken .Claims (& claims ); err != nil {
41- c .Redirect (http .StatusTemporaryRedirect , "/login" )
49+ claims , status := GetClaims (verifier , tokenString )
50+ if status != http .StatusOK {
51+ c .Redirect (status , "/login" )
4252 c .Abort ()
4353 return
4454 }
@@ -63,6 +73,7 @@ func AuthenticationMiddleware(verifier *oidc.IDTokenVerifier) gin.HandlerFunc {
6373 c .Set ("wiis" , claims .Wiis )
6474 c .Set ("wwfc" , claims .WWFC )
6575 c .Set ("dominos" , claims .Dominos )
76+ c .Set ("just_eat" , claims .JustEat )
6677 c .Next ()
6778 }
6879}
@@ -77,29 +88,9 @@ func AuthenticationPOSTMiddleware(verifier *oidc.IDTokenVerifier) gin.HandlerFun
7788 return
7889 }
7990
80- // Verify the OpenID Connect idToken.
81- ctx := context .Background ()
82- idToken , err := verifier .Verify (ctx , tokenString )
83- if err != nil {
84- c .Status (http .StatusUnauthorized )
85- c .Abort ()
86- return
87- }
88-
89- // Parse custom claims if needed.
90- var claims struct {
91- Email string `json:"email"`
92- Username string `json:"preferred_username"`
93- Name string `json:"name"`
94- UserId string `json:"sub"`
95- Groups []string `json:"groups"`
96- Wiis []string `json:"wiis"`
97- WWFC []string `json:"wwfc"`
98- Dominos map [string ]bool `json:"dominos"`
99- }
100-
101- if err = idToken .Claims (& claims ); err != nil {
102- c .Status (http .StatusInternalServerError )
91+ claims , status := GetClaims (verifier , tokenString )
92+ if status != http .StatusOK {
93+ c .Redirect (status , "/login" )
10394 c .Abort ()
10495 return
10596 }
@@ -108,6 +99,7 @@ func AuthenticationPOSTMiddleware(verifier *oidc.IDTokenVerifier) gin.HandlerFun
10899 c .Set ("wiis" , claims .Wiis )
109100 c .Set ("wwfc" , claims .WWFC )
110101 c .Set ("dominos" , claims .Dominos )
102+ c .Set ("just_eat" , claims .JustEat )
111103 c .Next ()
112104 }
113105}
0 commit comments