Skip to content

Commit

Permalink
Bumped Android MSAL to 5.1.x and updated deprecated functions in Sign…
Browse files Browse the repository at this point in the history
…InSilent. Bumped the Android CompileSDK to API level 34.
  • Loading branch information
wrobins committed Feb 15, 2024
1 parent ecdca15 commit d909884
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-msal",
"version": "4.0.4",
"version": "4.1.0",
"description": "A Cordova plugin providing a wrapper for Microsoft's MSAL library for Android and iOS.",
"cordova": {
"id": "cordova-plugin-msal",
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-msal" version="4.0.4">
id="cordova-plugin-msal" version="4.1.0">
<name>Cordova MSAL Plugin</name>
<description>A Cordova plugin providing a wrapper for Microsoft's MSAL library for Android and iOS.</description>
<author>Walter Robins</author>
Expand Down Expand Up @@ -48,7 +48,7 @@
</config-file>
<source-file src="src/android/MsalPlugin.java" target-dir="src/com/wrobins/cordova/plugin" />
<framework src="src/android/build-extras.gradle" custom="true" type="gradleReference" />
<framework src="com.microsoft.identity.client:msal:5.0.+" />
<framework src="com.microsoft.identity.client:msal:5.1.0" />
</platform>
<platform name="ios">
<config-file target="config.xml" parent="/*">
Expand Down
79 changes: 52 additions & 27 deletions src/android/MsalPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -24,8 +26,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.microsoft.identity.client.AcquireTokenParameters;
import com.microsoft.identity.client.AcquireTokenSilentParameters;
import com.microsoft.identity.client.AuthenticationCallback;
import com.microsoft.identity.client.IAccount;
import com.microsoft.identity.client.IAuthenticationResult;
Expand All @@ -36,6 +40,7 @@
import com.microsoft.identity.client.Prompt;
import com.microsoft.identity.client.PublicClientApplication;
import com.microsoft.identity.client.MultipleAccountPublicClientApplication;
import com.microsoft.identity.client.SilentAuthenticationCallback;
import com.microsoft.identity.client.exception.MsalException;


Expand Down Expand Up @@ -130,7 +135,6 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
prompt = Prompt.CONSENT;
break;
default:
prompt = Prompt.WHEN_REQUIRED;
}
}
List<Map.Entry<String, String>> authorizationQueryStringParameters = new ArrayList<>();
Expand All @@ -141,17 +145,15 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
JSONObject queryParam = queryParams.getJSONObject(i);
params.put(queryParam.getString("param"), queryParam.getString("value"));
}
for (Map.Entry<String, String> param: params.entrySet()) {
authorizationQueryStringParameters.add(param);
}
authorizationQueryStringParameters.addAll(params.entrySet());
}
ArrayList<String> scopes = new ArrayList<String>();
String[] otherScopesToAuthorize = new String[] {};
if (args.length() > 3) {
for (int i = 0; i < args.getJSONArray(3).length(); ++i) {
scopes.add(args.getJSONArray(3).getString(i));
}
otherScopesToAuthorize = scopes.toArray(new String[scopes.size()]);
otherScopesToAuthorize = scopes.toArray(new String[0]);
}
this.signinUserInteractive(loginHint, authorizationQueryStringParameters, prompt, otherScopesToAuthorize);
}
Expand Down Expand Up @@ -188,16 +190,16 @@ public void run() {
for (int i = 0; i < authoritiesList.length(); ++i) {
JSONObject authority = authoritiesList.getJSONObject(i);
authorities.append(" {\n");
authorities.append(" \"type\": \"" + authority.getString("type") + "\",\n");
authorities.append(" \"type\": \"").append(authority.getString("type")).append("\",\n");
authorities.append(" \"audience\": {\n");
authorities.append(" \"type\": \"" + authority.getString("audience") + "\",\n");
authorities.append(" \"tenant_id\": \"" + MsalPlugin.this.tenantId + "\"\n");
StringBuilder audience = authorities.append(" \"type\": \"").append(authority.getString("audience")).append("\",\n");
authorities.append(" \"tenant_id\": \"").append(MsalPlugin.this.tenantId).append("\"\n");
authorities.append(" },\n");
if (authority.has("authorityUrl") && !authority.getString("authorityUrl").equals("")) {
authorities.append(" \"authority_url\": \"" + authority.getString("authorityUrl") + "\",\n");
authorities.append(" \"authority_url\": \"").append(authority.getString("authorityUrl")).append("\",\n");
}
if (authority.has("default")) {
authorities.append(" \"default\": " + authority.getBoolean("default") + "\n");
authorities.append(" \"default\": ").append(authority.getBoolean("default")).append("\n");
}
if (i < authoritiesList.length() - 1) {
authorities.append(" },\n");
Expand Down Expand Up @@ -231,13 +233,11 @@ public void run() {
for (int i = 0; i < options.getJSONArray("scopes").length(); ++i) {
scopes.add(options.getJSONArray("scopes").getString(i));
}
MsalPlugin.this.scopes = scopes.toArray(new String[scopes.size()]);
MsalPlugin.this.scopes = scopes.toArray(new String[0]);
MsalPlugin.this.isInit = true;
MsalPlugin.this.callbackContext.success();
} catch (JSONException ignored) {}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MsalException e) {
} catch (InterruptedException | MsalException e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -323,8 +323,22 @@ public void run() {
if (MsalPlugin.this.appSingleClient.getCurrentAccount().getCurrentAccount() == null) {
MsalPlugin.this.callbackContext.error("No account currently exists");
} else {
IAuthenticationResult silentAuthResult = MsalPlugin.this.appSingleClient.acquireTokenSilent(MsalPlugin.this.scopes, authority);
MsalPlugin.this.callbackContext.success(getAuthResult(silentAuthResult));
AcquireTokenSilentParameters params = new AcquireTokenSilentParameters.Builder()
.withScopes(Arrays.asList(MsalPlugin.this.scopes))
.fromAuthority(authority)
.withCallback(new SilentAuthenticationCallback() {
@Override
public void onSuccess(IAuthenticationResult authenticationResult) {
MsalPlugin.this.callbackContext.success(getAuthResult(authenticationResult));
}

@Override
public void onError(MsalException exception) {
MsalPlugin.this.callbackContext.error(exception.getMessage());
}
})
.build();
MsalPlugin.this.appSingleClient.acquireTokenSilent(params);
}
} catch (InterruptedException e) {
MsalPlugin.this.callbackContext.error(e.getMessage());
Expand All @@ -351,12 +365,23 @@ public void run() {
return;
}
String authority = MsalPlugin.this.appMultipleClient.getConfiguration().getDefaultAuthority().getAuthorityURL().toString();
IAuthenticationResult result = MsalPlugin.this.appMultipleClient.acquireTokenSilent(
MsalPlugin.this.scopes,
MsalPlugin.this.appMultipleClient.getAccount(account),
authority
);
MsalPlugin.this.callbackContext.success(getAuthResult(result));
AcquireTokenSilentParameters params = new AcquireTokenSilentParameters.Builder()
.withScopes(Arrays.asList(MsalPlugin.this.scopes))
.fromAuthority(authority)
.forAccount(MsalPlugin.this.appMultipleClient.getAccount(account))
.withCallback(new SilentAuthenticationCallback() {
@Override
public void onSuccess(IAuthenticationResult authenticationResult) {
MsalPlugin.this.callbackContext.success(getAuthResult(authenticationResult));
}

@Override
public void onError(MsalException exception) {
MsalPlugin.this.callbackContext.error(exception.getMessage());
}
})
.build();
MsalPlugin.this.appMultipleClient.acquireTokenSilent(params);
} catch (InterruptedException e) {
MsalPlugin.this.callbackContext.error(e.getMessage());
} catch (MsalException e) {
Expand All @@ -370,7 +395,7 @@ public void run() {

private void signinUserInteractive(final String loginHint, final List<Map.Entry<String, String>> authorizationQueryStringParameters, final Prompt prompt, final String[] otherScopesToAuthorize) {
if (this.checkConfigInit()) {
if (this.SINGLE_ACCOUNT.equals(this.accountMode)) {
if (SINGLE_ACCOUNT.equals(this.accountMode)) {
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -444,7 +469,7 @@ public void onError(MsalException e) {

private void signOut(final String account) {
this.checkConfigInit();
if (this.SINGLE_ACCOUNT.equals(this.accountMode)) {
if (SINGLE_ACCOUNT.equals(this.accountMode)) {
cordova.getThreadPool().execute(new Runnable() {
@Override
public void run() {
Expand All @@ -471,7 +496,7 @@ public void onSignOut() {
}

@Override
public void onError(MsalException e) {
public void onError(@NonNull MsalException e) {
MsalPlugin.this.callbackContext.error(e.getMessage());
}
});
Expand All @@ -498,7 +523,7 @@ public void onRemoved() {
}

@Override
public void onError(MsalException e) {
public void onError(@NonNull MsalException e) {
MsalPlugin.this.callbackContext.error(e.getMessage());
}
});
Expand Down Expand Up @@ -552,7 +577,7 @@ private JSONObject getAccountObject(IAccount account) {
try {
acct.put("id", account.getId());
acct.put("username", account.getUsername());
acct.put("claims", processClaims(account.getClaims()));
acct.put("claims", processClaims(Objects.requireNonNull(account.getClaims())));
} catch (JSONException e) {
MsalPlugin.this.callbackContext.error(e.getMessage());
}
Expand Down
8 changes: 1 addition & 7 deletions src/android/build-extras.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,4 @@ repositories {
}
}

dependencies {
implementation('com.microsoft.identity.client:msal:5.0.+') {
exclude group: 'io.opentelemetry'
}
implementation 'io.opentelemetry:opentelemetry-api:1.18.0'
implementation 'io.opentelemetry:opentelemetry-context:1.18.0'
}
ext.cdvCompileSdkVersion = 34

0 comments on commit d909884

Please sign in to comment.