1818class Endpoint {
1919
2020 private final PrivacyIDEA privacyIDEA ;
21- private String authToken ; // lazy init
2221 private List <String > logExcludedEndpointPrints = Collections .emptyList (); //Arrays.asList(org.privacyidea.Constants.ENDPOINT_AUTH, org.privacyidea.Constants.ENDPOINT_POLL_TRANSACTION);
23- private boolean doSSLVerify = true ;
24- private final String hostname ;
25- private final String serviceAccountName ;
26- private final String serviceAccountPass ;
27-
28- Endpoint (PrivacyIDEA privacyIDEA , String hostname , boolean doSSLVerify , String serviceAccountName , String serviceAccountPass ) {
29- this .hostname = hostname ;
30- this .doSSLVerify = doSSLVerify ;
31- this .serviceAccountName = serviceAccountName ;
32- this .serviceAccountPass = serviceAccountPass ;
22+ private final Configuration configuration ;
23+
24+ Endpoint (PrivacyIDEA privacyIDEA , Configuration configuration ) {
3325 this .privacyIDEA = privacyIDEA ;
26+ this .configuration = configuration ;
3427 }
3528
3629 /**
3730 * Make a https call to the specified path, the URL is taken from the config.
38- * If SSL Verification is turned off in the config, the endpoints certificate will not be verified.
31+ * If SSL verification is set to false in the config, the endpoints certificate will not be verified.
3932 *
40- * @param path Path to the API endpoint
41- * @param params All necessary parameters for request
33+ * @param path path to the API endpoint
34+ * @param params all necessary parameters for the request
4235 * @param authTokenRequired whether the authorization header should be set
4336 * @param method "POST" or "GET"
4437 * @return String containing the whole response
@@ -67,7 +60,7 @@ String sendRequest(String path, Map<String, String> params, boolean authTokenReq
6760 HttpURLConnection con = null ;
6861 String response = null ;
6962 try {
70- String strURL = hostname + path ;
63+ String strURL = configuration . serverURL + path ;
7164
7265 if (method .equals ("GET" )) {
7366 strURL += "?" + paramsSB .toString ();
@@ -80,23 +73,25 @@ String sendRequest(String path, Map<String, String> params, boolean authTokenReq
8073 con = (HttpURLConnection ) (url .openConnection ());
8174 }
8275
83- if (!doSSLVerify && (con instanceof HttpsURLConnection )) {
76+ if (!configuration . doSSLVerify && (con instanceof HttpsURLConnection )) {
8477 con = disableSSLVerification ((HttpsURLConnection ) con );
8578 }
8679
8780 if (method .equals ("POST" )) {
8881 con .setDoOutput (true );
8982 }
83+
9084 con .setRequestMethod (method );
85+ con .addRequestProperty ("User-Agent" , configuration .userAgent );
9186
92- if (authToken == null && authTokenRequired ) {
93- getAuthTokenFromServer ();
94- }
87+ if (authTokenRequired ) {
88+ String authToken = getAuthTokenFromServer ();
89+ if (authToken .isEmpty ()) {
90+ privacyIDEA .log ("Failed to fetch authorization token from server!" );
91+ return "" ;
92+ }
9593
96- if (authToken != null && authTokenRequired ) {
9794 con .setRequestProperty ("Authorization" , authToken );
98- } else if (authTokenRequired ) {
99- throw new IllegalStateException ("Authorization token could not be acquired, but it is needed!" );
10095 }
10196
10297 con .connect ();
@@ -132,10 +127,10 @@ String sendRequest(String path, Map<String, String> params, boolean authTokenReq
132127 response = br .lines ().reduce ("" , (a , s ) -> a += s );
133128 }
134129 }
135- privacyIDEA .log ("Reponse from error : " + response );
130+ privacyIDEA .log ("Response from ErrorStream : " + response );
136131 }
137132 } catch (IOException ioe ) {
138- privacyIDEA .log ("Exception while getting ErrorStream: " + e .getMessage ());
133+ privacyIDEA .log ("Exception getting ErrorStream: " + ioe .getMessage ());
139134 }
140135
141136 }
@@ -178,35 +173,31 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
178173 return con ;
179174 }
180175
181- private void getAuthTokenFromServer () {
182- if (authToken != null ) {
183- // The TTL of the AuthToken should be long enough for the usage (default is 60min)
184- //log.info("Auth token already set.");
185- return ;
186- }
187-
176+ String getAuthTokenFromServer () {
188177 if (!privacyIDEA .checkServiceAccountAvailable ()) {
189178 privacyIDEA .log ("Service account information not set, cannot retrieve auth token" );
190- return ;
179+ return "" ;
191180 }
192181
193- //log.info("Getting auth token from PI");
194182 Map <String , String > params = new LinkedHashMap <>();
195- params .put (Constants .PARAM_KEY_USERNAME , serviceAccountName );
196- params .put (Constants .PARAM_KEY_PASSWORD , serviceAccountPass );
183+ params .put (Constants .PARAM_KEY_USERNAME , configuration .serviceAccountName );
184+ params .put (Constants .PARAM_KEY_PASSWORD , configuration .serviceAccountPass );
185+
186+ if (configuration .serviceAccountRealm != null && !configuration .serviceAccountRealm .isEmpty ()) {
187+ params .put (Constants .PARAM_KEY_REALM , configuration .serviceAccountRealm );
188+ } else if (configuration .realm != null && !configuration .realm .isEmpty ()) {
189+ params .put (Constants .PARAM_KEY_REALM , configuration .realm );
190+ }
191+
197192 String response = sendRequest (Constants .ENDPOINT_AUTH , params , false , Constants .POST );
198193
199194 JsonObject obj = JsonParser .parseString (response ).getAsJsonObject ();
200195 if (obj != null ) {
201- authToken = obj .getAsJsonObject ("result" ).getAsJsonObject ("value" ).getAsJsonPrimitive ("token" ).getAsString ();
202- }
203- }
204-
205- String getAuthToken () {
206- if (authToken == null ) {
207- getAuthTokenFromServer ();
196+ return obj .getAsJsonObject ("result" ).getAsJsonObject ("value" ).getAsJsonPrimitive ("token" ).getAsString ();
197+ } else {
198+ privacyIDEA .log ("Response did not contain an authorization token: " + response );
199+ return "" ;
208200 }
209- return authToken ;
210201 }
211202
212203 public static String prettyPrintJson (String json ) {
@@ -221,7 +212,6 @@ public static String prettyPrintJson(String json) {
221212 return json ;
222213 }
223214
224- //return sw.toString();
225215 return gson .toJson (obj );
226216 }
227217
0 commit comments