-
Notifications
You must be signed in to change notification settings - Fork 17
/
AuthenticationRequestBuilder.java
384 lines (352 loc) · 16 KB
/
AuthenticationRequestBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package ee.sk.smartid;
/*-
* #%L
* Smart ID sample Java client
* %%
* Copyright (C) 2018 SK ID Solutions AS
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
import ee.sk.smartid.exception.UnprocessableSmartIdResponseException;
import ee.sk.smartid.exception.permanent.ServerMaintenanceException;
import ee.sk.smartid.exception.useraccount.DocumentUnusableException;
import ee.sk.smartid.exception.useraccount.UserAccountNotFoundException;
import ee.sk.smartid.exception.useraction.SessionTimeoutException;
import ee.sk.smartid.exception.useraction.UserRefusedException;
import ee.sk.smartid.exception.useraction.UserSelectedWrongVerificationCodeException;
import ee.sk.smartid.rest.SessionStatusPoller;
import ee.sk.smartid.rest.SmartIdConnector;
import ee.sk.smartid.rest.dao.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static ee.sk.smartid.util.StringUtil.isNotEmpty;
/**
* Class for building authentication request and getting the response
* <p>
* Mandatory request parameters:
* <ul>
* <li><b>Host url</b> - can be set on the {@link ee.sk.smartid.SmartIdClient} level</li>
* <li><b>Relying party uuid</b> - can either be set on the client or builder level</li>
* <li><b>Relying party name</b> - can either be set on the client or builder level</li>
* <li>Either <b>Document number</b> or <b>semantics identifier</b> or <b>private company identifier</b></li>
* <li><b>Authentication hash</b></li>
* </ul>
* Optional request parameters:
* <ul>
* <li><b>Certificate level</b></li>
* <li><b>Display text</b></li>
* <li><b>Nonce</b></li>
* </ul>
*/
public class AuthenticationRequestBuilder extends SmartIdRequestBuilder {
private static final Logger logger = LoggerFactory.getLogger(AuthenticationRequestBuilder.class);
/**
* Constructs a new {@code AuthenticationRequestBuilder}
*
* @param connector for requesting authentication initiation
* @param sessionStatusPoller for polling the authentication response
*/
public AuthenticationRequestBuilder(SmartIdConnector connector, SessionStatusPoller sessionStatusPoller) {
super(connector, sessionStatusPoller);
logger.debug("Instantiating authentication request builder");
}
/**
* Sets the request's UUID of the relying party
* <p>
* If not for explicit need, it is recommended to use
* {@link ee.sk.smartid.SmartIdClient#setRelyingPartyUUID(String)}
* instead. In that case when getting the builder from
* {@link ee.sk.smartid.SmartIdClient} it is not required
* to set the UUID every time when building a new request.
*
* @param relyingPartyUUID UUID of the relying party
* @return this builder
*/
public AuthenticationRequestBuilder withRelyingPartyUUID(String relyingPartyUUID) {
this.relyingPartyUUID = relyingPartyUUID;
return this;
}
/**
* Sets the request's name of the relying party
* <p>
* If not for explicit need, it is recommended to use
* {@link ee.sk.smartid.SmartIdClient#setRelyingPartyName(String)}
* instead. In that case when getting the builder from
* {@link ee.sk.smartid.SmartIdClient} it is not required
* to set name every time when building a new request.
*
* @param relyingPartyName name of the relying party
* @return this builder
*/
public AuthenticationRequestBuilder withRelyingPartyName(String relyingPartyName) {
this.relyingPartyName = relyingPartyName;
return this;
}
/**
* Sets the request's document number
* <p>
* Document number is unique for the user's certificate/device
* that is used for the authentication.
*
* @param documentNumber document number of the certificate/device to be authenticated
* @return this builder
*/
public AuthenticationRequestBuilder withDocumentNumber(String documentNumber) {
this.documentNumber = documentNumber;
return this;
}
/**
* Sets the request's personal semantics identifier
* <p>
* Semantics identifier consists of identity type, country code, a hyphen and the identifier.
*
* @param semanticsIdentifier semantics identifier for a person
* @return this builder
*/
public AuthenticationRequestBuilder withSemanticsIdentifierAsString(String semanticsIdentifier) {
this.semanticsIdentifier = new SemanticsIdentifier(semanticsIdentifier);
return this;
}
/**
* Sets the request's personal semantics identifier
* <p>
* Semantics identifier consists of identity type, country code, and the identifier.
*
* @param semanticsIdentifier semantics identifier for a person
* @return this builder
*/
public AuthenticationRequestBuilder withSemanticsIdentifier(SemanticsIdentifier semanticsIdentifier) {
this.semanticsIdentifier = semanticsIdentifier;
return this;
}
/**
* Sets the request's authentication hash
* <p>
* It is the hash that is signed by a person's device
* which is essential for the authentication verification.
* For security reasons the hash should be generated
* randomly for every new request. It is recommended to use:
* {@link ee.sk.smartid.AuthenticationHash#generateRandomHash()}
*
* @param authenticationHash hash used to sign for authentication
* @return this builder
*/
public AuthenticationRequestBuilder withAuthenticationHash(AuthenticationHash authenticationHash) {
this.hashToSign = authenticationHash;
return this;
}
/**
* Sets the request's certificate level
* <p>
* Defines the minimum required level of the certificate.
* Optional. When not set, it defaults to what is configured
* on the server side i.e. "QUALIFIED".
*
* @param certificateLevel the level of the certificate
* @return this builder
*/
public AuthenticationRequestBuilder withCertificateLevel(String certificateLevel) {
this.certificateLevel = certificateLevel;
return this;
}
/**
* Sets the request's nonce
* <p>
* By default the authentication's initiation request
* has idempotent behaviour meaning when the request
* is repeated inside a given time frame with exactly
* the same parameters, session ID of an existing session
* can be returned as a result. When requester wants, it can
* override the idempotent behaviour inside of this time frame
* using an optional "nonce" parameter present for all POST requests.
* <p>
* Normally, this parameter can be omitted.
*
* @param nonce nonce of the request
* @return this builder
*/
public AuthenticationRequestBuilder withNonce(String nonce) {
this.nonce = nonce;
return this;
}
/**
* Specifies capabilities of the user
* <p>
* By default there are no specified capabilities.
* The capabilities need to be specified in case of
* a restricted Smart ID user
* {@link #withCapabilities(String...)}
* @param capabilities are specified capabilities for a restricted Smart ID user
* and is one of [QUALIFIED, ADVANCED]
* @return this builder
*/
public AuthenticationRequestBuilder withCapabilities(Capability... capabilities) {
this.capabilities = Arrays.stream(capabilities).map(Objects::toString).collect(Collectors.toSet());
return this;
}
/**
* Specifies capabilities of the user
* <p>
*
* By default, there are no specified capabilities.
* The capabilities need to be specified in case of
* a restricted Smart ID user
* {@link #withCapabilities(Capability...)}
* @param capabilities are specified capabilities for a restricted Smart ID user
* and is one of ["QUALIFIED", "ADVANCED"]
* @return this builder
*/
public AuthenticationRequestBuilder withCapabilities(String... capabilities) {
this.capabilities = new HashSet<>(Arrays.asList(capabilities));
return this;
}
/**
* @param allowedInteractionsOrder Preferred order of what dialog to present to user. What actually gets displayed depends on user's device and its software version.
* First option from this list that the device is capable of handling is displayed to the user.
* @return this builder
*/
public AuthenticationRequestBuilder withAllowedInteractionsOrder(List<Interaction> allowedInteractionsOrder) {
this.allowedInteractionsOrder = allowedInteractionsOrder;
return this;
}
/**
* Ask to return the IP address of the mobile device where Smart-ID app was running.
* @see <a href="https://github.com/SK-EID/smart-id-documentation#238-mobile-device-ip-sharing">Mobile Device IP sharing</a>
*
* @return this builder
*/
public AuthenticationRequestBuilder withShareMdClientIpAddress(boolean shareMdClientIpAddress) {
this.shareMdClientIpAddress = shareMdClientIpAddress;
return this;
}
/**
* Send the authentication request and get the response
* <p>
* This method uses automatic session status polling internally
* and therefore blocks the current thread until authentication is concluded/interrupted etc.
*
* @throws UserAccountNotFoundException when the user account was not found
* @throws UserRefusedException when the user has refused the session. NB! This exception has subclasses to determine the screen where user pressed cancel.
* @throws UserSelectedWrongVerificationCodeException when user was presented with three control codes and user selected wrong code
* @throws SessionTimeoutException when there was a timeout, i.e. end user did not confirm or refuse the operation within given timeframe
* @throws DocumentUnusableException when for some reason, this relying party request cannot be completed.
* User must either check his/her Smart-ID mobile application or turn to customer support for getting the exact reason.
* @throws ServerMaintenanceException when the server is under maintenance
*
* @return the authentication response
*/
public SmartIdAuthenticationResponse authenticate() throws UserAccountNotFoundException, UserRefusedException,
UserSelectedWrongVerificationCodeException, SessionTimeoutException, DocumentUnusableException, ServerMaintenanceException {
String sessionId = initiateAuthentication();
SessionStatus sessionStatus = getSessionStatusPoller().fetchFinalSessionStatus(sessionId);
return createSmartIdAuthenticationResponse(sessionStatus);
}
/**
* Send the authentication request and get the session Id
*
* @throws UserAccountNotFoundException when the user account was not found
* @throws ServerMaintenanceException when the server is under maintenance
*
* @return session Id - later to be used for manual session status polling
*/
public String initiateAuthentication() throws UserAccountNotFoundException, ServerMaintenanceException {
validateParameters();
AuthenticationSessionRequest request = createAuthenticationSessionRequest();
AuthenticationSessionResponse response = getAuthenticationResponse(request);
return response.getSessionID();
}
/**
* Create {@link SmartIdAuthenticationResponse} from {@link SessionStatus}
*
* @throws UserRefusedException when the user has refused the session. NB! This exception has subclasses to determine the screen where user pressed cancel.
* @throws SessionTimeoutException when there was a timeout, i.e. end user did not confirm or refuse the operation within given time frame
* @throws UserSelectedWrongVerificationCodeException when user was presented with three control codes and user selected wrong code
* @throws DocumentUnusableException when for some reason, this relying party request cannot be completed.
*
* @param sessionStatus session status response
* @return the authentication response
*/
public SmartIdAuthenticationResponse createSmartIdAuthenticationResponse(SessionStatus sessionStatus) throws UserRefusedException, UserSelectedWrongVerificationCodeException,
SessionTimeoutException, DocumentUnusableException {
validateAuthenticationResponse(sessionStatus);
SessionResult sessionResult = sessionStatus.getResult();
SessionSignature sessionSignature = sessionStatus.getSignature();
SessionCertificate certificate = sessionStatus.getCert();
SmartIdAuthenticationResponse authenticationResponse = new SmartIdAuthenticationResponse();
authenticationResponse.setEndResult(sessionResult.getEndResult());
authenticationResponse.setSignedHashInBase64(getHashInBase64());
authenticationResponse.setHashType(getHashType());
authenticationResponse.setSignatureValueInBase64(sessionSignature.getValue());
authenticationResponse.setAlgorithmName(sessionSignature.getAlgorithm());
authenticationResponse.setCertificate(CertificateParser.parseX509Certificate(certificate.getValue()));
authenticationResponse.setRequestedCertificateLevel(getCertificateLevel());
authenticationResponse.setCertificateLevel(certificate.getCertificateLevel());
authenticationResponse.setDocumentNumber(sessionResult.getDocumentNumber());
authenticationResponse.setInteractionFlowUsed(sessionStatus.getInteractionFlowUsed());
authenticationResponse.setDeviceIpAddress(sessionStatus.getDeviceIpAddress());
return authenticationResponse;
}
protected void validateParameters() {
super.validateParameters();
super.validateAuthSignParameters();
}
private void validateAuthenticationResponse(SessionStatus sessionStatus) {
validateSessionResult(sessionStatus.getResult());
if (sessionStatus.getSignature() == null) {
logger.error("Signature was not present in the response");
throw new UnprocessableSmartIdResponseException("Signature was not present in the response");
}
if (sessionStatus.getCert() == null) {
logger.error("Certificate was not present in the response");
throw new UnprocessableSmartIdResponseException("Certificate was not present in the response");
}
}
private AuthenticationSessionResponse getAuthenticationResponse(AuthenticationSessionRequest request) {
SemanticsIdentifier semanticsIdentifier = getSemanticsIdentifier();
if (isNotEmpty(getDocumentNumber())) {
return getConnector().authenticate(getDocumentNumber(), request);
}
else {
return getConnector().authenticate(semanticsIdentifier, request);
}
}
private AuthenticationSessionRequest createAuthenticationSessionRequest() {
AuthenticationSessionRequest request = new AuthenticationSessionRequest();
request.setRelyingPartyUUID(getRelyingPartyUUID());
request.setRelyingPartyName(getRelyingPartyName());
request.setCertificateLevel(getCertificateLevel());
request.setHashType(getHashTypeString());
request.setHash(getHashInBase64());
request.setNonce(getNonce());
request.setCapabilities(getCapabilities());
request.setAllowedInteractionsOrder(getAllowedInteractionsOrder());
RequestProperties requestProperties = new RequestProperties();
requestProperties.setShareMdClientIpAddress(this.shareMdClientIpAddress);
if (requestProperties.hasProperties()) {
request.setRequestProperties(requestProperties);
}
return request;
}
}