Skip to content

Commit effb2f1

Browse files
davidcoutadeurrouazana
authored andcommitted
make graphAPI endpoints customizable #3 (not tested!)
1 parent 2fda6b6 commit effb2f1

9 files changed

Lines changed: 88 additions & 10 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<username></username>
2323
<password></password>
2424
<msgraphapi:msGraphApiConnectionSettings>
25+
<msgraphapi:authenticationURL>${MS_GRAPH_API_AUTHENTICATION_URL}</msgraphapi:authenticationURL>
26+
<msgraphapi:usersURL>${MS_GRAPH_API_USERS_URL}</msgraphapi:usersURL>
2527
<msgraphapi:clientId>${MS_GRAPH_API_CLIENT_ID}</msgraphapi:clientId>
2628
<msgraphapi:clientSecret>${MS_GRAPH_API_CLIENT_SECRET}</msgraphapi:clientSecret>
2729
<msgraphapi:tenant>${MS_GRAPH_API_TENANT}</msgraphapi:tenant>

sample/msgraphapi-to-ldap/lsc.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<username></username>
2323
<password></password>
2424
<msgraphapi:msGraphApiConnectionSettings>
25+
<msgraphapi:authenticationURL>${MS_GRAPH_API_AUTHENTICATION_URL}</msgraphapi:authenticationURL>
26+
<msgraphapi:usersURL>${MS_GRAPH_API_USERS_URL}</msgraphapi:usersURL>
2527
<msgraphapi:clientId>${MS_GRAPH_API_CLIENT_ID}</msgraphapi:clientId>
2628
<msgraphapi:clientSecret>${MS_GRAPH_API_CLIENT_SECRET}</msgraphapi:clientSecret>
2729
<msgraphapi:tenant>${MS_GRAPH_API_TENANT}</msgraphapi:tenant>

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,18 @@
5353
import org.lsc.plugins.connectors.msgraphapi.beans.AuthenticationResponse;
5454

5555
public class MsGraphApiAuthentication {
56+
private static final String DEFAULT_AUTHENTICATION_URL = "https://login.microsoftonline.com/";
5657
private static final String GRAPH_DEFAULT_SCOPE = "https://graph.microsoft.com/.default";
5758
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
5859

59-
public AuthenticationResponse authenticate(String tenant, String clientId, String clientSecret) throws AuthorizationException {
60+
public AuthenticationResponse authenticate(String tenant, String authenticationURL, String clientId, String clientSecret) throws AuthorizationException {
61+
if( authenticationURL == null || authenticationURL.isEmpty() )
62+
{
63+
authenticationURL = DEFAULT_AUTHENTICATION_URL;
64+
}
6065
WebTarget authTarget = ClientBuilder.newClient()
6166
.register(JacksonFeature.class)
62-
.target("https://login.microsoftonline.com/")
67+
.target(authenticationURL)
6368
.path(tenant)
6469
.path("oauth2/v2.0/token");
6570
Form authForm = new Form("client_id", clientId)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@
6464
import org.lsc.exception.LscServiceException;
6565
import org.lsc.plugins.connectors.msgraphapi.beans.User;
6666
import org.lsc.plugins.connectors.msgraphapi.beans.UsersListResponse;
67+
import org.lsc.plugins.connectors.msgraphapi.generated.MsGraphApiConnectionSettings;
6768
import org.lsc.plugins.connectors.msgraphapi.generated.MsGraphApiUsersService;
6869
import org.slf4j.Logger;
6970
import org.slf4j.LoggerFactory;
7071

7172
public class MsGraphApiDao {
7273
public static final String USER_PATH = "/users";
7374
public static final String DEFAULT_PIVOT = "mail";
75+
public static final String DEFAULT_USERS_URL = "https://graph.microsoft.com";
7476
public static final String ID = "id";
7577
private static final Logger LOGGER = LoggerFactory.getLogger(MsGraphApiDao.class);
7678

@@ -80,20 +82,22 @@ public class MsGraphApiDao {
8082

8183
private final Optional<Integer> pageSize;
8284
private final String pivot;
85+
private final String usersURL;
8386
private final Optional<String> filter;
8487
private final Optional<String> select;
8588

86-
public MsGraphApiDao(String token, MsGraphApiUsersService serviceConfiguration) {
89+
public MsGraphApiDao(String token, MsGraphApiConnectionSettings settings, MsGraphApiUsersService serviceConfiguration) {
8790
authorizationBearer = "Bearer " + token;
8891
this.filter = getStringParameter(serviceConfiguration.getFilter());
8992
this.select = getStringParameter(serviceConfiguration.getSelect());
9093
this.pivot = getStringParameter(serviceConfiguration.getPivot()).orElse(DEFAULT_PIVOT);
94+
this.usersURL = getStringParameter(settings.getUsersURL()).orElse(DEFAULT_USERS_URL);
9195
this.pageSize = Optional.ofNullable(serviceConfiguration.getPageSize()).filter(size -> size > 0);
9296
LOGGER.debug("bearer " + authorizationBearer);
9397
client = ClientBuilder.newClient()
9498
.register(JacksonFeature.class);
9599
usersClient = client
96-
.target("https://graph.microsoft.com")
100+
.target(this.usersURL)
97101
.path("v1.0")
98102
.path(USER_PATH);
99103
}

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

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

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

107-
dao = new MsGraphApiDao(token, service);
107+
dao = new MsGraphApiDao(token, settings, service);
108108

109109
} catch (ClassNotFoundException | AuthorizationException e) {
110110
throw new LscServiceConfigurationException(e);

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* &lt;complexContent>
2626
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
2727
* &lt;sequence>
28+
* &lt;element name="authenticationURL" type="{http://www.w3.org/2001/XMLSchema}string"/>
29+
* &lt;element name="usersURL" type="{http://www.w3.org/2001/XMLSchema}string"/>
2830
* &lt;element name="clientId" type="{http://www.w3.org/2001/XMLSchema}string"/>
2931
* &lt;element name="clientSecret" type="{http://www.w3.org/2001/XMLSchema}string"/>
3032
* &lt;element name="tenant" type="{http://www.w3.org/2001/XMLSchema}string"/>
@@ -38,20 +40,74 @@
3840
*/
3941
@XmlAccessorType(XmlAccessType.FIELD)
4042
@XmlType(name = "", propOrder = {
43+
"authenticationURL",
44+
"usersURL",
4145
"clientId",
4246
"clientSecret",
4347
"tenant"
4448
})
4549
@XmlRootElement(name = "msGraphApiConnectionSettings", namespace = "http://lsc-project.org/XSD/lsc-microsoft-graph-api-plugin-1.0.xsd")
4650
public class MsGraphApiConnectionSettings {
4751

52+
@XmlElement(namespace = "http://lsc-project.org/XSD/lsc-microsoft-graph-api-plugin-1.0.xsd", required = true)
53+
protected String authenticationURL;
54+
@XmlElement(namespace = "http://lsc-project.org/XSD/lsc-microsoft-graph-api-plugin-1.0.xsd", required = true)
55+
protected String usersURL;
4856
@XmlElement(namespace = "http://lsc-project.org/XSD/lsc-microsoft-graph-api-plugin-1.0.xsd", required = true)
4957
protected String clientId;
5058
@XmlElement(namespace = "http://lsc-project.org/XSD/lsc-microsoft-graph-api-plugin-1.0.xsd", required = true)
5159
protected String clientSecret;
5260
@XmlElement(namespace = "http://lsc-project.org/XSD/lsc-microsoft-graph-api-plugin-1.0.xsd", required = true)
5361
protected String tenant;
5462

63+
/**
64+
* Gets the value of the authenticationURL property.
65+
*
66+
* @return
67+
* possible object is
68+
* {@link String }
69+
*
70+
*/
71+
public String getAuthenticationURL() {
72+
return authenticationURL;
73+
}
74+
75+
/**
76+
* Sets the value of the authenticationURL property.
77+
*
78+
* @param value
79+
* allowed object is
80+
* {@link String }
81+
*
82+
*/
83+
public void setAuthenticationURL(String value) {
84+
this.authenticationURL = value;
85+
}
86+
87+
/**
88+
* Gets the value of the usersURL property.
89+
*
90+
* @return
91+
* possible object is
92+
* {@link String }
93+
*
94+
*/
95+
public String getUsersURL() {
96+
return usersURL;
97+
}
98+
99+
/**
100+
* Sets the value of the usersURL property.
101+
*
102+
* @param value
103+
* allowed object is
104+
* {@link String }
105+
*
106+
*/
107+
public void setUsersURL(String value) {
108+
this.usersURL = value;
109+
}
110+
55111
/**
56112
* Gets the value of the clientId property.
57113
*

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<xsd:element name="msGraphApiConnectionSettings">
1010
<xsd:complexType>
1111
<xsd:sequence>
12+
<xsd:element name="authenticationURL" type="xsd:string" />
13+
<xsd:element name="usersURL" type="xsd:string" />
1214
<xsd:element name="clientId" type="xsd:string" />
1315
<xsd:element name="clientSecret" type="xsd:string" />
1416
<xsd:element name="tenant" type="xsd:string" />

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class MsGraphApiAuthenticationTest {
5959
private final static String CLIENT_ID = System.getenv("TEST_MS_GRAPH_API_CLIENT_ID");
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");
62+
private final static String AUTHENTICATION_URL = System.getenv("TEST_MS_GRAPH_API_AUTHENTICATION_URL");
6263

6364
private final MsGraphApiAuthentication msGraphApiAuthentication;
6465

@@ -75,24 +76,24 @@ static void setup() {
7576

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

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

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

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

9899
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ static void setup() throws AuthorizationException {
8686
String clientId = System.getenv("TEST_MS_GRAPH_API_CLIENT_ID");
8787
String clientSecret = System.getenv("TEST_MS_GRAPH_API_CLIENT_SECRET");
8888
String tenant = System.getenv("TEST_MS_GRAPH_API_TENANT");
89+
String authenticationURL = System.getenv("TEST_MS_GRAPH_API_AUTHENTICATION_URL");
90+
String usersURL = System.getenv("TEST_MS_GRAPH_API_USERS_URL");
8991

92+
assumeTrue(StringUtils.isNotBlank(authenticationURL));
93+
assumeTrue(StringUtils.isNotBlank(usersURL));
9094
assumeTrue(StringUtils.isNotBlank(clientId));
9195
assumeTrue(StringUtils.isNotBlank(clientSecret));
9296
assumeTrue(StringUtils.isNotBlank(tenant));
@@ -103,6 +107,8 @@ static void setup() throws AuthorizationException {
103107
when(connectionSettings.getClientId()).thenReturn(clientId);
104108
when(connectionSettings.getClientSecret()).thenReturn(clientSecret);
105109
when(connectionSettings.getTenant()).thenReturn(tenant);
110+
when(connectionSettings.getAuthenticationURL()).thenReturn(authenticationURL);
111+
when(connectionSettings.getUsersURL()).thenReturn(usersURL);
106112
when(task.getBean()).thenReturn("org.lsc.beans.SimpleBean");
107113
when(task.getPluginSourceService()).thenReturn(pluginSourceService);
108114
}

0 commit comments

Comments
 (0)