|
16 | 16 | package jwt |
17 | 17 |
|
18 | 18 | import ( |
| 19 | + "encoding/json" |
19 | 20 | "fmt" |
20 | 21 | "strings" |
21 | 22 | "testing" |
@@ -782,6 +783,70 @@ func TestAccountMappingWith30And0Weights(t *testing.T) { |
782 | 783 | } |
783 | 784 | } |
784 | 785 |
|
| 786 | +func TestAccountMappingBackwardCompatibility(t *testing.T) { |
| 787 | + // test that old JWTs without weight field get weight 100 on unmarshal |
| 788 | + oldJWT := `{"subject":"hello", "to": "hi"}` |
| 789 | + |
| 790 | + var m WeightedMapping |
| 791 | + err := json.Unmarshal([]byte(oldJWT), &m) |
| 792 | + if err != nil { |
| 793 | + t.Fatal(err) |
| 794 | + } |
| 795 | + |
| 796 | + // verify weight defaults to 100 for old JWTs without weight field |
| 797 | + if m.Weight != 100 { |
| 798 | + t.Fatalf("Expected weight 100 for old JWT without weight field, got: %d", m.Weight) |
| 799 | + } |
| 800 | + |
| 801 | + // test that new JWTs with weight 0 keep weight 0 |
| 802 | + newJWT := `{"subject":"hello", "to": "hi", "weight": 0}` |
| 803 | + err = json.Unmarshal([]byte(newJWT), &m) |
| 804 | + if err != nil { |
| 805 | + t.Fatal(err) |
| 806 | + } |
| 807 | + |
| 808 | + if m.Weight != 0 { |
| 809 | + t.Fatalf("Expected weight 0 for new JWT with weight:0, got: %d", m.Weight) |
| 810 | + } |
| 811 | +} |
| 812 | + |
| 813 | +func TestAccountMappingOldJWT(t *testing.T) { |
| 814 | + // old JWT with mapping that doesn't have weight field |
| 815 | + oldJWT := `eyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJqdGkiOiJCTFE3UllWSEs3QVZCN0gzRVgzRUpWRlNORTRPUEVUTkg1VlNZQkpVVTdUVTVCWkVDNjVRIiwiaWF0IjoxNzYzMjE2MDI5LCJpc3MiOiJPREo3Q0UyVklCWTdHM0dYVVZFTVFYR1ZRNlJSRlRPWFdaNEpOVzVBMklZTlNHNVk2VDRWNFNBUyIsIm5hbWUiOiJNIiwic3ViIjoiQUFYUVE3TFdQWUZGT1g1S0ZBNU02VjY3VVBOTzJKTUFKVTdKUVJRQkRDVjdRSUhGNllIUDU3Q0siLCJuYXRzIjp7ImxpbWl0cyI6eyJzdWJzIjotMSwiZGF0YSI6LTEsInBheWxvYWQiOi0xLCJpbXBvcnRzIjotMSwiZXhwb3J0cyI6LTEsIndpbGRjYXJkcyI6dHJ1ZSwiY29ubiI6LTEsImxlYWYiOi0xfSwiZGVmYXVsdF9wZXJtaXNzaW9ucyI6eyJwdWIiOnt9LCJzdWIiOnt9fSwibWFwcGluZ3MiOnsiYSI6W3sic3ViamVjdCI6ImIifV19LCJhdXRob3JpemF0aW9uIjp7fSwidHlwZSI6ImFjY291bnQiLCJ2ZXJzaW9uIjoyfX0.qFxSSQKqHxpl2qS21x1Yj8zqDufGLIp9Gncb-YBf3P-CYxB31Dtp5swSYOmsA8zEGYMdnynY7z_73LweHqedAg` |
| 816 | + |
| 817 | + account, err := DecodeAccountClaims(oldJWT) |
| 818 | + if err != nil { |
| 819 | + t.Fatal(err) |
| 820 | + } |
| 821 | + |
| 822 | + // verify mapping was loaded |
| 823 | + if len(account.Mappings) != 1 { |
| 824 | + t.Fatalf("Expected 1 mapping, got %d", len(account.Mappings)) |
| 825 | + } |
| 826 | + |
| 827 | + // verify mapping "a" -> "b" exists and has weight 100 (default for old JWTs) |
| 828 | + mappingsA, ok := account.Mappings["a"] |
| 829 | + if !ok { |
| 830 | + t.Fatal("Expected mapping 'a' to exist") |
| 831 | + } |
| 832 | + if len(mappingsA) != 1 { |
| 833 | + t.Fatalf("Expected 1 mapping for 'a', got %d", len(mappingsA)) |
| 834 | + } |
| 835 | + if mappingsA[0].Subject != "b" { |
| 836 | + t.Fatalf("Expected subject 'b', got %s", mappingsA[0].Subject) |
| 837 | + } |
| 838 | + if mappingsA[0].Weight != 100 { |
| 839 | + t.Fatalf("Expected weight 100 for old JWT mapping without weight field, got %d", mappingsA[0].Weight) |
| 840 | + } |
| 841 | + |
| 842 | + // validate should pass |
| 843 | + vr := &ValidationResults{} |
| 844 | + account.Validate(vr) |
| 845 | + if !vr.IsEmpty() { |
| 846 | + t.Fatalf("Expected no validation errors, got: %v", vr.Issues) |
| 847 | + } |
| 848 | +} |
| 849 | + |
785 | 850 | func TestAccountExternalAuthorization(t *testing.T) { |
786 | 851 | akp := createAccountNKey(t) |
787 | 852 | apk := publicKey(akp, t) |
|
0 commit comments