Skip to content

Commit 7840bc6

Browse files
davidcoutadeurrouazana
authored andcommitted
create new parameter for graph api scope (make graphAPI endpoints customizable #3)
1 parent 640a4bf commit 7840bc6

9 files changed

Lines changed: 47 additions & 16 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ The values to configure are:
2828

2929
##### Connection
3030

31+
- `connections.pluginConnection.msGraphApiConnectionSettings.authenticationURL`: The base URL used for authentication (default is https://login.microsoftonline.com/)
32+
- `connections.pluginConnection.msGraphApiConnectionSettings.usersURL`: The base URL used for operations on users (default is https://graph.microsoft.com)
33+
- `connections.pluginConnection.msGraphApiConnectionSettings.scope`: The scope url used during authentication (default is https://graph.microsoft.com/.default)
3134
- `connections.pluginConnection.msGraphApiConnectionSettings.clientId`: The client id for the application
3235
- `connections.pluginConnection.msGraphApiConnectionSettings.clientSecret`: The client secret used to connect to the application
3336
- `connections.pluginConnection.msGraphApiConnectionSettings.tenant`: The Azure AD tenant

sample/msgraphapi-to-ldap-advanced/lsc.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<msgraphapi:msGraphApiConnectionSettings>
2525
<msgraphapi:authenticationURL>${MS_GRAPH_API_AUTHENTICATION_URL}</msgraphapi:authenticationURL>
2626
<msgraphapi:usersURL>${MS_GRAPH_API_USERS_URL}</msgraphapi:usersURL>
27+
<msgraphapi:scope>${MS_GRAPH_API_SCOPE}</msgraphapi:scope>
2728
<msgraphapi:clientId>${MS_GRAPH_API_CLIENT_ID}</msgraphapi:clientId>
2829
<msgraphapi:clientSecret>${MS_GRAPH_API_CLIENT_SECRET}</msgraphapi:clientSecret>
2930
<msgraphapi:tenant>${MS_GRAPH_API_TENANT}</msgraphapi:tenant>

sample/msgraphapi-to-ldap/lsc.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<msgraphapi:msGraphApiConnectionSettings>
2525
<msgraphapi:authenticationURL>${MS_GRAPH_API_AUTHENTICATION_URL}</msgraphapi:authenticationURL>
2626
<msgraphapi:usersURL>${MS_GRAPH_API_USERS_URL}</msgraphapi:usersURL>
27+
<msgraphapi:scope>${MS_GRAPH_API_SCOPE}</msgraphapi:scope>
2728
<msgraphapi:clientId>${MS_GRAPH_API_CLIENT_ID}</msgraphapi:clientId>
2829
<msgraphapi:clientSecret>${MS_GRAPH_API_CLIENT_SECRET}</msgraphapi:clientSecret>
2930
<msgraphapi:tenant>${MS_GRAPH_API_TENANT}</msgraphapi:tenant>

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,18 @@
5454

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

61-
public AuthenticationResponse authenticate(String tenant, String authenticationURL, String usersURL, String clientId, String clientSecret) throws AuthorizationException {
62-
60+
public AuthenticationResponse authenticate(String tenant, String authenticationURL, String scope, String clientId, String clientSecret) throws AuthorizationException {
6361
if( authenticationURL == null || authenticationURL.isEmpty() )
6462
{
6563
authenticationURL = DEFAULT_AUTHENTICATION_URL;
6664
}
67-
68-
String scope;
69-
if( usersURL == null || usersURL.isEmpty() )
65+
if( scope == null || scope.isEmpty() )
7066
{
71-
usersURL = DEFAULT_USERS_URL;
67+
scope = GRAPH_DEFAULT_SCOPE;
7268
}
73-
scope = usersURL.replaceAll("/$", "") + GRAPH_DEFAULT_SCOPE;
74-
7569
WebTarget authTarget = ClientBuilder.newClient()
7670
.register(JacksonFeature.class)
7771
.target(authenticationURL)

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.getUsersURL(), settings.getClientId(), settings.getClientSecret())
104+
.authenticate(settings.getTenant(), settings.getAuthenticationURL(), settings.getScope(), settings.getClientId(), settings.getClientSecret())
105105
.getAccessToken();
106106

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

src/main/java/org/lsc/plugins/connectors/msgraphapi/generated/MsGraphApiConnectionSettings.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* &lt;sequence>
2828
* &lt;element name="authenticationURL" type="{http://www.w3.org/2001/XMLSchema}string"/>
2929
* &lt;element name="usersURL" type="{http://www.w3.org/2001/XMLSchema}string"/>
30+
* &lt;element name="scope" type="{http://www.w3.org/2001/XMLSchema}string"/>
3031
* &lt;element name="clientId" type="{http://www.w3.org/2001/XMLSchema}string"/>
3132
* &lt;element name="clientSecret" type="{http://www.w3.org/2001/XMLSchema}string"/>
3233
* &lt;element name="tenant" type="{http://www.w3.org/2001/XMLSchema}string"/>
@@ -42,6 +43,7 @@
4243
@XmlType(name = "", propOrder = {
4344
"authenticationURL",
4445
"usersURL",
46+
"scope",
4547
"clientId",
4648
"clientSecret",
4749
"tenant"
@@ -54,6 +56,8 @@ public class MsGraphApiConnectionSettings {
5456
@XmlElement(namespace = "http://lsc-project.org/XSD/lsc-microsoft-graph-api-plugin-1.0.xsd", required = true)
5557
protected String usersURL;
5658
@XmlElement(namespace = "http://lsc-project.org/XSD/lsc-microsoft-graph-api-plugin-1.0.xsd", required = true)
59+
protected String scope;
60+
@XmlElement(namespace = "http://lsc-project.org/XSD/lsc-microsoft-graph-api-plugin-1.0.xsd", required = true)
5761
protected String clientId;
5862
@XmlElement(namespace = "http://lsc-project.org/XSD/lsc-microsoft-graph-api-plugin-1.0.xsd", required = true)
5963
protected String clientSecret;
@@ -108,6 +112,30 @@ public void setUsersURL(String value) {
108112
this.usersURL = value;
109113
}
110114

115+
/**
116+
* Gets the value of the scope property.
117+
*
118+
* @return
119+
* possible object is
120+
* {@link String }
121+
*
122+
*/
123+
public String getScope() {
124+
return scope;
125+
}
126+
127+
/**
128+
* Sets the value of the scope property.
129+
*
130+
* @param value
131+
* allowed object is
132+
* {@link String }
133+
*
134+
*/
135+
public void setScope(String value) {
136+
this.scope = value;
137+
}
138+
111139
/**
112140
* Gets the value of the clientId property.
113141
*

src/main/resources/schemas/lsc-microsoft-graph-api-plugin-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<xsd:sequence>
1212
<xsd:element name="authenticationURL" type="xsd:string" />
1313
<xsd:element name="usersURL" type="xsd:string" />
14+
<xsd:element name="scope" type="xsd:string" />
1415
<xsd:element name="clientId" type="xsd:string" />
1516
<xsd:element name="clientSecret" type="xsd:string" />
1617
<xsd:element name="tenant" type="xsd:string" />

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +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");
63+
private final static String SCOPE = System.getenv("TEST_MS_GRAPH_API_SCOPE");
6464

6565
private final MsGraphApiAuthentication msGraphApiAuthentication;
6666

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

7878
@Test
7979
void shouldObtainValidAccessToken() throws AuthorizationException {
80-
AuthenticationResponse response = msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, USERS_URL, CLIENT_ID, CLIENT_SECRET);
80+
AuthenticationResponse response = msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, SCOPE, CLIENT_ID, CLIENT_SECRET);
8181
assertThat(response.getAccessToken()).isNotBlank();
8282
assertThatCode(() -> JWT.decode(response.getAccessToken())).doesNotThrowAnyException();
8383
}
8484

8585
@Test
8686
void shouldThrowIfInvalidTenant() {
87-
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate("NOT_A_TENANT", AUTHENTICATION_URL, USERS_URL, CLIENT_ID, CLIENT_SECRET)).isInstanceOf(AuthorizationException.class);
87+
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate("NOT_A_TENANT", AUTHENTICATION_URL, SCOPE, CLIENT_ID, CLIENT_SECRET)).isInstanceOf(AuthorizationException.class);
8888
}
8989

9090
@Test
9191
void shouldThrowIfInvalidClientId() {
92-
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, USERS_URL, "NOT_A_CLIENT_ID", CLIENT_SECRET)).isInstanceOf(AuthorizationException.class);
92+
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, SCOPE, "NOT_A_CLIENT_ID", CLIENT_SECRET)).isInstanceOf(AuthorizationException.class);
9393
}
9494

9595
@Test
9696
void shouldThrowIfInvalidClientSecret() {
97-
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, USERS_URL, CLIENT_ID, "NOT_A_SECRET")).isInstanceOf(AuthorizationException.class);
97+
assertThatThrownBy(() -> msGraphApiAuthentication.authenticate(TENANT, AUTHENTICATION_URL, SCOPE, CLIENT_ID, "NOT_A_SECRET")).isInstanceOf(AuthorizationException.class);
9898
}
9999

100100
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ static void setup() throws AuthorizationException {
8888
String tenant = System.getenv("TEST_MS_GRAPH_API_TENANT");
8989
String authenticationURL = System.getenv("TEST_MS_GRAPH_API_AUTHENTICATION_URL");
9090
String usersURL = System.getenv("TEST_MS_GRAPH_API_USERS_URL");
91+
String scope = System.getenv("TEST_MS_GRAPH_API_SCOPE");
9192

9293
assumeTrue(StringUtils.isNotBlank(authenticationURL));
9394
assumeTrue(StringUtils.isNotBlank(usersURL));
95+
assumeTrue(StringUtils.isNotBlank(scope));
9496
assumeTrue(StringUtils.isNotBlank(clientId));
9597
assumeTrue(StringUtils.isNotBlank(clientSecret));
9698
assumeTrue(StringUtils.isNotBlank(tenant));
@@ -108,6 +110,7 @@ static void setup() throws AuthorizationException {
108110
when(connectionSettings.getClientSecret()).thenReturn(clientSecret);
109111
when(connectionSettings.getTenant()).thenReturn(tenant);
110112
when(connectionSettings.getAuthenticationURL()).thenReturn(authenticationURL);
113+
when(connectionSettings.getScope()).thenReturn(scope);
111114
when(connectionSettings.getUsersURL()).thenReturn(usersURL);
112115
when(task.getBean()).thenReturn("org.lsc.beans.SimpleBean");
113116
when(task.getPluginSourceService()).thenReturn(pluginSourceService);

0 commit comments

Comments
 (0)