@@ -52,8 +52,8 @@ public class RNMSALModule extends ReactContextBaseJavaModule {
5252 private static final String AUTHORITY_TYPE_B2C = "B2C" ;
5353 private static final String AUTHORITY_TYPE_AAD = "AAD" ;
5454
55- private static final Pattern aadMyOrgAuthorityPattern = Pattern .compile ("https://login.microsoftonline.com/(?<tenant>. +)" );
56- private static final Pattern b2cAuthorityPattern = Pattern .compile ("https://.+? /tfp/(?<tenant>.+? )/.+" );
55+ private static final Pattern aadAuthorityPattern = Pattern .compile ("https://login\\ .microsoftonline\\ .com/([^/] +)" );
56+ private static final Pattern b2cAuthorityPattern = Pattern .compile ("https://([^/]+) /tfp/([^/]+ )/.+" );
5757
5858 private IMultipleAccountPublicClientApplication publicClientApplication ;
5959
@@ -134,7 +134,7 @@ public void createPublicClientApplication(ReadableMap params, Promise promise) {
134134 }
135135 }
136136
137- private JSONArray makeAuthoritiesJsonArray (List <String > authorityUrls , String authority ) throws JSONException {
137+ private JSONArray makeAuthoritiesJsonArray (List <String > authorityUrls , String authority ) throws JSONException , IllegalArgumentException {
138138 JSONArray authoritiesJsonArr = new JSONArray ();
139139 boolean foundDefaultAuthority = false ;
140140
@@ -147,38 +147,39 @@ private JSONArray makeAuthoritiesJsonArray(List<String> authorityUrls, String au
147147 foundDefaultAuthority = true ;
148148 }
149149
150- // Parse this information from the authority url. Some variables will end up staying null
151- String type = null , audience_type = null , audience_tenantId = null , b2cAuthorityUrl = null ;
152-
150+ Matcher aadAuthorityMatcher = aadAuthorityPattern .matcher (authorityUrl );
153151 Matcher b2cAuthorityMatcher = b2cAuthorityPattern .matcher (authorityUrl );
154- Matcher aadMyOrgAuthorityMatcher = aadMyOrgAuthorityPattern .matcher (authorityUrl );
155-
156- if (authorityUrl .equals ("https://login.microsoftonline.com/common" )) {
157- type = AUTHORITY_TYPE_AAD ;
158- audience_type = "AzureADandPersonalMicrosoftAccount" ;
159- } else if (authorityUrl .equals ("https://login.microsoftonline.com/organizations" )) {
160- type = AUTHORITY_TYPE_AAD ;
161- audience_type = "AzureADMultipleOrgs" ;
162- } else if (authorityUrl .equals ("https://login.microsoftonline.com/consumers" )) {
163- type = AUTHORITY_TYPE_AAD ;
164- audience_type = "PersonalMicrosoftAccount" ;
152+
153+ if (aadAuthorityMatcher .find ()) {
154+ String group = aadAuthorityMatcher .group (1 );
155+ if (group == null )
156+ throw new IllegalArgumentException ("Could not match group 1 for regex https://login.microsoftonline.com/([^/]+) in authority \" " + authorityUrl + "\" " );
157+
158+ JSONObject audience ;
159+ switch (group ) {
160+ case "common" :
161+ audience = new JSONObject ().put ("type" , "AzureADandPersonalMicrosoftAccount" );
162+ break ;
163+ case "organizations" :
164+ audience = new JSONObject ().put ("type" , "AzureADMultipleOrgs" );
165+ break ;
166+ case "consumers" :
167+ audience = new JSONObject ().put ("type" , "PersonalMicrosoftAccount" );
168+ break ;
169+ default :
170+ // assume `group` is a tenant id
171+ audience = new JSONObject ().put ("type" , "AzureADMyOrg" ).put ("tenant_id" , group );
172+ break ;
173+ }
174+ authorityJsonObj .put ("type" , AUTHORITY_TYPE_AAD );
175+ authorityJsonObj .put ("audience" , audience );
165176 } else if (b2cAuthorityMatcher .find ()) {
166- type = AUTHORITY_TYPE_B2C ;
167- b2cAuthorityUrl = authorityUrl ;
168- } else if (aadMyOrgAuthorityMatcher .find ()) {
169- type = AUTHORITY_TYPE_AAD ;
170- audience_type = "AzureADMyOrg" ;
171- audience_tenantId = aadMyOrgAuthorityMatcher .group (1 );
177+ authorityJsonObj .put ("type" , AUTHORITY_TYPE_B2C );
178+ authorityJsonObj .put ("authority_url" , authorityUrl );
179+ } else {
180+ throw new IllegalArgumentException ("Authority \" " + authorityUrl + "\" doesn't match AAD regex https://login.microsoftonline.com/([^/]+) or B2C regex https://([^/]+)/tfp/([^/]+)/.+" );
172181 }
173182
174- authorityJsonObj
175- .put ("type" , type )
176- .put ("authority_url" , b2cAuthorityUrl )
177- .put ("audience" , audience_type == null ? null : new JSONObject ()
178- .put ("type" , audience_type )
179- .put ("tenant_id" , audience_tenantId )
180- );
181-
182183 authoritiesJsonArr .put (authorityJsonObj );
183184 }
184185
@@ -396,8 +397,8 @@ private WritableMap msalResultToDictionary(@NonNull IAuthenticationResult result
396397 map .putString ("accessToken" , result .getAccessToken ());
397398 map .putString ("expiresOn" , String .format ("%s" , result .getExpiresOn ().getTime () / 1000 ));
398399 String idToken = result .getAccount ().getIdToken ();
399- if (idToken == null ){
400- idToken = ((IMultiTenantAccount ) result .getAccount ()).getTenantProfiles ().get (result .getTenantId ()).getIdToken ();
400+ if (idToken == null ) {
401+ idToken = ((IMultiTenantAccount ) result .getAccount ()).getTenantProfiles ().get (result .getTenantId ()).getIdToken ();
401402 }
402403 map .putString ("idToken" , idToken );
403404 map .putArray ("scopes" , Arguments .fromArray (result .getScope ()));
0 commit comments