@@ -424,6 +424,7 @@ public async Task TestRealOAuthTokenClaims()
424
424
425
425
[ Theory ]
426
426
[ InlineData ( "mt_1234567890abcdef" , TokenType . MachineToken ) ]
427
+ [ InlineData ( "m2m_1234567890abcdef" , TokenType . MachineTokenV2 ) ]
427
428
[ InlineData ( "oat_1234567890abcdef" , TokenType . OAuthToken ) ]
428
429
[ InlineData ( "ak_1234567890abcdef" , TokenType . ApiKey ) ]
429
430
[ InlineData ( "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9..." , TokenType . SessionToken ) ]
@@ -594,13 +595,58 @@ public async Task TestMachineTokenWithSecretKey()
594
595
var httpContext = CreateHttpContextWithToken ( "mt_test_token" ) ;
595
596
var state = await AuthenticateRequest . AuthenticateRequestAsync ( httpContext . Request , arOptions ) ;
596
597
597
- // Should attempt verification (will fail due to no real HTTP client, but won't fail on secret key)
598
598
Assert . NotEqual ( AuthErrorReason . SECRET_KEY_MISSING , state . ErrorReason ) ;
599
599
Assert . NotEqual ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
600
600
}
601
601
602
+ [ Fact ]
603
+ public async Task TestMachineTokenWithMachineSecretKey ( )
604
+ {
605
+ var arOptions = new AuthenticateRequestOptions (
606
+ machineSecretKey : "ms_test_machine_secret" ,
607
+ acceptsToken : new [ ] { "any" }
608
+ ) ;
609
+
610
+ var httpContext = CreateHttpContextWithToken ( "mt_test_token" ) ;
611
+ var state = await AuthenticateRequest . AuthenticateRequestAsync ( httpContext . Request , arOptions ) ;
612
+
613
+ Assert . NotEqual ( AuthErrorReason . SECRET_KEY_MISSING , state . ErrorReason ) ;
614
+ Assert . NotEqual ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
615
+ }
616
+
617
+ [ Fact ]
618
+ public async Task TestMachineTokenWithBothKeys ( )
619
+ {
620
+ var arOptions = new AuthenticateRequestOptions (
621
+ secretKey : "sk_test_secret" ,
622
+ machineSecretKey : "ms_test_machine_secret" ,
623
+ acceptsToken : new [ ] { "any" }
624
+ ) ;
625
+
626
+ var httpContext = CreateHttpContextWithToken ( "mt_test_token" ) ;
627
+ var state = await AuthenticateRequest . AuthenticateRequestAsync ( httpContext . Request , arOptions ) ;
628
+
629
+ Assert . NotEqual ( AuthErrorReason . SECRET_KEY_MISSING , state . ErrorReason ) ;
630
+ Assert . NotEqual ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
631
+ }
632
+
633
+ [ Fact ]
634
+ public async Task TestMachineTokenWithNoKeys ( )
635
+ {
636
+ var arOptions = new AuthenticateRequestOptions (
637
+ jwtKey : "test-jwt-key" ,
638
+ acceptsToken : new [ ] { "any" }
639
+ ) ;
640
+
641
+ var httpContext = CreateHttpContextWithToken ( "mt_test_token" ) ;
642
+ var state = await AuthenticateRequest . AuthenticateRequestAsync ( httpContext . Request , arOptions ) ;
643
+
644
+ Assert . Equal ( AuthErrorReason . SECRET_KEY_MISSING , state . ErrorReason ) ;
645
+ }
646
+
602
647
[ Theory ]
603
648
[ InlineData ( "mt_machine_token_123" ) ]
649
+ [ InlineData ( "m2m_machine_token_123" ) ]
604
650
[ InlineData ( "oat_oauth_token_123" ) ]
605
651
[ InlineData ( "ak_api_key_123" ) ]
606
652
public async Task TestDifferentMachineTokenPrefixes ( string token )
@@ -618,6 +664,109 @@ public async Task TestDifferentMachineTokenPrefixes(string token)
618
664
Assert . NotEqual ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
619
665
}
620
666
667
+ [ Fact ]
668
+ public async Task TestM2MTokenWithSecretKey ( )
669
+ {
670
+ var arOptions = new AuthenticateRequestOptions (
671
+ secretKey : "sk_test_secret" ,
672
+ acceptsToken : new [ ] { "m2m_token" }
673
+ ) ;
674
+
675
+ var httpContext = CreateHttpContextWithToken ( "m2m_test_token" ) ;
676
+ var state = await AuthenticateRequest . AuthenticateRequestAsync ( httpContext . Request , arOptions ) ;
677
+
678
+ Assert . NotEqual ( AuthErrorReason . SECRET_KEY_MISSING , state . ErrorReason ) ;
679
+ Assert . NotEqual ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
680
+ }
681
+
682
+ [ Fact ]
683
+ public async Task TestM2MTokenWithMachineSecretKey ( )
684
+ {
685
+ var arOptions = new AuthenticateRequestOptions (
686
+ machineSecretKey : "ms_test_machine_secret" ,
687
+ acceptsToken : new [ ] { "m2m_token" }
688
+ ) ;
689
+
690
+ var httpContext = CreateHttpContextWithToken ( "m2m_test_token" ) ;
691
+ var state = await AuthenticateRequest . AuthenticateRequestAsync ( httpContext . Request , arOptions ) ;
692
+
693
+ Assert . NotEqual ( AuthErrorReason . SECRET_KEY_MISSING , state . ErrorReason ) ;
694
+ Assert . NotEqual ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
695
+ }
696
+
697
+ [ Fact ]
698
+ public async Task TestM2MTokenWithBothKeys ( )
699
+ {
700
+ var arOptions = new AuthenticateRequestOptions (
701
+ secretKey : "sk_test_secret" ,
702
+ machineSecretKey : "ms_test_machine_secret" ,
703
+ acceptsToken : new [ ] { "m2m_token" }
704
+ ) ;
705
+
706
+ var httpContext = CreateHttpContextWithToken ( "m2m_test_token" ) ;
707
+ var state = await AuthenticateRequest . AuthenticateRequestAsync ( httpContext . Request , arOptions ) ;
708
+
709
+ Assert . NotEqual ( AuthErrorReason . SECRET_KEY_MISSING , state . ErrorReason ) ;
710
+ Assert . NotEqual ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
711
+ }
712
+
713
+ [ Fact ]
714
+ public async Task TestM2MTokenTypeAcceptance ( )
715
+ {
716
+ var arOptions = new AuthenticateRequestOptions (
717
+ secretKey : "sk_test_secret" ,
718
+ acceptsToken : new [ ] { "m2m_token" }
719
+ ) ;
720
+
721
+ var httpContext = CreateHttpContextWithToken ( "m2m_test_token" ) ;
722
+ var state = await AuthenticateRequest . AuthenticateRequestAsync ( httpContext . Request , arOptions ) ;
723
+
724
+ // Should not be rejected due to token type
725
+ Assert . NotEqual ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
726
+ }
727
+
728
+ [ Fact ]
729
+ public async Task TestM2MTokenRejectedWhenNotAccepted ( )
730
+ {
731
+ var arOptions = new AuthenticateRequestOptions (
732
+ secretKey : "sk_test_secret" ,
733
+ acceptsToken : new [ ] { "session_token" }
734
+ ) ;
735
+
736
+ var httpContext = CreateHttpContextWithToken ( "m2m_test_token" ) ;
737
+ var state = await AuthenticateRequest . AuthenticateRequestAsync ( httpContext . Request , arOptions ) ;
738
+
739
+ Assert . True ( state . IsSignedOut ( ) ) ;
740
+ Assert . Equal ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
741
+ }
742
+
743
+ [ Theory ]
744
+ [ InlineData ( "m2m_test_token" , new [ ] { "m2m_token" } , false ) ] // Should be accepted
745
+ [ InlineData ( "m2m_test_token" , new [ ] { "machine_token" } , false ) ] // Should be accepted (machine_token includes m2m_token)
746
+ [ InlineData ( "m2m_test_token" , new [ ] { "session_token" } , true ) ] // Should be rejected
747
+ [ InlineData ( "m2m_test_token" , new [ ] { "oauth_token" , "api_key" } , true ) ] // Should be rejected
748
+ public async Task TestM2MTokenTypeFiltering ( string token , string [ ] acceptedTypes , bool shouldBeRejected )
749
+ {
750
+ var arOptions = new AuthenticateRequestOptions (
751
+ secretKey : "sk_test_secret" ,
752
+ acceptsToken : acceptedTypes
753
+ ) ;
754
+
755
+ var httpContext = CreateHttpContextWithToken ( token ) ;
756
+ var state = await AuthenticateRequest . AuthenticateRequestAsync ( httpContext . Request , arOptions ) ;
757
+
758
+ if ( shouldBeRejected )
759
+ {
760
+ Assert . True ( state . IsSignedOut ( ) ) ;
761
+ Assert . Equal ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
762
+ }
763
+ else
764
+ {
765
+ // Token type is accepted, but verification might still fail
766
+ Assert . NotEqual ( AuthErrorReason . TOKEN_TYPE_NOT_SUPPORTED , state . ErrorReason ) ;
767
+ }
768
+ }
769
+
621
770
#endregion
622
771
623
772
#region Error Handling Tests
0 commit comments