Skip to content

Commit 640a4bf

Browse files
davidcoutadeurrouazana
authored andcommitted
fix graph api scope (make graphAPI endpoints customizable #3)
1 parent effb2f1 commit 640a4bf

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/main/java/org/lsc/plugins/connectors/msgraphapi/MsGraphApiAuthentication.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,31 @@
5454

5555
public class MsGraphApiAuthentication {
5656
private static final String DEFAULT_AUTHENTICATION_URL = "https://login.microsoftonline.com/";
57-
private static final String GRAPH_DEFAULT_SCOPE = "https://graph.microsoft.com/.default";
57+
private static final String DEFAULT_USERS_URL = "https://graph.microsoft.com";
58+
private static final String GRAPH_DEFAULT_SCOPE = "/.default";
5859
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
5960

60-
public AuthenticationResponse authenticate(String tenant, String authenticationURL, String clientId, String clientSecret) throws AuthorizationException {
61+
public AuthenticationResponse authenticate(String tenant, String authenticationURL, String usersURL, String clientId, String clientSecret) throws AuthorizationException {
62+
6163
if( authenticationURL == null || authenticationURL.isEmpty() )
6264
{
6365
authenticationURL = DEFAULT_AUTHENTICATION_URL;
6466
}
67+
68+
String scope;
69+
if( usersURL == null || usersURL.isEmpty() )
70+
{
71+
usersURL = DEFAULT_USERS_URL;
72+
}
73+
scope = usersURL.replaceAll("/$", "") + GRAPH_DEFAULT_SCOPE;
74+
6575
WebTarget authTarget = ClientBuilder.newClient()
6676
.register(JacksonFeature.class)
6777
.target(authenticationURL)
6878
.path(tenant)
6979
.path("oauth2/v2.0/token");
7080
Form authForm = new Form("client_id", clientId)
71-
.param("scope", GRAPH_DEFAULT_SCOPE)
81+
.param("scope", scope)
7282
.param("client_secret", clientSecret)
7383
.param("grant_type", "client_credentials");
7484

src/main/java/org/lsc/plugins/connectors/msgraphapi/MsGraphApiUsersSrcService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public MsGraphApiUsersSrcService(TaskType task) throws LscServiceConfigurationEx
101101
settings = (MsGraphApiConnectionSettings) pluginConnectionType.getAny().get(0);
102102

103103
String token = new MsGraphApiAuthentication()
104-
.authenticate(settings.getTenant(), settings.getAuthenticationURL(), settings.getClientId(), settings.getClientSecret())
104+
.authenticate(settings.getTenant(), settings.getAuthenticationURL(), settings.getUsersURL(), settings.getClientId(), settings.getClientSecret())
105105
.getAccessToken();
106106

107107
dao = new MsGraphApiDao(token, settings, service);

src/test/java/org/lsc/plugins/connectors/msgraphapi/MsGraphApiAuthenticationTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class MsGraphApiAuthenticationTest {
6060
private final static String CLIENT_SECRET = System.getenv("TEST_MS_GRAPH_API_CLIENT_SECRET");
6161
private final static String TENANT = System.getenv("TEST_MS_GRAPH_API_TENANT");
6262
private final static String AUTHENTICATION_URL = System.getenv("TEST_MS_GRAPH_API_AUTHENTICATION_URL");
63+
private final static String USERS_URL = System.getenv("TEST_MS_GRAPH_API_USERS_URL");
6364

6465
private final MsGraphApiAuthentication msGraphApiAuthentication;
6566

@@ -76,24 +77,24 @@ static void setup() {
7677

7778
@Test
7879
void shouldObtainValidAccessToken() throws AuthorizationException {
79-
AuthenticationResponse response = msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, CLIENT_ID, CLIENT_SECRET);
80+
AuthenticationResponse response = msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, USERS_URL, CLIENT_ID, CLIENT_SECRET);
8081
assertThat(response.getAccessToken()).isNotBlank();
8182
assertThatCode(() -> JWT.decode(response.getAccessToken())).doesNotThrowAnyException();
8283
}
8384

8485
@Test
8586
void shouldThrowIfInvalidTenant() {
86-
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate("NOT_A_TENANT", AUTHENTICATION_URL, CLIENT_ID, CLIENT_SECRET)).isInstanceOf(AuthorizationException.class);
87+
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate("NOT_A_TENANT", AUTHENTICATION_URL, USERS_URL, CLIENT_ID, CLIENT_SECRET)).isInstanceOf(AuthorizationException.class);
8788
}
8889

8990
@Test
9091
void shouldThrowIfInvalidClientId() {
91-
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, "NOT_A_CLIENT_ID", CLIENT_SECRET)).isInstanceOf(AuthorizationException.class);
92+
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, USERS_URL, "NOT_A_CLIENT_ID", CLIENT_SECRET)).isInstanceOf(AuthorizationException.class);
9293
}
9394

9495
@Test
9596
void shouldThrowIfInvalidClientSecret() {
96-
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, CLIENT_ID, "NOT_A_SECRET")).isInstanceOf(AuthorizationException.class);
97+
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, USERS_URL, CLIENT_ID, "NOT_A_SECRET")).isInstanceOf(AuthorizationException.class);
9798
}
9899

99100
}

0 commit comments

Comments
 (0)