|
5 | 5 | "fmt" |
6 | 6 | "net/http" |
7 | 7 | "net/http/httptest" |
| 8 | + "reflect" |
8 | 9 | "strings" |
9 | 10 | "testing" |
10 | 11 | "time" |
@@ -708,3 +709,156 @@ func TestWrapMCPEndpointWithValidToken(t *testing.T) { |
708 | 709 | t.Errorf("status = %d, want 200", rec.Code) |
709 | 710 | } |
710 | 711 | } |
| 712 | + |
| 713 | +// TestValidateTokenCached tests that only non-expired tokens are cached. |
| 714 | +func TestValidateTokenCached(t *testing.T) { |
| 715 | + t.Parallel() |
| 716 | + |
| 717 | + tests := []struct { |
| 718 | + name string |
| 719 | + expirationFromNow time.Duration |
| 720 | + tokenExpiryBuffer time.Duration |
| 721 | + want *User |
| 722 | + wantErr string |
| 723 | + }{ |
| 724 | + { |
| 725 | + name: "success", |
| 726 | + expirationFromNow: time.Minute, |
| 727 | + want: &User{Subject: "testuser"}, |
| 728 | + }, |
| 729 | + { |
| 730 | + name: "expired token", |
| 731 | + expirationFromNow: -time.Minute, |
| 732 | + wantErr: "authentication failed: failed to parse and validate token: token has invalid claims: token is expired", |
| 733 | + }, |
| 734 | + { |
| 735 | + name: "token expires in buffer", |
| 736 | + tokenExpiryBuffer: 5 * time.Minute, |
| 737 | + expirationFromNow: time.Minute, |
| 738 | + wantErr: "authentication failed: token expired or expiring too soon", |
| 739 | + }, |
| 740 | + } |
| 741 | + |
| 742 | + for _, tt := range tests { |
| 743 | + t.Run(tt.name, func(t *testing.T) { |
| 744 | + t.Parallel() |
| 745 | + |
| 746 | + cfg := &Config{ |
| 747 | + Mode: "native", |
| 748 | + Provider: "hmac", |
| 749 | + Audience: "api://test", |
| 750 | + JWTSecret: []byte("test-secret-key-must-be-32-bytes-long!"), |
| 751 | + ServerURL: "https://test-server.com", |
| 752 | + Issuer: "https://test.example.com", |
| 753 | + TokenExpiryBuffer: tt.tokenExpiryBuffer, |
| 754 | + } |
| 755 | + srv, err := NewServer(cfg) |
| 756 | + if err != nil { |
| 757 | + t.Fatalf("NewServer: %v", err) |
| 758 | + } |
| 759 | + |
| 760 | + // Create a valid HMAC token |
| 761 | + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ |
| 762 | + "sub": "testuser", |
| 763 | + "aud": "api://test", |
| 764 | + "iss": "https://test.example.com", |
| 765 | + "exp": time.Now().Add(tt.expirationFromNow).Unix(), |
| 766 | + }) |
| 767 | + tokenString, err := token.SignedString(cfg.JWTSecret) |
| 768 | + if err != nil { |
| 769 | + t.Fatalf("SignedString: %v", err) |
| 770 | + } |
| 771 | + |
| 772 | + user, err := srv.ValidateTokenCached(context.Background(), tokenString) |
| 773 | + if tt.wantErr != "" { |
| 774 | + if err == nil { |
| 775 | + t.Fatalf("expected error %q, got nil", tt.wantErr) |
| 776 | + } |
| 777 | + if err.Error() != tt.wantErr { |
| 778 | + t.Errorf("expected error %q, got %q", tt.wantErr, err.Error()) |
| 779 | + } |
| 780 | + return |
| 781 | + } |
| 782 | + if err != nil { |
| 783 | + t.Fatalf("unexpected error: %s", err) |
| 784 | + } |
| 785 | + if !reflect.DeepEqual(user, tt.want) { |
| 786 | + t.Errorf("expected user %v got user %v", tt.want, user) |
| 787 | + } |
| 788 | + }) |
| 789 | + } |
| 790 | +} |
| 791 | + |
| 792 | +// TestValidateTokenCached tests that the cache expires correctly. |
| 793 | +func TestValidateTokenCached_Expires(t *testing.T) { |
| 794 | + t.Parallel() |
| 795 | + |
| 796 | + tests := []struct { |
| 797 | + name string |
| 798 | + expirationFromNow time.Duration |
| 799 | + tokenExpiryBuffer time.Duration |
| 800 | + wantErr string |
| 801 | + }{ |
| 802 | + { |
| 803 | + name: "default expiry buffer", |
| 804 | + expirationFromNow: time.Second, |
| 805 | + wantErr: "authentication failed: failed to parse and validate token: token has invalid claims: token is expired", |
| 806 | + }, |
| 807 | + { |
| 808 | + name: "custom expiry buffer", |
| 809 | + expirationFromNow: 5 * time.Second, |
| 810 | + tokenExpiryBuffer: 4 * time.Second, |
| 811 | + wantErr: "authentication failed: token expired or expiring too soon", |
| 812 | + }, |
| 813 | + } |
| 814 | + |
| 815 | + for _, tt := range tests { |
| 816 | + t.Run(tt.name, func(t *testing.T) { |
| 817 | + t.Parallel() |
| 818 | + |
| 819 | + cfg := &Config{ |
| 820 | + Mode: "native", |
| 821 | + Provider: "hmac", |
| 822 | + Audience: "api://test", |
| 823 | + JWTSecret: []byte("test-secret-key-must-be-32-bytes-long!"), |
| 824 | + ServerURL: "https://test-server.com", |
| 825 | + Issuer: "https://test.example.com", |
| 826 | + TokenExpiryBuffer: tt.tokenExpiryBuffer, |
| 827 | + } |
| 828 | + srv, err := NewServer(cfg) |
| 829 | + if err != nil { |
| 830 | + t.Fatalf("NewServer: %v", err) |
| 831 | + } |
| 832 | + |
| 833 | + // Create a valid HMAC token |
| 834 | + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ |
| 835 | + "sub": "testuser", |
| 836 | + "aud": "api://test", |
| 837 | + "iss": "https://test.example.com", |
| 838 | + "exp": time.Now().Add(tt.expirationFromNow).Unix(), |
| 839 | + }) |
| 840 | + tokenString, err := token.SignedString(cfg.JWTSecret) |
| 841 | + if err != nil { |
| 842 | + t.Fatalf("SignedString: %v", err) |
| 843 | + } |
| 844 | + |
| 845 | + // Token is successfully verified and cached |
| 846 | + _, err = srv.ValidateTokenCached(context.Background(), tokenString) |
| 847 | + if err != nil { |
| 848 | + t.Fatalf("unexpected error %s", err) |
| 849 | + } |
| 850 | + |
| 851 | + // Wait twice as long as the token should take to expire. |
| 852 | + time.Sleep(2 * (tt.expirationFromNow - tt.tokenExpiryBuffer)) |
| 853 | + |
| 854 | + _, err = srv.ValidateTokenCached(context.Background(), tokenString) |
| 855 | + if err == nil { |
| 856 | + t.Fatal("expected error, got nil") |
| 857 | + } |
| 858 | + |
| 859 | + if err.Error() != tt.wantErr { |
| 860 | + t.Errorf("expected error %q, got %q", tt.wantErr, err.Error()) |
| 861 | + } |
| 862 | + }) |
| 863 | + } |
| 864 | +} |
0 commit comments