18
18
class Endpoint {
19
19
20
20
private final PrivacyIDEA privacyIDEA ;
21
- private String authToken ; // lazy init
22
21
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 ) {
33
25
this .privacyIDEA = privacyIDEA ;
26
+ this .configuration = configuration ;
34
27
}
35
28
36
29
/**
37
30
* 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.
39
32
*
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
42
35
* @param authTokenRequired whether the authorization header should be set
43
36
* @param method "POST" or "GET"
44
37
* @return String containing the whole response
@@ -67,7 +60,7 @@ String sendRequest(String path, Map<String, String> params, boolean authTokenReq
67
60
HttpURLConnection con = null ;
68
61
String response = null ;
69
62
try {
70
- String strURL = hostname + path ;
63
+ String strURL = configuration . serverURL + path ;
71
64
72
65
if (method .equals ("GET" )) {
73
66
strURL += "?" + paramsSB .toString ();
@@ -80,23 +73,25 @@ String sendRequest(String path, Map<String, String> params, boolean authTokenReq
80
73
con = (HttpURLConnection ) (url .openConnection ());
81
74
}
82
75
83
- if (!doSSLVerify && (con instanceof HttpsURLConnection )) {
76
+ if (!configuration . doSSLVerify && (con instanceof HttpsURLConnection )) {
84
77
con = disableSSLVerification ((HttpsURLConnection ) con );
85
78
}
86
79
87
80
if (method .equals ("POST" )) {
88
81
con .setDoOutput (true );
89
82
}
83
+
90
84
con .setRequestMethod (method );
85
+ con .addRequestProperty ("User-Agent" , configuration .userAgent );
91
86
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
+ }
95
93
96
- if (authToken != null && authTokenRequired ) {
97
94
con .setRequestProperty ("Authorization" , authToken );
98
- } else if (authTokenRequired ) {
99
- throw new IllegalStateException ("Authorization token could not be acquired, but it is needed!" );
100
95
}
101
96
102
97
con .connect ();
@@ -132,10 +127,10 @@ String sendRequest(String path, Map<String, String> params, boolean authTokenReq
132
127
response = br .lines ().reduce ("" , (a , s ) -> a += s );
133
128
}
134
129
}
135
- privacyIDEA .log ("Reponse from error : " + response );
130
+ privacyIDEA .log ("Response from ErrorStream : " + response );
136
131
}
137
132
} catch (IOException ioe ) {
138
- privacyIDEA .log ("Exception while getting ErrorStream: " + e .getMessage ());
133
+ privacyIDEA .log ("Exception getting ErrorStream: " + ioe .getMessage ());
139
134
}
140
135
141
136
}
@@ -178,35 +173,31 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
178
173
return con ;
179
174
}
180
175
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 () {
188
177
if (!privacyIDEA .checkServiceAccountAvailable ()) {
189
178
privacyIDEA .log ("Service account information not set, cannot retrieve auth token" );
190
- return ;
179
+ return "" ;
191
180
}
192
181
193
- //log.info("Getting auth token from PI");
194
182
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
+
197
192
String response = sendRequest (Constants .ENDPOINT_AUTH , params , false , Constants .POST );
198
193
199
194
JsonObject obj = JsonParser .parseString (response ).getAsJsonObject ();
200
195
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 "" ;
208
200
}
209
- return authToken ;
210
201
}
211
202
212
203
public static String prettyPrintJson (String json ) {
@@ -221,7 +212,6 @@ public static String prettyPrintJson(String json) {
221
212
return json ;
222
213
}
223
214
224
- //return sw.toString();
225
215
return gson .toJson (obj );
226
216
}
227
217
0 commit comments