@@ -507,3 +507,121 @@ func TestMergeCapabilitiesAndClaims(t *testing.T) {
507507 })
508508 }
509509}
510+
511+ func TestTenantDiscoveryValidateIssuer (t * testing.T ) {
512+ tests := []struct {
513+ desc string
514+ issuer string
515+ authority string
516+ aliases map [string ]bool
517+ expectError bool
518+ }{
519+ {
520+ desc : "issuer exactly matches authority" ,
521+ issuer : "https://login.microsoftonline.com/tenant-id" ,
522+ authority : "https://login.microsoftonline.com/tenant-id" ,
523+ expectError : false ,
524+ },
525+ {
526+ desc : "issuer matches authority with trailing slash in authority" ,
527+ issuer : "https://login.microsoftonline.com/tenant-id" ,
528+ authority : "https://login.microsoftonline.com/tenant-id/" ,
529+ expectError : false ,
530+ },
531+ {
532+ desc : "issuer matches authority with trailing slash in issuer" ,
533+ issuer : "https://login.microsoftonline.com/tenant-id/" ,
534+ authority : "https://login.microsoftonline.com/tenant-id" ,
535+ expectError : false ,
536+ },
537+ {
538+ desc : "issuer is shorter than authority but is a prefix" ,
539+ issuer : "https://login.microsoftonline.com" ,
540+ authority : "https://login.microsoftonline.com/tenant-id" ,
541+ expectError : false ,
542+ },
543+ {
544+ desc : "authority is shorter than issuer but is a prefix" ,
545+ issuer : "https://login.microsoftonline.com/tenant-id/additional-path" ,
546+ authority : "https://login.microsoftonline.com/tenant-id" ,
547+ expectError : false ,
548+ },
549+ {
550+ desc : "issuer and authority have different paths" ,
551+ issuer : "https://login.microsoftonline.com/other-tenant" ,
552+ authority : "https://login.microsoftonline.com/tenant-id" ,
553+ expectError : false ,
554+ },
555+ {
556+ desc : "custom authority with a non-matching Entra issuer" ,
557+ issuer : "https://login.microsoftonline.com/" ,
558+ authority : "https://contoso.com/tenant-id" ,
559+ expectError : true ,
560+ },
561+ {
562+ desc : "Entra authority with a non-matching custom issuer" ,
563+ issuer : "https://contoso.com/" ,
564+ authority : "https://login.microsoftonline.com/tenant-id" ,
565+ expectError : true ,
566+ },
567+ {
568+ desc : "empty issuer" ,
569+ issuer : "" ,
570+ authority : "https://login.microsoftonline.com/tenant-id" ,
571+ expectError : true ,
572+ },
573+ {
574+ desc : "empty issuer and authority" ,
575+ issuer : "" ,
576+ authority : "" ,
577+ aliases : map [string ]bool {"alias1.example.com" : true , "alias2.example.com" : true },
578+ expectError : true ,
579+ },
580+ // New test cases for alias validation
581+ {
582+ desc : "issuer matches an alias" ,
583+ issuer : "https://alias1.example.com/tenant-id" ,
584+ authority : "https://contoso.com/tenant-id" ,
585+ aliases : map [string ]bool {"alias1.example.com" : true , "alias2.example.com" : true },
586+ expectError : false ,
587+ },
588+ {
589+ desc : "issuer matches a different alias" ,
590+ issuer : "https://alias2.example.com/tenant-id" ,
591+ authority : "https://contoso.com/tenant-id" ,
592+ aliases : map [string ]bool {"alias1.example.com" : true , "alias2.example.com" : true },
593+ expectError : false ,
594+ },
595+ {
596+ desc : "issuer doesn't match any alias" ,
597+ issuer : "https://unknown.example.com/tenant-id" ,
598+ authority : "https://contoso.com/tenant-id" ,
599+ aliases : map [string ]bool {"alias1.example.com" : true , "alias2.example.com" : true },
600+ expectError : true ,
601+ },
602+ {
603+ desc : "empty aliases map" ,
604+ issuer : "https://unknown.example.com/tenant-id" ,
605+ authority : "https://contoso.com/tenant-id" ,
606+ aliases : map [string ]bool {},
607+ expectError : true ,
608+ },
609+ }
610+
611+ for _ , test := range tests {
612+ t .Run (test .desc , func (t * testing.T ) {
613+ response := & TenantDiscoveryResponse {
614+ AuthorizationEndpoint : "https://login.microsoftonline.com/tenant-id/oauth2/v2.0/authorize" ,
615+ TokenEndpoint : "https://login.microsoftonline.com/tenant-id/oauth2/v2.0/token" ,
616+ Issuer : test .issuer ,
617+ }
618+
619+ err := response .ValidateIssuerMatchesAuthority (test .authority , test .aliases )
620+ if test .expectError && err == nil {
621+ t .Errorf ("expected error but got none" )
622+ } else if ! test .expectError && err != nil {
623+ t .Errorf ("unexpected error: %v" , err )
624+ }
625+ })
626+ }
627+ }
0 commit comments