-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecSignIDApi.php
More file actions
601 lines (495 loc) · 20.5 KB
/
SecSignIDApi.php
File metadata and controls
601 lines (495 loc) · 20.5 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
<?php
//
// SecSign ID Api in php.
//
// (c) 2014-2019 SecSign Technologies Inc.
//
define("SCRIPT_VERSION", '1.47');
/*
* PHP class to connect to a secsign id server. the class will check secsign id server certificate and request for authentication session generation for a given
* user id which is called secsign id. Each authentication session generation needs a new instance of this class.
*
* @author SecSign Technologies Inc.
* @copyright 2014-2017
*/
class SecSignIDApi
{
// once created the api can be used to create a single request for a certain specified userid
private $secSignIDServer = NULL;
private $secSignIDServerPort = NULL;
private $secSignIDServer_fallback = NULL;
private $secSignIDServerPort_fallback = NULL;
// numeric script version.
private $scriptVersion = 0;
private $referer = NULL;
private $logger = NULL;
private $pluginName = NULL;
private $showAccesspass = NULL;
private $lastResponse = NULL;
/*
* Constructor
*/
function __construct()
{
// secsign id server: hostname and port
$this->secSignIDServer = (string) "https://httpapi.secsign.com";
$this->secSignIDServerPort = (int) 443;
$this->secSignIDServer_fallback = (string) "https://httpapi2.secsign.com";
$this->secSignIDServerPort_fallback = (int) 443;
// script version from cvs revision string
$this->scriptVersion = SCRIPT_VERSION;
// use a constant string rather than using the __CLASS__ definition
// because this could cause problems when the class is in a submodule
$this->referer = "SecSignIDApi_PHP";
}
/*
* Destructor
*/
function __destruct()
{
$this->secSignIDServer = NULL;
$this->secSignIDServerPort = NULL;
$this->secSignIDServer_fallback = NULL;
$this->secSignIDServerPort_fallback = NULL;
$this->pluginName = NULL;
$this->showAccesspass = NULL;
$this->scriptVersion = NULL;
$this->logger = NULL;
}
/**
* Function to check whether curl is available
*/
function prerequisite()
{
if(! function_exists("curl_init")){
return false;
}
if(! function_exists("curl_exec")){
return false;
}
if(! is_callable("curl_init", true, $callable_name)){
return false;
}
if(! is_callable("curl_exec", true, $callable_name)){
return false;
}
return true;
}
/*
* Sets a function which is used as a logger
*/
function setLogger($logger)
{
if($logger !== NULL && isset($logger) && is_callable($logger) === TRUE){
$this->logger = $logger;
}
}
/*
* logs a message if logger instance is not NULL
*/
private function log($message)
{
if($this->logger !== NULL){
$logMessage = __CLASS__ . " (v" . $this->scriptVersion . "): " . $message;
call_user_func($this->logger, $logMessage);
}
}
/*
* Sets an optional plugin name
*/
function setPluginName($pluginName)
{
$this->pluginName = $pluginName;
}
/*
* Sets an optional parameter to determine if the accesspass should be used
*/
function setShowAccesspass($showAccesspass)
{
$this->showAccesspass = $showAccesspass;
}
/*
* Gets last response
*/
function getResponse()
{
return $this->lastResponse;
}
/*
* Send query to secsign id server to create an authentication session for a certain secsign id. This method returns the authentication session itself.
*/
function requestAuthSession($secsignid, $servicename, $serviceadress)
{
$this->log("Call of function 'requestAuthSession'.");
if(empty($servicename)){
$this->log("Parameter \$servicename must not be null.");
throw new Exception("Parameter \$servicename must not be null.");
}
if(empty($serviceadress)){
$this->log("Parameter \$serviceadress must not be null.");
throw new Exception("Parameter \$serviceadress must not be null.");
}
if(empty($secsignid)){
$this->log("Parameter \$secsignid must not be null.");
throw new Exception("Parameter \$secsignid must not be null.");
}
// secsign id is always key insensitive. comvert to lower case and trim whitespace
$secsignid = trim(strtolower($secsignid));
// check again. probably just spacess which will ne empty after trim()
if(empty($secsignid)){
$this->log("Parameter \$secsignid must not be null.");
throw new Exception("Parameter \$secsignid must not be null.");
}
$requestParameter = array('request' => 'ReqRequestAuthSession',
'secsignid' => $secsignid,
'servicename' => $servicename,
'serviceaddress' => $serviceadress);
if($this->pluginName !== NULL){
$requestParameter['pluginname'] = $this->pluginName;
}
if($this->showAccesspass !== NULL){
$requestParameter['showaccesspass'] = $this->showAccesspass;
}
$response = $this->send($requestParameter, NULL);
$authSession = new AuthSession();
$authSession->CreateAuthSessionFromArray($response);
return $authSession;
}
/*
* Gets the authentication session state for a certain secsign id whether the authentication session is still pending or it was accepted or denied.
*/
function getAuthSessionState($authSession)
{
$this->log("Call of function 'getAuthSessionState'.");
if($authSession === NULL || !($authSession instanceof AuthSession)){
$message = "Parameter \$authSession is not an instance of AuthSession. get_class(\$authSession)=" . get_class($authSession);
$this->log($message);
throw new Exception($message);
}
$requestParameter = array('request' => 'ReqGetAuthSessionState');
$response = $this->send($requestParameter, $authSession);
return $response['authsessionstate'];
}
/*
* Cancel the given auth session.
*/
function cancelAuthSession($authSession)
{
$this->log("Call of function 'cancelAuthSession'.");
if($authSession === NULL || !($authSession instanceof AuthSession)){
$message = "Parameter \$authSession is not an instance of AuthSession. get_class(\$authSession)=" . get_class($authSession);
$this->log($message);
throw new Exception($message);
}
$requestParameter = array('request' => 'ReqCancelAuthSession');
$response = $this->send($requestParameter, $authSession);
return $response['authsessionstate'];
}
/*
* build an array with all parameters which has to be send to server
*/
private function buildParameterArray($parameter, $authSession)
{
//$mandatoryParams = array('apimethod' => $this->referer, 'scriptversion' => $this->scriptVersion);
$mandatoryParams = array('apimethod' => $this->referer);
if(isset($authSession))
{
// add auth session data to mandatory parameter array
$authSessionData = array('secsignid' => strtolower($authSession->getSecSignID()),
'authsessionid' => $authSession->getAuthSessionID(),
'requestid' => $authSession->getRequestID());
$mandatoryParams = array_merge($mandatoryParams, $authSessionData);
}
return array_merge($mandatoryParams, $parameter);
}
/*
* sends given parameters to secsign id server and wait given amount
* of seconds till the connection is timed out
*/
function send($parameter, $authSession)
{
$requestQuery = http_build_query($this->buildParameterArray($parameter, $authSession), '', '&');
$timeout_in_seconds = 15;
// create cURL resource
$ch = $this->getCURLHandle($this->secSignIDServer, $this->secSignIDServerPort, $requestQuery, $timeout_in_seconds);
$this->log("curl_init: " . $ch);
// $output contains the output string
$this->log("cURL curl_exec sent params: " . $requestQuery);
$output = curl_exec($ch);
if ($output === false)
{
$this->log("curl_error: " . curl_error($ch));
}
// close curl resource to free up system resources
$this->log("curl_close: " . $ch);
curl_close($ch);
// check if output is NULL. in that case the secsign id might not have been reached.
if($output === NULL)
{
$this->log("curl: output is NULL. Server " . $this->secSignIDServer . ":" . $this->secSignIDServerPort . " has not been reached.");
if($this->secSignIDServer_fallback !== NULL)
{
$this->log("curl: get new handle from fallback server.");
$ch = $this->getCURLHandle($this->secSignIDServer_fallback, $this->secSignIDServerPort_fallback, $requestQuery, $timeout_in_seconds);
$this->log("curl_init: " . $ch . " connecting to " . curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
// $output contains the output string
$output = curl_exec($ch);
if($output === NULL)
{
$this->log("output is NULL. Fallback server " . $this->secSignIDServer_fallback . ":" . $this->secSignIDServerPort_fallback . " has not been reached.");
$this->log("curl_error: " . curl_error($ch));
throw new Exception("curl_exec error: can't connect to Server - " . curl_error($ch));
}
// close curl resource to free up system resources
$this->log("curl_close: " . $ch);
curl_close($ch);
}
else
{
$this->log("curl: no fallback server has been specified.");
}
}
$this->log("curl_exec response: " . ($output === NULL ? "NULL" : $output));
$this->lastResponse = $output;
return $this->checkResponse($output, TRUE); // will throw an exception in case of an error
}
/*
* checks the secsign id server response string
*/
private function checkResponse($response, $throwExcIfError)
{
if(! isset($response))
{
$this->log("Could not connect to host '" . $this->secSignIDServer . ":" . $this->secSignIDServerPort . "'");
if($throwExcIfError)
{
throw new Exception("Could not connect to server.");
}
}
$responseArray = array();
// server send parameter strings like:
// var1=value1&var2=value2&var3=value3&...
$valuePairs = explode("&", $response);
foreach($valuePairs as $pair)
{
$exploded = explode("=", $pair, 2);
if (count($exploded) === 2)
{
list($key, $value) = $exploded;
$responseArray[$key] = $value;
}
}
// check if server send a parameter named 'error'
if(isset($responseArray['error']))
{
$this->log("SecSign ID server sent error. code=" . $responseArray['error'] . " message=" . $responseArray['errormsg']);
if($throwExcIfError)
{
throw new Exception($responseArray['errormsg'], $responseArray['error']);
}
}
return $responseArray;
}
/*
* Gets a cURL resource handle.
*/
private function getCURLHandle($server = NULL, $port = -1, $parameter, $timeout_in_seconds)
{
// create cURL resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $server);
//curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
curl_setopt($ch, CURLOPT_PORT, $port);
//curl_setopt($ch, CURLOPT_SSLVERSION, 3);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0); // value 0 will strip header information in response
// set connection timeout
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout_in_seconds);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
// make sure the common name of the certificate's subject matches the server's host name
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// validate the certificate chain of the server
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
//The CA certificates
curl_setopt($ch, CURLOPT_CAINFO, realpath(dirname(__FILE__)) .'/curl-ca-bundle.crt');
// add referer
curl_setopt($ch, CURLOPT_REFERER, $this->referer);
// add all parameter and change request mode to POST
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);
return $ch;
}
}
/*
* PHP class to gather all information about a so called authentication session.
* It contains the access pass, a request id, a session id and the secsign id.
*
* @author SecSign Technologies Inc.
* @copyright 2014-2017
*/
class AuthSession
{
/*
* No State: Used when the session state is undefined.
*/
const NOSTATE = 0;
/*
* Pending: The session is still pending for authentication.
*/
const PENDING = 1;
/*
* Expired: The authentication timeout has been exceeded.
*/
const EXPIRED = 2;
/*
* Authenticated: The user was successfully authenticated.
*/
const AUTHENTICATED = 3;
/*
* Denied: The user denied this session.
*/
const DENIED = 4;
/*
* Suspended: The server suspended this session, because another authentication request was received while this session was still pending.
*/
const SUSPENDED = 5;
/*
* Canceled: The service has canceled this session.
*/
const CANCELED = 6;
/*
* Fetched: The device has already fetched the session, but the session hasn't been authenticated or denied yet.
*/
const FETCHED = 7;
/*
* Invalid: This session has become invalid.
*/
const INVALID = 8;
/*
* the secsign id the authentication session has been craeted for
*/
private $secSignID = NULL;
/*
* authentication session id
*/
private $authSessionID = NULL;
/*
* the name of the requesting service. this will be shown at the smartphone
*/
private $requestingServiceName = NULL;
/*
* the address, a valid url, of the requesting service. this will be shown at the smartphone
*/
private $requestingServiceAddress = NULL;
/*
* the request ID is similar to a server side session ID.
* it is generated after a authentication session has been created. all other request like dispose, withdraw or to get the auth session state
* will be rejected if a request id is not specified.
*/
private $requestID = NULL;
/*
* icon data of the so called access pass. the image data needs to be displayed otherwise the user does not know which access apss he needs to choose in order to accept the authentication session.
*/
private $authSessionIconData = NULL;
/*
* Getter for secsign id
*/
function getSecSignID()
{
return $this->secSignID;
}
/*
* Getter for auth session id
*/
function getAuthSessionID()
{
return $this->authSessionID;
}
/*
* Getter for auth session requesting service
*/
function getRequestingServiceName()
{
return $this->requestingServiceName;
}
/*
* Getter for auth session requesting service
*/
function getRequestingServiceAddress()
{
return $this->requestingServiceAddress;
}
/*
* Getter for request id
*/
function getRequestID()
{
return $this->requestID;
}
/*
* Getter for icon data which needs to be display
*/
function getIconData()
{
return $this->authSessionIconData;
}
/*
* method to get string representation of this authentication session object
*/
function __toString()
{
return $this->authSessionID . " (" . $this->secSignID . ", " . $this->requestingServiceAddress . ", icondata=" . $this->authSessionIconData . ")";
}
/*
* builds an url parameter string like key1=value1&key2=value2&foo=bar
*/
function getAuthSessionAsArray()
{
return array('secsignid' => $this->secSignID,
'authsessionid' => $this->authSessionID,
'servicename' => $this->requestingServiceName,
'serviceaddress'=> $this->requestingServiceAddress,
'authsessionicondata'=> $this->authSessionIconData,
'requestid' => $this->requestID);
}
/*
* Creates/Fills the auth session obejct using the given array. The array must use secsignid, auth session id etc as keys.
*/
function createAuthSessionFromArray($array, $ignoreOptionalParameter = false)
{
if(! isset($array)){
throw new Exception("Parameter array is NULL.");
}
if(! is_array($array)){
throw new Exception("Parameter array is not an array. (array=" . $array . ")");
}
// check mandatory parameter
if(! isset($array['secsignid'])){
throw new Exception("Parameter array does not contain a value 'secsignid'.");
}
if(! isset($array['authsessionid'])){
throw new Exception("Parameter array does not contain a value 'authsessionid'.");
}
if(! isset($array['servicename']) && !$ignoreOptionalParameter){
throw new Exception("Parameter array does not contain a value 'servicename'.");
}
if(! isset($array['serviceaddress']) && !$ignoreOptionalParameter){
throw new Exception("Parameter array does not contain a value 'serviceaddress'.");
}
if(! isset($array['requestid'])){
throw new Exception("Parameter array does not contain a value 'requestid'.");
}
$this->secSignID = $array['secsignid'];
$this->authSessionID = $array['authsessionid'];
$this->requestingServiceName = $array['servicename'];
$this->requestingServiceAddress = $array['serviceaddress'];
$this->requestID = $array['requestid'];
// everything else must exist
if(isset($array['authsessionicondata'])){
$this->authSessionIconData = $array['authsessionicondata'];
}
}
}
?>