Skip to content

Commit 52952ac

Browse files
jurusixJuraj Jakubik
andauthored
Add MS Entra External ID support (CIAM) (#48)
* add ms entra external id (CIAM) * add support for ios * add missing authorityType (CIAM) to definitions --------- Co-authored-by: Juraj Jakubik <[email protected]>
1 parent 985b902 commit 52952ac

File tree

7 files changed

+51
-44
lines changed

7 files changed

+51
-44
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ const result = await MsAuthPlugin.login({
8181
domainHint: '<domainHint>',
8282
scopes: ['<scopes, defaults to no scopes>'],
8383
keyHash: '<Android only, the key hash as obtained above>',
84+
authorityType: '<AAD/B2C/CIAM>',
85+
authorityUrl: '<To sign the user into a specific CIAM tenant, configure with a specific authority. For example: https://xxx.ciamlogin.com/dddd5555-eeee-6666-ffff-00001111aaaa>',
8486
});
8587

8688
const accessToken = result.accessToken;

android/src/main/java/nl/recognize/msauthplugin/AuthorityType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
public enum AuthorityType {
44
AAD,
5-
B2C
5+
B2C,
6+
CIAM
67
}

android/src/main/java/nl/recognize/msauthplugin/MsAuthPlugin.java

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import com.microsoft.identity.client.*;
1313
import com.microsoft.identity.client.exception.MsalException;
1414
import com.microsoft.identity.client.exception.MsalUiRequiredException;
15-
1615
import java.io.File;
1716
import java.io.FileWriter;
1817
import java.io.IOException;
@@ -124,9 +123,9 @@ private void acquireToken(ISingleAccountPublicClientApplication context, List<St
124123
try {
125124
Logger.info("Starting silent login flow");
126125
AcquireTokenSilentParameters.Builder builder = new AcquireTokenSilentParameters.Builder()
127-
.withScopes(scopes)
128-
.fromAuthority(authority)
129-
.forAccount(result.getCurrentAccount());
126+
.withScopes(scopes)
127+
.fromAuthority(authority)
128+
.forAccount(result.getCurrentAccount());
130129

131130
AcquireTokenSilentParameters parameters = builder.build();
132131
IAuthenticationResult silentAuthResult = context.acquireTokenSilent(parameters);
@@ -147,44 +146,43 @@ private void acquireToken(ISingleAccountPublicClientApplication context, List<St
147146

148147
Logger.info("Starting interactive login flow");
149148
AcquireTokenParameters.Builder params = new AcquireTokenParameters.Builder()
150-
.startAuthorizationFromActivity(this.getActivity())
151-
.withScopes(scopes)
152-
.withPrompt(Prompt.SELECT_ACCOUNT)
153-
.withCallback(
154-
new AuthenticationCallback() {
155-
@Override
156-
public void onCancel() {
157-
Logger.info("Login cancelled");
158-
callback.tokenReceived(null);
159-
}
160-
161-
@Override
162-
public void onSuccess(IAuthenticationResult authenticationResult) {
163-
TokenResult tokenResult = new TokenResult();
164-
165-
IAccount account = authenticationResult.getAccount();
166-
tokenResult.setAccessToken(authenticationResult.getAccessToken());
167-
tokenResult.setIdToken(account.getIdToken());
168-
tokenResult.setScopes(authenticationResult.getScope());
169-
170-
callback.tokenReceived(tokenResult);
171-
}
172-
173-
@Override
174-
public void onError(MsalException ex) {
175-
Logger.error("Unable to acquire token interactively", ex);
176-
callback.tokenReceived(null);
177-
}
178-
}
179-
);
149+
.startAuthorizationFromActivity(this.getActivity())
150+
.withScopes(scopes)
151+
.withPrompt(Prompt.SELECT_ACCOUNT)
152+
.withCallback(
153+
new AuthenticationCallback() {
154+
@Override
155+
public void onCancel() {
156+
Logger.info("Login cancelled");
157+
callback.tokenReceived(null);
158+
}
159+
160+
@Override
161+
public void onSuccess(IAuthenticationResult authenticationResult) {
162+
TokenResult tokenResult = new TokenResult();
163+
164+
IAccount account = authenticationResult.getAccount();
165+
tokenResult.setAccessToken(authenticationResult.getAccessToken());
166+
tokenResult.setIdToken(account.getIdToken());
167+
tokenResult.setScopes(authenticationResult.getScope());
168+
169+
callback.tokenReceived(tokenResult);
170+
}
171+
172+
@Override
173+
public void onError(MsalException ex) {
174+
Logger.error("Unable to acquire token interactively", ex);
175+
callback.tokenReceived(null);
176+
}
177+
}
178+
);
180179

181180
if (result.getCurrentAccount() != null) {
182181
// Set loginHint otherwise MSAL throws an exception because of mismatched account
183182
params.withLoginHint(result.getCurrentAccount().getUsername());
184183
}
185184

186185
context.acquireToken(params.build());
187-
188186
}
189187

190188
private ISingleAccountPublicClientApplication createContextFromPluginCall(PluginCall call)
@@ -207,8 +205,10 @@ private ISingleAccountPublicClientApplication createContextFromPluginCall(Plugin
207205
authorityType = AuthorityType.AAD;
208206
} else if (AuthorityType.B2C.name().equals(authorityTypeString)) {
209207
authorityType = AuthorityType.B2C;
208+
} else if (AuthorityType.CIAM.name().equals(authorityTypeString)) {
209+
authorityType = AuthorityType.CIAM;
210210
} else {
211-
call.reject("Invalid authorityType specified. Only AAD and B2C are supported.");
211+
call.reject("Invalid authorityType specified. Only AAD, B2C and CIAM are supported.");
212212
return null;
213213
}
214214

@@ -244,6 +244,9 @@ private ISingleAccountPublicClientApplication createContext(
244244
authorityConfig.put("authority_url", authorityUrl);
245245
authorityConfig.put("default", "true");
246246
break;
247+
case CIAM:
248+
authorityConfig.put("type", AuthorityType.CIAM.name()).put("authority_url", authorityUrl);
249+
break;
247250
}
248251

249252
configFile.put("client_id", clientId);

ios/Plugin/Plugin.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public class MsAuthPlugin: CAPPlugin {
131131
let authorityURL = call.getString("authorityUrl")
132132
let authorityType = call.getString("authorityType") ?? "AAD"
133133

134-
if authorityType != "AAD" && authorityType != "B2C" {
135-
call.reject("authorityType must be one of 'AAD' or 'B2C'")
134+
if authorityType != "AAD" && authorityType != "B2C" && authorityType != "CIAM" {
135+
call.reject("authorityType must be one of 'AAD' or 'B2C' or 'CIAM'")
136136
return nil
137137
}
138138

@@ -154,7 +154,7 @@ public class MsAuthPlugin: CAPPlugin {
154154
}
155155

156156
do {
157-
let authority = authorityType == .aad
157+
let authority = authorityType == .aad || authorityType == .ciam
158158
? try MSALAADAuthority(url: authorityURL) : try MSALB2CAuthority(url: authorityURL)
159159

160160
if domainHint != nil {
@@ -302,6 +302,7 @@ public class MsAuthPlugin: CAPPlugin {
302302
enum AuthorityType: String {
303303
case aad
304304
case b2c
305+
case ciam
305306
}
306307

307308
extension UIApplicationDelegate {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@recognizebv/capacitor-plugin-msauth",
3-
"version": "3.6.1",
3+
"version": "3.6.2",
44
"description": "This plugin enables MSAL support for Capacitor.",
55
"main": "dist/plugin.cjs.js",
66
"module": "dist/esm/index.js",

src/definitions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface BaseOptions {
22
clientId: string;
33
tenant?: string;
44
domainHint?: string;
5-
authorityType?: 'AAD' | 'B2C';
5+
authorityType?: 'AAD' | 'B2C' | 'CIAM';
66
authorityUrl?: string;
77
knownAuthorities?: string[];
88
keyHash?: string;

0 commit comments

Comments
 (0)