(t *testing.T)
| 747 | } |
| 748 | |
| 749 | func TestClaimsDuringAuthorization(t *testing.T) { |
| 750 | // the middleware to test |
| 751 | authMiddleware, _ := New(&GinJWTMiddleware{ |
| 752 | Realm: "test zone", |
| 753 | Key: key, |
| 754 | Timeout: time.Hour, |
| 755 | MaxRefresh: time.Hour * 24, |
| 756 | PayloadFunc: func(data any) jwt.MapClaims { |
| 757 | if v, ok := data.(jwt.MapClaims); ok { |
| 758 | return v |
| 759 | } |
| 760 | |
| 761 | if reflect.TypeOf(data).String() != "string" { |
| 762 | return jwt.MapClaims{} |
| 763 | } |
| 764 | |
| 765 | var testkey string |
| 766 | switch data.(string) { |
| 767 | case testAdmin: |
| 768 | testkey = "1234" |
| 769 | case testUser: |
| 770 | testkey = "5678" |
| 771 | case "Guest": |
| 772 | testkey = "" |
| 773 | } |
| 774 | // Set custom claim, to be checked in Authorizer method |
| 775 | now := time.Now() |
| 776 | return jwt.MapClaims{ |
| 777 | "identity": data.(string), |
| 778 | "testkey": testkey, |
| 779 | "exp": now.Add(time.Hour).Unix(), |
| 780 | "iat": now.Unix(), |
| 781 | "nbf": now.Unix(), |
| 782 | } |
| 783 | }, |
| 784 | Authenticator: func(c *gin.Context) (any, error) { |
| 785 | var loginVals Login |
| 786 | |
| 787 | if err := c.BindJSON(&loginVals); err != nil { |
| 788 | return "", ErrMissingLoginValues |
| 789 | } |
| 790 | |
| 791 | userID := loginVals.Username |
| 792 | password := loginVals.Password |
| 793 | |
| 794 | if userID == testAdmin && password == testPassword { |
| 795 | return userID, nil |
| 796 | } |
| 797 | |
| 798 | if userID == testUser && password == testUserPasswd { |
| 799 | return userID, nil |
| 800 | } |
| 801 | |
| 802 | return "Guest", ErrFailedAuthentication |
| 803 | }, |
| 804 | Authorizer: func(c *gin.Context, user any) bool { |
| 805 | jwtClaims := ExtractClaims(c) |
| 806 |
nothing calls this directly
no test coverage detected
searching dependent graphs…