@@ -606,14 +606,27 @@ async function discoverMetadataWithFallback(
606
606
}
607
607
608
608
/**
609
- * Identify common providers from metadata
610
- * Used for providers that have quirks needing conditional handling
609
+ * Using metadata, identifies common issuers that need special handling.
610
+ * Only for large, unusual issuers, fully spec compliant issuers should not be identified, small issuers should not be given special treatment.
611
611
* e.g. Azure no PKCE advertised, scope param instead of resource param.
612
612
*/
613
- function identifyProvider ( metadata : AuthorizationServerMetadata ) : "azure_v2" | undefined {
614
- if ( metadata . issuer . includes ( "login.microsoftonline.com" ) ) {
615
- return "azure_v2"
613
+ function identifyQuirkyIssuer ( metadata : AuthorizationServerMetadata ) : "azure_v2" | undefined {
614
+ const issuerString = metadata . issuer ;
615
+ let issuerUrl : URL ;
616
+ // Parse issuer URL and treat failed parse as normal issuer.
617
+ try {
618
+ issuerUrl = new URL ( issuerString ) ;
619
+ } catch ( e ) {
620
+ if ( e instanceof TypeError && e . message === "Invalid URL" ) {
621
+ return undefined ;
622
+ }
623
+ throw e ;
616
624
}
625
+ // Check for known issuer types needing conditional handling
626
+ if ( issuerUrl . hostname === "login.microsoftonline.com" && issuerUrl . pathname . endsWith ( '/v2.0' ) ) {
627
+ return "azure_v2" ;
628
+ }
629
+ return undefined ;
617
630
}
618
631
619
632
/**
@@ -790,7 +803,7 @@ export async function discoverAuthorizationServerMetadata(
790
803
} else {
791
804
const metadata = OpenIdProviderDiscoveryMetadataSchema . parse ( await response . json ( ) ) ;
792
805
// Azure Bypass
793
- if ( identifyProvider ( metadata ) === "azure_v2" && ! metadata . code_challenge_methods_supported ) {
806
+ if ( identifyQuirkyIssuer ( metadata ) === "azure_v2" && ! metadata . code_challenge_methods_supported ) {
794
807
metadata . code_challenge_methods_supported = [ "S256" ] ;
795
808
}
796
809
@@ -884,7 +897,7 @@ export async function startAuthorization(
884
897
}
885
898
886
899
if ( resource ) {
887
- if ( metadata && identifyProvider ( metadata ) === "azure_v2" ) {
900
+ if ( metadata && identifyQuirkyIssuer ( metadata ) === "azure_v2" ) {
888
901
authorizationUrl . searchParams . set ( "scope" , `${ resource . href } /.default` ) ;
889
902
} else {
890
903
authorizationUrl . searchParams . set ( "resource" , resource . href ) ;
@@ -966,7 +979,7 @@ export async function exchangeAuthorization(
966
979
}
967
980
968
981
if ( resource ) {
969
- if ( metadata && identifyProvider ( metadata ) === "azure_v2" ) {
982
+ if ( metadata && identifyQuirkyIssuer ( metadata ) === "azure_v2" ) {
970
983
params . set ( "scope" , `${ resource . href } /.default` ) ;
971
984
} else {
972
985
params . set ( "resource" , resource . href ) ;
@@ -1054,7 +1067,7 @@ export async function refreshAuthorization(
1054
1067
}
1055
1068
1056
1069
if ( resource ) {
1057
- if ( metadata && identifyProvider ( metadata ) === "azure_v2" ) {
1070
+ if ( metadata && identifyQuirkyIssuer ( metadata ) === "azure_v2" ) {
1058
1071
params . set ( "scope" , `${ resource . href } /.default` ) ;
1059
1072
} else {
1060
1073
params . set ( "resource" , resource . href ) ;
0 commit comments