Completed
Push — master ( c49dc0...0bed7f )
by Terrence
13:47
created

DBService::getUser()   D

Complexity

Conditions 10
Paths 4

Size

Total Lines 40
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 36
cp 0
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 30
nc 4
nop 1
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CILogon\Service;
4
5
use CILogon\Service\Util;
6
7
/**
8
 * DBService
9
 *
10
 * This class is a wrapper for the dbService servlet.  The dbService
11
 * servlet acts as a frontend to the database that stores info on users,
12
 * portal parameters, and IdPs. This was created to allow for fast
13
 * access to the database by keeping a connection open.  This class is a
14
 * rework of the old store.php class.
15
 *
16
 * Example usage:
17
 *     // For authentication, we have a bunch of attributes from an
18
 *     // identity provider. Thus get the database uid for the user
19
 *     // by using the multi-parameter version of getUser().
20
 *     $uid = '';
21
 *     $dbservice = new DBService();
22
 *     $dbservice->getUser('[email protected]',
23
 *                         'urn:mace:incommon:uiuc.edu',
24
 *                         'University of Illinois at Urbana-Champaign',
25
 *                         'John','Smith','John Smith,
26
 *                          '[email protected]');
27
 *     if (!($dbservice->status & 1)) { // OK status codes are even
28
 *         $uid = $dbservice->user_uid;
29
 *     }
30
 *
31
 *     // Later in the code, re-fetch the user using this uid
32
 *     // and print out the stored attributes.
33
 *     if (strlen($uid) > 0) {
34
 *         $dbservice->getUser($uid);
35
 *         echo 'Name = ' . $dbservice->first_name . ' ' .
36
 *                          $dbservice->last_name  . "\n";
37
 *         echo 'DN = '   . $dbservice->distinguished_name . "\n";
38
 *     }
39
 *
40
 *     // For getting/setting the Shibboleth-based IdPs, use the
41
 *     // getIdps()/setIdps() methods.  These methods utilize the
42
 *     // class member array $idp_uids for reading/writing. Two
43
 *     // convenience methods (setIdpsFromKeys($array) and
44
 *     // setIdpsFromValues($array)) are provided to populate the
45
 *     // $idp_uids array from the passed-in $array.
46
 *     $dbservice->getIdps();
47
 *     foreach($dbservice->idp_uids as $value) {
48
 *         echo "$value\n";
49
 *     }
50
 *
51
 *     $idps = array('urn:mace:incommon:ucsd.edu',
52
 *                   'urn:mace:incommon:uiuc.edu');
53
 *     $dbservice->setIdpsFromValues($idps);
54
 *     //   --- OR ---
55
 *     $idps = array('urn:mace:incommon:ucsd.edu' => 1,
56
 *                   'urn:mace:incommon:uiuc.edu' => 1);
57
 *     $dbservice->setIdpsFromKeys($idps);
58
 */
59
60
class DBService
61
{
62
    /**
63
     * @var string DEFAULTDBSERVICEURL The main URL for the dbService.
64
     *      Corresponds to the OAuth 1.0a .war.
65
     */
66
    const DEFAULTDBSERVICEURL = 'http://localhost:8080/oauth/dbService';
67
68
    /**
69
     * @var string DEFAULTDBSERVICEURL The new URL for the dbService, to be
70
     *      used once Jeff has verified all dbService calls work with the
71
     *      new OAuth 2.0 .war.
72
     */
73
    const OAUTH2DBSERVICEURL  = 'http://localhost:8080/oauth2/dbService';
74
75
    /**
76
     * @var array $STATUS The various STATUS_* constants, originally from
77
     *      Store.pm. The keys of the array are strings corresponding to the
78
     *      constant names. The values of the array are the integer (hex)
79
     *      values. For example, DBService::$STATUS['STATUS_OK'] = 0;
80
     *      Use 'array_search($this->status,DBService::$STATUS)' to look
81
     *      up the STATUS_* name given the status integer value.
82
     */
83
    public static $STATUS = array(
84
        'STATUS_OK'                        => 0x0,
85
        'STATUS_ACTION_NOT_FOUND'          => 0x1,
86
        'STATUS_NEW_USER'                  => 0x2,
87
        'STATUS_USER_UPDATED'              => 0x4,
88
        'STATUS_USER_NOT_FOUND'            => 0x6,
89
        'STATUS_USER_EXISTS'               => 0x8,
90
        'STATUS_USER_EXISTS_ERROR'         => 0xFFFA1,
91
        'STATUS_USER_NOT_FOUND_ERROR'      => 0xFFFA3,
92
        'STATUS_TRANSACTION_NOT_FOUND'     => 0xFFFA5,
93
        'STATUS_IDP_SAVE_FAILED'           => 0xFFFA7,
94
        'STATUS_DUPLICATE_PARAMETER_FOUND' => 0xFFFF1,
95
        'STATUS_INTERNAL_ERROR'            => 0xFFFF3,
96
        'STATUS_SAVE_IDP_FAILED'           => 0xFFFF5,
97
        'STATUS_MALFORMED_INPUT_ERROR'     => 0xFFFF7,
98
        'STATUS_MISSING_PARAMETER_ERROR'   => 0xFFFF9,
99
        'STATUS_NO_REMOTE_USER'            => 0xFFFFB,
100
        'STATUS_NO_IDENTITY_PROVIDER'      => 0xFFFFD,
101
        'STATUS_CLIENT_NOT_FOUND'          => 0xFFFFF,
102
        'STATUS_TRANSACTION_NOT_FOUND'     => 0x10001,
103
        'STATUS_EPTID_MISMATCH'            => 0x100001,
104
    );
105
106
    /**
107
     * @var int $status The returned status code from dbService calls
108
     */
109
    public $status;
110
111
    /**
112
     * @var string $user_uid The CILogon UID
113
     */
114
    public $user_uid;
115
116
    /**
117
     * @var string $remote_user The HTTP session REMOTE_USER
118
     */
119
    public $remote_user;
120
121
    /**
122
     * @var string $idp The Identity Provider's entityId
123
     */
124
    public $idp;
125
126
    /**
127
     * @var string $idp_display_name The Identity Provider's name
128
     */
129
    public $idp_display_name;
130
131
    /**
132
     * @var string $first_name User's given name
133
     */
134
    public $first_name;
135
136
    /**
137
     * @var string $last_name User's family name
138
     */
139
    public $last_name;
140
141
    /**
142
     * @var string $display_name User's full name
143
     */
144
    public $display_name;
145
146
    /**
147
     * @var string $email User's email address
148
     */
149
    public $email;
150
151
    /**
152
     * @var string $distinguished_name X.509 DN + email address
153
     */
154
    public $distinguished_name;
155
156
    /**
157
     * @var string $eppn eduPersonPrincipalName
158
     */
159
    public $eppn;
160
161
    /**
162
     * @var string $eptid eduPersonTargetedID
163
     */
164
    public $eptid;
165
166
    /**
167
     * @var string $open_id Old Google OpenID 2.0 identifier
168
     */
169
    public $open_id;
170
171
    /**
172
     * @var string $oidc OpenID Connect identifier
173
     */
174
    public $oidc;
175
176
    /**
177
     * @var string $affiliation eduPersonScopedAffiliation
178
     */
179
    public $affiliation;
180
181
    /**
182
     * @var string $ou Organizational Unit
183
     */
184
    public $ou;
185
186
    /**
187
     * @var string $serial_string CILogon serial string (e.g., A34201)
188
     */
189
    public $serial_string;
190
191
    /**
192
     * @var string $create_time Time user entry was created
193
     */
194
    public $create_time;
195
196
    /**
197
     * @var string $oauth_token OAuth 2.0 token
198
     */
199
    public $oauth_token;
200
201
    /**
202
     * @var string $cilogon_callback OAuth 1.0a callback URL
203
     */
204
    public $cilogon_callback;
205
206
    /**
207
     * @var string $cilogon_success OAuth 1.0a success URL
208
     */
209
    public $cilogon_success;
210
211
    /**
212
     * @var string $cilogon_failure OAuth 1.0a failure URL
213
     */
214
    public $cilogon_failure;
215
216
    /**
217
     * @var string $cilogon_portal_name OAuth client name
218
     */
219
    public $cilogon_portal_name;
220
221
    /**
222
     * @var string $two_factor Two factor string used by TwoFactor.php
223
     */
224
    public $two_factor;
225
226
    /**
227
     * @var array $idp_uids IdPs stored in the 'values' of the array
228
     */
229
    public $idp_uids;
230
231
    /**
232
     * @var string $client_name OAuth 2.0 client name
233
     */
234
    public $client_name;
235
236
    /**
237
     * @var string $client_id OAuth 2.0 client identifier
238
     */
239
    public $client_id;
240
241
    /**
242
     * @var string $client_home_uri OAuth 2.0 client home URL
243
     */
244
    public $client_home_uri;
245
246
    /**
247
     * @var array $client_callback_uris An array of OAuth 2.0 callback URLs
248
     */
249
    public $client_callback_uris;
250
251
    /**
252
     * @var string $dbservice URL The URL to use for the dbService
253
     */
254
    private $dbserviceurl;
255
256
    /**
257
     * __construct
258
     *
259
     * Default constructor.  All of the various class members are
260
     * initialized to 'null' or empty arrays.
261
     *
262
     * @param string $serviceurl (Optional) The URL of the database service
263
     *        servlet
264
     */
265
    public function __construct($serviceurl = self::DEFAULTDBSERVICEURL)
266
    {
267
        $this->clear();
268
        $this->setDBServiceURL($serviceurl);
269
    }
270
271
    /**
272
     * getDBServiceURL
273
     *
274
     * Returns the full URL of the database servlet used by the call()
275
     * function.
276
     *
277
     * @return string The URL of the database service servlet
278
     */
279
    public function getDBServiceURL()
280
    {
281
        return $this->dbserviceurl;
282
    }
283
284
    /**
285
     * setDBServiceURL
286
     *
287
     * Set the private variable $dbserviceurl to the full URL of the
288
     * database servlet, which is used by the call() function.
289
     *
290
     * @param string $serviceurl The URL of the database service servlet.
291
     */
292
    public function setDBServiceURL($serviceurl)
293
    {
294
        $this->dbserviceurl = $serviceurl;
295
    }
296
297
    /**
298
     * clear
299
     *
300
     * Set all of the class members to 'null' or empty arrays.
301
     */
302
    public function clear()
303
    {
304
        $this->clearUser();
305
        $this->clearPortal();
306
        $this->clearIdps();
307
        $this->clearClient();
308
    }
309
310
    /**
311
     * clearUser
312
     *
313
     * Set all of the class member variables associated with getUser()
314
     * to 'null'.
315
     */
316
    public function clearUser()
317
    {
318
        $this->status = null;
319
        $this->user_uid = null;
320
        $this->remote_user = null;
321
        $this->idp = null;
322
        $this->idp_display_name = null;
323
        $this->first_name = null;
324
        $this->last_name = null;
325
        $this->display_name = null;
326
        $this->email = null;
327
        $this->distinguished_name = null;
328
        $this->serial_string = null;
329
        $this->create_time = null;
330
        $this->two_factor = null;
331
        $this->affiliation = null;
332
        $this->ou = null;
333
    }
334
335
    /**
336
     * clearPortal
337
     *
338
     * Set all of the class member variables associated with
339
     * getPortalParameters() to 'null'.
340
     */
341
    public function clearPortal()
342
    {
343
        $this->status = null;
344
        $this->oauth_token = null;
345
        $this->cilogon_callback = null;
346
        $this->cilogon_success = null;
347
        $this->cilogon_failure = null;
348
        $this->cilogon_portal_name = null;
349
    }
350
351
    /**
352
     * clearIdps
353
     *
354
     * Set the class member variable $idp_uids to an empty array.
355
     */
356
    public function clearIdps()
357
    {
358
        $this->status = null;
359
        $this->idp_uids = array();
360
    }
361
362
    /**
363
     * clearClient
364
     *
365
     * Set all of the class member variables associated with
366
     * getClient() to 'null'.
367
     */
368
    public function clearClient()
369
    {
370
        $this->status = null;
371
        $this->client_name = null;
372
        $this->client_id = null;
373
        $this->client_home_uri = null;
374
        $this->client_callback_uris = array();
375
    }
376
377
    /**
378
     * getUser
379
     *
380
     * This method calls the 'getUser' action of the servlet and sets
381
     * the class member variables associated with user info
382
     * appropriately.  If the servlet returns correctly (i.e. an HTTP
383
     * status code of 200), this method returns true.
384
     *
385
     * @param mixed $args Variable number of parameters: 1, or more.
386
     *        For 1 parameter : $uid (database user identifier)
387
     *        For more than 1 parameter, parameters can include:
388
     *            $remote_user, $idp, $idp_display_name,
389
     *            $first_name, $last_name, $display_name, $email,
390
     *            $eppn, $eptid, $openid, $oidc, $affiliation, $ou
391
     *
392
     * @return bool True if the servlet returned correctly. Else false.
393
     */
394
    public function getUser(...$args)
395
    {
396
        $retval = false;
397
        $this->clearUser();
398
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
399
        $numargs = count($args);
400
        if ($numargs == 1) {
401
            $retval = $this->call('action=getUser&user_uid=' .
402
                urlencode($args[0]));
403
        } elseif ($numargs > 1) {
404
            $params = array('remote_user','idp','idp_display_name',
405
                            'first_name','last_name','display_name','email',
406
                            'eppn','eptid','open_id','oidc','affiliation','ou');
407
            $cmd = 'action=getUser';
408
            for ($i = 0; $i < $numargs; $i++) {
409
                $arg = $args[$i];
410
                if (strlen($arg) > 0) {
411
                    $cmd .= '&' . $params[$i] . '=';
412
                    // Convert idp_display_name, first_name, last_name to UTF-7
413
                    if (($i == 2) || ($i == 3) || ($i == 4)) {
414
                        $cmd .= urlencode(iconv('UTF-8', 'UTF-7', $arg));
415
                    } else {
416
                        $cmd .= urlencode($arg);
417
                    }
418
                }
419
            }
420
            // Add 'us_idp' parameter for InCommon/Google (1) or eduGAIN (0)
421
            $us_idp = 0;
422
            $idp = $args[1];
423
            $idp_display_name = $args[2];
424
            if ((Util::getIdpList()->isRegisteredByInCommon($idp)) ||
425
                (in_array($idp_display_name, ['Google', 'GitHub', 'ORCID']))) {
426
                $us_idp = 1;
427
            }
428
            $cmd .= "&us_idp=$us_idp";
429
430
            $retval = $this->call($cmd);
431
        }
432
        return $retval;
433
    }
434
435
    /**
436
     * getLastArchivedUser
437
     *
438
     * This method calls the 'getLastArchivedUser' action of the
439
     * servlet and sets the class member variables associated with user
440
     * info appropriately.  If the servlet returns correctly (i.e. an
441
     * HTTP status code of 200), this method returns true.
442
     *
443
     * @param string $uid The database user identifier
444
     * @return bool True if the servlet returned correctly. Else false.
445
     */
446
    public function getLastArchivedUser($uid)
447
    {
448
        $this->clearUser();
449
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
450
        return $this->call('action=getLastArchivedUser&user_uid=' .
451
            urlencode($uid));
452
    }
453
454
    /**
455
     * removeUser
456
     *
457
     * This method calls the 'removeUser' action of the servlet and
458
     * sets the class member variable $status appropriately.  If the
459
     * servlet returns correctly (i.e. an HTTP status code of 200),
460
     * this method returns true.
461
     *
462
     * @param string $uid The database user identifier
463
     * @return bool True if the servlet returned correctly. Else false.
464
     */
465
    public function removeUser($uid)
466
    {
467
        $this->clearUser();
468
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
469
        return $this->call('action=removeUser&user_uid=' .
470
            urlencode($uid));
471
    }
472
473
    /**
474
     * getTwoFactorInfo
475
     *
476
     * This method calls the 'getTwoFactorInfo' action of the servlet
477
     * and sets the class member variables associated with the user's
478
     * two-factor info appropriately. If the servlet returns correctly
479
     * (i.e. an HTTP status code of 200), this method returns true.
480
     * Note that this method isn't strictly necessary since the
481
     * two_factor info data is returned when getUser is called.
482
     *
483
     * @param string $uid The database user identifier
484
     * @return bool True if the servlet returned correctly. Else false.
485
     */
486
    public function getTwoFactorInfo($uid)
487
    {
488
        $this->two_factor = null;
489
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
490
        return $this->call('action=getTwoFactorInfo&user_uid=' .
491
            urlencode($uid));
492
    }
493
494
    /**
495
     * setTwoFactorInfo
496
     *
497
     * This method calls the 'setTwoFactorInfo' action of the servlet
498
     * and sets the class member variable associated with the user's
499
     * two-factor info appropriately. If the servlet returns correctly
500
     * (i.e. an HTTP status code of 200), this method returns true.
501
     *
502
     * @param string $uid The database user identifier
503
     * @param string $two_factor (Optional) The two-factor info string.
504
     *        Defaults to empty string.
505
     * @return bool True if the servlet returned correctly. Else false.
506
     */
507
    public function setTwoFactorInfo($uid, $two_factor = '')
508
    {
509
        $this->two_factor = $two_factor;
510
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
511
        return $this->call('action=setTwoFactorInfo&user_uid=' .
512
            urlencode($uid) . '&two_factor=' . urlencode($two_factor));
513
    }
514
515
    /**
516
     * getPortalParameters
517
     *
518
     * This method calls the 'getPortalParameter' action of the servlet
519
     * and sets the class member variables associated with the portal
520
     * parameters appropriately. If the servlet returns correctly (i.e.
521
     * an HTTP status code of 200), this method returns true.
522
     *
523
     * @param string $oauth_token The database OAuth identifier token
524
     * @return bool True if the servlet returned correctly. Else false.
525
     */
526
    public function getPortalParameters($oauth_token)
527
    {
528
        $this->clearPortal();
529
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
530
        return $this->call('action=getPortalParameter&oauth_token=' .
531
            urlencode($oauth_token));
532
    }
533
534
    /**
535
     * getIdps
536
     *
537
     * This method calls the 'getAllIdps' action of the servlet and
538
     * sets the class member array $idp_uris to contain all of the
539
     * Idps in the database, stored in the 'values' of the array.  If
540
     * the servlet returns correctly (i.e. an HTTP status code of 200),
541
     * this method returns true.
542
     *
543
     * @return bool True if the servlet returned correctly. Else false.
544
     */
545
    public function getIdps()
546
    {
547
        $this->clearIdps();
548
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
549
        return $this->call('action=getAllIdps');
550
    }
551
552
    /**
553
     * setIdps
554
     *
555
     * This method calls the 'setAllIdps' action of the servlet using
556
     * the class memeber array $idp_uris as the source for the Idps to
557
     * be stored to the database.  Note that if this array is empty,
558
     * an error code will be returned in the status since at least one
559
     * IdP should be saved to the database.  If you want to pass an
560
     * array of Idps to be saved, see the setIdpsFromKeys($array) and
561
     * setIdpsFromValues($array) methods.  If the servlet returns
562
     * correctly (i.e. an HTTP status code of 200), this method
563
     * returns true.
564
     *
565
     * @return bool True if the servlet returned correctly. Else false.
566
     */
567
    public function setIdps()
568
    {
569
        $retval = false;
570
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
571
        $idpcount = count($this->idp_uids);
572
        $idpidx = 0;
573
        if ($idpcount > 0) {
574
            // Loop through the idp_uids in chunks of 50 to deal
575
            // with query parameter limit of http browsers/servers.
576
            while ($idpidx < $idpcount) { // Loop through all IdPs
577
                $fiftyidx = 0;
578
                $idplist = '';
579
                while (($fiftyidx < 50) && // Send 50 IdPs at a time
580
                       ($idpidx < $idpcount)) {
581
                    $idplist .=  '&idp_uid=' .
582
                                 urlencode($this->idp_uids[$idpidx]);
583
                    $fiftyidx++;
584
                    $idpidx++;
585
                }
586
                $cmd = 'action=setAllIdps' . $idplist;
587
                $retval = $this->call($cmd);
588
            }
589
        }
590
        return $retval;
591
    }
592
593
    /**
594
     * setIdpsFromKeys
595
     *
596
     * This is a convenience method which calls setIdps using a
597
     * passed-in array of IdPs stored as the keys of the array.  It
598
     * first sets the class member array $idp_uids appropriately and
599
     * then calls the setIdps() method. If the servlet returns
600
     * correctly (i.e. an HTTP status code of 200), this method
601
     * returns true.  See also setIdpsFromValues().
602
     *
603
     * @param array $idps An array of IdPs to be saved, stored in the
604
     *       'keys' of the array.
605
     * @return bool True if the servlet returned correctly. Else false.
606
     */
607
    public function setIdpsFromKeys($idps)
608
    {
609
        $this->clearIdps();
610
        foreach ($idps as $key => $value) {
611
            $this->idp_uids[] = $key;
612
        }
613
        return $this->setIdps();
614
    }
615
616
    /**
617
     * setIdpsFromValues
618
     *
619
     * This is a convenience method which calls setIdps using a
620
     * passed-in array of IdPs stored as the values of the array.  It
621
     * first sets the class member array $idp_uids appropriately and
622
     * then calls the setIdps() method. If the servlet returns
623
     * correctly (i.e. an HTTP status code of 200), this method
624
     * returns true.  See also setIdpsFromKeys().
625
     *
626
     * @param array $idps An array of IdPs to be saved, stored in the
627
     *        'values' of the array.
628
     * @return bool True if the servlet returned correctly. Else false.
629
     */
630
    public function setIdpsFromValues($idps)
631
    {
632
        $this->clearIdps();
633
        foreach ($idps as $value) {
634
            $this->idp_uids[] = $value;
635
        }
636
        return $this->setIdps();
637
    }
638
639
    /**
640
     * getClient
641
     *
642
     * This method calls the 'getClient' action of the Oauth 2.0
643
     * servlet and sets the class member variables associated with
644
     * client info appropriately.  If the servlet returns correctly
645
     * (i.e. an HTTP status code of 200), this method returns true.
646
     *
647
     * @param string $cid The Oauth 2.0 Client ID (client_id).
648
     * @return bool True if the servlet returned correctly. Else false.
649
     */
650
    public function getClient($cid)
651
    {
652
        $this->clearClient();
653
        $this->setDBServiceURL(static::OAUTH2DBSERVICEURL);
654
        return $this->call('action=getClient&client_id=' .
655
            urlencode($cid));
656
    }
657
658
    /**
659
     * setTransactionState
660
     *
661
     * This method calls the 'setTransactionState' action of the Oauth
662
     * 2.0 servlet to associate the Oauth 2.0 'code' with the database
663
     * user UID. This is necessary for the Oauth 2.0 server to be able
664
     * to return information about the user (name, email address) as
665
     * well as return a certificate for the user. If the servlet
666
     * returns correctly (i.e. an HTTP status code of 200), this method
667
     * returns true. Check the 'status' return value to verify that
668
     * the transaction state was set successfully.
669
     *
670
     * @param string $code The 'code' as returned by the OAuth 2.0 server.
671
     * @param string $uid The database user UID.
672
     * @param int The Unix timestamp of the user authentication.
673
     * @param string $loa (Optional) The Level of Assurance: '' = basic,
674
     *        'openid' =  OpenID Connect (e.g., Google),
675
     *        'http://incommonfederation.org/assurance/silver' = silver
676
     * @param string $myproxyinfo (Optional) the 'info:...' string to be
677
     *        passed to MyProxy.
678
     * @return bool True if the servlet returned correctly. Else false.
679
     */
680
    public function setTransactionState(
681
        $code,
682
        $uid,
683
        $authntime,
684
        $loa = '',
685
        $myproxyinfo = ''
686
    ) {
687
        $this->setDBServiceURL(static::OAUTH2DBSERVICEURL);
688
        return $this->call(
689
            'action=setTransactionState' .
690
            '&code=' . urlencode($code) .
691
            '&user_uid=' . urlencode($uid) .
692
            '&auth_time=' . urlencode($authntime) .
693
            '&loa=' . urlencode($loa) .
694
            ((strlen($myproxyinfo) > 0) ?
695
                ('&cilogon_info=' . urlencode($myproxyinfo)) : '')
696
        );
697
    }
698
699
    /**
700
     * call
701
     *
702
     * This method does the brunt of the work for calling the
703
     * dbService servlet.  The single parameter is a string of
704
     * 'key1=value1&key2=value2&...' containing all of the parameters
705
     * for the dbService.  If the servlet returns an HTTP status code
706
     * of 200, then this method will return true.  It parses the return
707
     * output for various 'key=value' lines and stores then in the
708
     * appropriate member variables, urldecoded of course.
709
     *
710
     * @param string $params A string containing 'key=value' pairs,
711
     *        separated by ampersands ('&') as appropriate for passing to a
712
     *        URL for a GET query.
713
     * @return bool True if the servlet returned correctly. Else false.
714
     */
715
    public function call($params)
716
    {
717
        $success = false;
718
719
        $ch = curl_init();
720
        if ($ch !== false) {
721
            $url = $this->getDBServiceURL() . '?' . $params;
722
            curl_setopt($ch, CURLOPT_URL, $url);
723
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
724
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
725
            $output = curl_exec($ch);
726
            if (curl_errno($ch)) { // Send alert on curl errors
727
                Util::sendErrorAlert(
728
                    'cUrl Error',
729
                    'cUrl Error    = ' . curl_error($ch) . "\n" .
730
                    "URL Accessed  = $url"
731
                );
732
            }
733
            if (!empty($output)) {
734
                $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
735
                if ($httpcode == 200) {
736
                    $success = true;
737
                    if (preg_match('/status=([^\r\n]+)/', $output, $match)) {
738
                        $this->status = (int)(urldecode($match[1]));
739
                    }
740
                    if (preg_match('/user_uid=([^\r\n]+)/', $output, $match)) {
741
                        $this->user_uid = urldecode($match[1]);
742
                    }
743
                    if (preg_match('/remote_user=([^\r\n]+)/', $output, $match)) {
744
                        $this->remote_user = urldecode($match[1]);
745
                    }
746
                    if (preg_match('/idp=([^\r\n]+)/', $output, $match)) {
747
                        $this->idp = urldecode($match[1]);
748
                    }
749
                    if (preg_match('/idp_display_name=([^\r\n]+)/', $output, $match)) {
750
                        $this->idp_display_name = urldecode($match[1]);
751
                    }
752
                    if (preg_match('/first_name=([^\r\n]+)/', $output, $match)) {
753
                        $this->first_name = urldecode($match[1]);
754
                    }
755
                    if (preg_match('/last_name=([^\r\n]+)/', $output, $match)) {
756
                        $this->last_name = urldecode($match[1]);
757
                    }
758
                    if (preg_match('/display_name=([^\r\n]+)/', $output, $match)) {
759
                        $this->display_name = urldecode($match[1]);
760
                    }
761
                    if (preg_match('/email=([^\r\n]+)/', $output, $match)) {
762
                        $this->email = urldecode($match[1]);
763
                    }
764
                    if (preg_match('/distinguished_name=([^\r\n]+)/', $output, $match)) {
765
                        $this->distinguished_name = urldecode($match[1]);
766
                    }
767
                    if (preg_match('/eppn=([^\r\n]+)/', $output, $match)) {
768
                        $this->eppn = urldecode($match[1]);
769
                    }
770
                    if (preg_match('/eptid=([^\r\n]+)/', $output, $match)) {
771
                        $this->eptid = urldecode($match[1]);
772
                    }
773
                    if (preg_match('/open_id=([^\r\n]+)/', $output, $match)) {
774
                        $this->open_id = urldecode($match[1]);
775
                    }
776
                    if (preg_match('/oidc=([^\r\n]+)/', $output, $match)) {
777
                        $this->oidc = urldecode($match[1]);
778
                    }
779
                    if (preg_match('/affiliation=([^\r\n]+)/', $output, $match)) {
780
                        $this->affiliation = urldecode($match[1]);
781
                    }
782
                    if (preg_match('/ou=([^\r\n]+)/', $output, $match)) {
783
                        $this->ou = urldecode($match[1]);
784
                    }
785
                    if (preg_match('/serial_string=([^\r\n]+)/', $output, $match)) {
786
                        $this->serial_string = urldecode($match[1]);
787
                    }
788
                    if (preg_match('/create_time=([^\r\n]+)/', $output, $match)) {
789
                        $this->create_time = urldecode($match[1]);
790
                    }
791
                    if (preg_match('/oauth_token=([^\r\n]+)/', $output, $match)) {
792
                        $this->oauth_token = urldecode($match[1]);
793
                    }
794
                    if (preg_match('/cilogon_callback=([^\r\n]+)/', $output, $match)) {
795
                        $this->cilogon_callback = urldecode($match[1]);
796
                    }
797
                    if (preg_match('/cilogon_success=([^\r\n]+)/', $output, $match)) {
798
                        $this->cilogon_success = urldecode($match[1]);
799
                    }
800
                    if (preg_match('/cilogon_failure=([^\r\n]+)/', $output, $match)) {
801
                        $this->cilogon_failure = urldecode($match[1]);
802
                    }
803
                    if (preg_match('/cilogon_portal_name=([^\r\n]+)/', $output, $match)) {
804
                        $this->cilogon_portal_name = urldecode($match[1]);
805
                    }
806
                    if (preg_match('/two_factor=([^\r\n]+)/', $output, $match)) {
807
                        $this->two_factor = urldecode($match[1]);
808
                    }
809
                    if (preg_match_all('/idp_uid=([^\r\n]+)/', $output, $match)) {
810
                        foreach ($match[1] as $value) {
811
                            $this->idp_uids[] = urldecode($value);
812
                        }
813
                    }
814
                    if (preg_match('/client_name=([^\r\n]+)/', $output, $match)) {
815
                        $this->client_name = urldecode($match[1]);
816
                    }
817
                    if (preg_match('/client_id=([^\r\n]+)/', $output, $match)) {
818
                        $this->client_id = urldecode($match[1]);
819
                    }
820
                    if (preg_match('/client_home_uri=([^\r\n]+)/', $output, $match)) {
821
                        $this->client_home_uri = urldecode($match[1]);
822
                    }
823
                    if (preg_match('/client_callback_uris=([^\r\n]+)/', $output, $match)) {
824
                        $this->client_callback_uris = explode(urldecode($match[1]), ',');
825
                    }
826
                }
827
            }
828
            curl_close($ch);
829
        }
830
        return $success;
831
    }
832
833
    /**
834
     * dump
835
     *
836
     * This is a convenience method which prints out all of the
837
     * non-null / non-empty member variables to stdout.
838
     */
839
    public function dump()
840
    {
841
        if (!is_null($this->status)) {
842
            echo "status=$this->status (" .
843
            array_search($this->status, static::$STATUS) . ")\n";
844
        }
845
        if (!is_null($this->user_uid)) {
846
            echo "user_uid=$this->user_uid\n";
847
        }
848
        if (!is_null($this->remote_user)) {
849
            echo "remote_user=$this->remote_user\n";
850
        }
851
        if (!is_null($this->idp)) {
852
            echo "idp=$this->idp\n";
853
        }
854
        if (!is_null($this->idp_display_name)) {
855
            echo "idp_display_name=$this->idp_display_name\n";
856
        }
857
        if (!is_null($this->first_name)) {
858
            echo "first_name=$this->first_name\n";
859
        }
860
        if (!is_null($this->last_name)) {
861
            echo "last_name=$this->last_name\n";
862
        }
863
        if (!is_null($this->display_name)) {
864
            echo "display_name=$this->display_name\n";
865
        }
866
        if (!is_null($this->email)) {
867
            echo "email=$this->email\n";
868
        }
869
        if (!is_null($this->distinguished_name)) {
870
            echo "distinguished_name=$this->distinguished_name\n";
871
        }
872
        if (!is_null($this->eppn)) {
873
            echo "eppn=$this->eppn\n";
874
        }
875
        if (!is_null($this->eptid)) {
876
            echo "eptid=$this->eptid\n";
877
        }
878
        if (!is_null($this->open_id)) {
879
            echo "open_id=$this->open_id\n";
880
        }
881
        if (!is_null($this->oidc)) {
882
            echo "oidc=$this->oidc\n";
883
        }
884
        if (!is_null($this->affiliation)) {
885
            echo "affiliation=$this->affiliation\n";
886
        }
887
        if (!is_null($this->ou)) {
888
            echo "ou=$this->ou\n";
889
        }
890
        if (!is_null($this->serial_string)) {
891
            echo "serial_string=$this->serial_string\n";
892
        }
893
        if (!is_null($this->create_time)) {
894
            echo "create_time=$this->create_time\n";
895
        }
896
        if (!is_null($this->oauth_token)) {
897
            echo "oauth_token=$this->oauth_token\n";
898
        }
899
        if (!is_null($this->cilogon_callback)) {
900
            echo "cilogon_callback=$this->cilogon_callback\n";
901
        }
902
        if (!is_null($this->cilogon_success)) {
903
            echo "cilogon_success=$this->cilogon_success\n";
904
        }
905
        if (!is_null($this->cilogon_failure)) {
906
            echo "cilogon_failure=$this->cilogon_failure\n";
907
        }
908
        if (!is_null($this->cilogon_portal_name)) {
909
            echo "cilogon_portal_name=$this->cilogon_portal_name\n";
910
        }
911
        if (!is_null($this->two_factor)) {
912
            echo "two_factor=$this->two_factor\n";
913
        }
914
        if (count($this->idp_uids) > 0) {
915
            uasort($this->idp_uids, 'strcasecmp');
916
            echo "idp_uids={\n";
917
            foreach ($this->idp_uids as $value) {
918
                echo "    $value\n";
919
            }
920
            echo "}\n";
921
        }
922
        if (!is_null($this->client_name)) {
923
            echo "client_name=$this->client_name\n";
924
        }
925
        if (!is_null($this->client_id)) {
926
            echo "client_id=$this->client_id\n";
927
        }
928
        if (!is_null($this->client_home_uri)) {
929
            echo "client_home_uri=$this->client_home_uri\n";
930
        }
931
        if (count($this->client_callback_uris) > 0) {
932
            uasort($this->client_callback_uris, 'strcasecmp');
933
            echo "client_callback_uris={\n";
934
            foreach ($this->client_callback_uris as $value) {
935
                echo "    $value\n";
936
            }
937
            echo "}\n";
938
        }
939
    }
940
}
941