44package labapi ;
55
66import com .google .gson .Gson ;
7+ import com .microsoft .aad .msal4j .*;
78
9+ import java .net .MalformedURLException ;
10+ import java .util .Collections ;
811import java .util .HashMap ;
912import java .util .Map ;
13+ import java .util .concurrent .ExecutionException ;
1014
1115public class LabService {
1216
17+ static ConfidentialClientApplication labApp ;
18+
19+ static void initLabApp () throws MalformedURLException {
20+ KeyVaultSecretsProvider keyVaultSecretsProvider = new KeyVaultSecretsProvider ();
21+
22+ String appID = keyVaultSecretsProvider .getSecret (LabConstants .APP_ID_KEY_VAULT_SECRET );
23+ String appSecret = keyVaultSecretsProvider .getSecret (LabConstants .APP_PASSWORD_KEY_VAULT_SECRET );
24+
25+ labApp = ConfidentialClientApplication .builder (
26+ appID , ClientCredentialFactory .createFromSecret (appSecret )).
27+ authority (TestConstants .MICROSOFT_AUTHORITY ).
28+ build ();
29+ }
30+
31+ static String getLabAccessToken () throws MalformedURLException , ExecutionException , InterruptedException {
32+ if (labApp == null ){
33+ initLabApp ();
34+ }
35+ return labApp .acquireToken (ClientCredentialParameters
36+ .builder (Collections .singleton (TestConstants .MSIDLAB_DEFAULT_SCOPE ))
37+ .build ()).
38+ get ().accessToken ();
39+ }
40+
1341 User getUser (UserQueryParameters query ){
1442 try {
1543 Map <String , String > queryMap = query .parameters ;
16- String result = HttpClientHelper .sendRequestToLab (LabConstants .LAB_USER_ENDPOINT , queryMap );
44+ String result = HttpClientHelper .sendRequestToLab (
45+ LabConstants .LAB_USER_ENDPOINT , queryMap , getLabAccessToken ());
1746
1847 User [] users = new Gson ().fromJson (result , User [].class );
1948 User user = users [0 ];
@@ -31,18 +60,20 @@ User getUser(UserQueryParameters query){
3160
3261 public static App getApp (String appId ){
3362 try {
34- String result = HttpClientHelper .sendRequestToLab (LabConstants .LAB_APP_ENDPOINT , appId );
63+ String result = HttpClientHelper .sendRequestToLab (
64+ LabConstants .LAB_APP_ENDPOINT , appId , getLabAccessToken ());
3565 App [] apps = new Gson ().fromJson (result , App [].class );
3666 return apps [0 ];
3767 } catch (Exception ex ) {
3868 throw new RuntimeException ("Error getting app from lab: " + ex .getMessage ());
3969 }
4070 }
4171
42- public static Lab getLab (String labId ){
72+ public static Lab getLab (String labId ) {
4373 String result ;
4474 try {
45- result = HttpClientHelper .sendRequestToLab (LabConstants .LAB_LAB_ENDPOINT , labId );
75+ result = HttpClientHelper .sendRequestToLab (
76+ LabConstants .LAB_LAB_ENDPOINT , labId , getLabAccessToken ());
4677 Lab [] labs = new Gson ().fromJson (result , Lab [].class );
4778 return labs [0 ];
4879 } catch (Exception ex ) {
@@ -55,7 +86,8 @@ private String getUserSecret(String labName){
5586 try {
5687 Map <String , String > queryMap = new HashMap <>();
5788 queryMap .put ("secret" , labName );
58- result = HttpClientHelper .sendRequestToLab (LabConstants .LAB_USER_SECRET_ENDPOINT , queryMap );
89+ result = HttpClientHelper .sendRequestToLab (
90+ LabConstants .LAB_USER_SECRET_ENDPOINT , queryMap , getLabAccessToken ());
5991
6092 return new Gson ().fromJson (result , UserSecret .class ).value ;
6193 } catch (Exception ex ) {
0 commit comments