Skip to content

Commit fc58e8b

Browse files
author
wslaghekke
committed
Fix build by making setup more like sample capacitorjs plugin
1 parent 76ddb7b commit fc58e8b

File tree

5 files changed

+642
-1509
lines changed

5 files changed

+642
-1509
lines changed

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,19 @@ public void login(final PluginCall call) {
7272
}
7373
}
7474

75-
this.acquireToken(
76-
context,
77-
call.getArray("scopes").toList(),
78-
prompt,
79-
tokenResult -> {
80-
if (tokenResult != null) {
81-
JSObject result = new JSObject();
82-
result.put("accessToken", tokenResult.getAccessToken());
83-
result.put("idToken", tokenResult.getIdToken());
84-
JSONArray scopes = new JSONArray(Arrays.asList(tokenResult.getScopes()));
85-
result.put("scopes", scopes);
86-
87-
call.resolve(result);
88-
} else {
89-
call.reject("Unable to obtain access token");
90-
}
75+
this.acquireToken(context, call.getArray("scopes").toList(), prompt, tokenResult -> {
76+
if (tokenResult != null) {
77+
JSObject result = new JSObject();
78+
result.put("accessToken", tokenResult.getAccessToken());
79+
result.put("idToken", tokenResult.getIdToken());
80+
JSONArray scopes = new JSONArray(Arrays.asList(tokenResult.getScopes()));
81+
result.put("scopes", scopes);
82+
83+
call.resolve(result);
84+
} else {
85+
call.reject("Unable to obtain access token");
9186
}
92-
);
87+
});
9388
} catch (Exception ex) {
9489
Logger.error("Unable to login: " + ex.getMessage(), ex);
9590
call.reject("Unable to fetch access token.");
@@ -139,14 +134,17 @@ protected String getAuthorityUrl(ISingleAccountPublicClientApplication context)
139134
return context.getConfiguration().getDefaultAuthority().getAuthorityURL().toString();
140135
}
141136

142-
private void acquireToken(ISingleAccountPublicClientApplication context, List<String> scopes, Prompt prompt, final TokenResultCallback callback)
143-
throws MsalException, InterruptedException {
137+
private void acquireToken(
138+
ISingleAccountPublicClientApplication context,
139+
List<String> scopes,
140+
Prompt prompt,
141+
final TokenResultCallback callback
142+
) throws MsalException, InterruptedException {
144143
String authority = getAuthorityUrl(context);
145144

146145
ICurrentAccountResult result = context.getCurrentAccount();
147146
if (result.getCurrentAccount() != null) {
148147
try {
149-
150148
Logger.info("Starting silent login flow");
151149
AcquireTokenSilentParameters.Builder builder = new AcquireTokenSilentParameters.Builder()
152150
.withScopes(scopes)

android/src/test/java/nl/recognize/msauthplugin/MsAuthPluginTest.java

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,38 @@ class MsAuthPluginTest {
5959
void setUp() throws MsalException, InterruptedException {
6060
reset(mockedContext, mockedActivity, publicClientApplicationFactoryMock, singleAccountPublicClientApplication);
6161

62-
when(publicClientApplicationFactoryMock.createSingleAccountPublicClientApplication(any(Context.class), any(File.class)))
63-
.thenReturn(singleAccountPublicClientApplication);
62+
when(publicClientApplicationFactoryMock.createSingleAccountPublicClientApplication(any(Context.class), any(File.class))).thenReturn(
63+
singleAccountPublicClientApplication
64+
);
6465

65-
plugin =
66-
new MsAuthPlugin(publicClientApplicationFactoryMock) {
67-
final Context applicationContext = mock(Context.class);
66+
plugin = new MsAuthPlugin(publicClientApplicationFactoryMock) {
67+
final Context applicationContext = mock(Context.class);
6868

69-
@Override
70-
public Context getContext() {
71-
when(mockedContext.getApplicationContext()).thenReturn(applicationContext);
69+
@Override
70+
public Context getContext() {
71+
when(mockedContext.getApplicationContext()).thenReturn(applicationContext);
7272

73-
return mockedContext;
74-
}
73+
return mockedContext;
74+
}
7575

76-
@Override
77-
public AppCompatActivity getActivity() {
78-
lenient().when(applicationContext.getPackageName()).thenReturn("nl.recognize.project-x");
79-
lenient().when(mockedActivity.getApplicationContext()).thenReturn(applicationContext);
76+
@Override
77+
public AppCompatActivity getActivity() {
78+
lenient().when(applicationContext.getPackageName()).thenReturn("nl.recognize.project-x");
79+
lenient().when(mockedActivity.getApplicationContext()).thenReturn(applicationContext);
8080

81-
return mockedActivity;
82-
}
81+
return mockedActivity;
82+
}
8383

84-
@Override
85-
protected String getLogTag() {
86-
return "LogTag";
87-
}
84+
@Override
85+
protected String getLogTag() {
86+
return "LogTag";
87+
}
8888

89-
@Override
90-
protected String getAuthorityUrl(ISingleAccountPublicClientApplication context) {
91-
return AUTHORITY_URL;
92-
}
93-
};
89+
@Override
90+
protected String getAuthorityUrl(ISingleAccountPublicClientApplication context) {
91+
return AUTHORITY_URL;
92+
}
93+
};
9494
}
9595

9696
@Test
@@ -109,8 +109,7 @@ void loginExpectAcquireTokenSilent() throws JSONException, MsalException, Interr
109109
parameters -> parameters.getScopes().equals(List.of("mocked-scope")) && parameters.getAuthority().equals(AUTHORITY_URL)
110110
)
111111
)
112-
)
113-
.thenReturn(result);
112+
).thenReturn(result);
114113

115114
ArgumentCaptor<JSObject> jsObjectCaptor = ArgumentCaptor.forClass(JSObject.class);
116115
doNothing().when(pluginCallMock).resolve(jsObjectCaptor.capture());
@@ -123,31 +122,31 @@ void loginExpectAcquireTokenSilent() throws JSONException, MsalException, Interr
123122
assertEquals("access-token", resolve.getString("accessToken"));
124123
assertEquals(ID_TOKEN, resolve.getString("idToken"));
125124

126-
verify(singleAccountPublicClientApplication)
127-
.acquireTokenSilent(argThat(parameters -> parameters.getAuthority().equals(AUTHORITY_URL)));
125+
verify(singleAccountPublicClientApplication).acquireTokenSilent(
126+
argThat(parameters -> parameters.getAuthority().equals(AUTHORITY_URL))
127+
);
128128
}
129129

130130
private void initializePluginCallMockWithDefaults(PluginCall pluginCallMock) throws JSONException {
131131
when(pluginCallMock.getArray("scopes")).thenReturn(new JSArray(new String[] { "mocked-scope" }));
132-
when(pluginCallMock.getString(any()))
133-
.thenAnswer(
134-
(Answer<String>) invocation -> {
135-
switch (invocation.getArgument(0).toString()) {
136-
case "clientId":
137-
return CLIENT_ID;
138-
case "domainHint":
139-
return DOMAIN_HINT;
140-
case "tenant":
141-
return TENANT;
142-
case "keyHash":
143-
return KEY_HASH;
144-
case "authorityUrl":
145-
return AUTHORITY_URL;
146-
}
147-
148-
return null;
132+
when(pluginCallMock.getString(any())).thenAnswer(
133+
(Answer<String>) invocation -> {
134+
switch (invocation.getArgument(0).toString()) {
135+
case "clientId":
136+
return CLIENT_ID;
137+
case "domainHint":
138+
return DOMAIN_HINT;
139+
case "tenant":
140+
return TENANT;
141+
case "keyHash":
142+
return KEY_HASH;
143+
case "authorityUrl":
144+
return AUTHORITY_URL;
149145
}
150-
);
146+
147+
return null;
148+
}
149+
);
151150
when(pluginCallMock.getString("authorityType", AuthorityType.AAD.name())).thenReturn("AAD");
152151
}
153152

0 commit comments

Comments
 (0)