Completed
Push — master ( fa0664...7412e9 )
by Terrence
15:09
created

DBService::getUser()   C

Complexity

Conditions 12
Paths 8

Size

Total Lines 55
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 0
cts 39
cp 0
rs 6.8009
c 0
b 0
f 0
cc 12
eloc 39
nc 8
nop 1
crap 156

How to fix   Long Method    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 $member_of isMemberOf group information
188
     */
189
    public $member_of;
190
191
    /**
192
     * @var string $serial_string CILogon serial string (e.g., A34201)
193
     */
194
    public $serial_string;
195
196
    /**
197
     * @var string $create_time Time user entry was created
198
     */
199
    public $create_time;
200
201
    /**
202
     * @var string $oauth_token OAuth 2.0 token
203
     */
204
    public $oauth_token;
205
206
    /**
207
     * @var string $cilogon_callback OAuth 1.0a callback URL
208
     */
209
    public $cilogon_callback;
210
211
    /**
212
     * @var string $cilogon_success OAuth 1.0a success URL
213
     */
214
    public $cilogon_success;
215
216
    /**
217
     * @var string $cilogon_failure OAuth 1.0a failure URL
218
     */
219
    public $cilogon_failure;
220
221
    /**
222
     * @var string $cilogon_portal_name OAuth client name
223
     */
224
    public $cilogon_portal_name;
225
226
    /**
227
     * @var string $two_factor Two factor string used by TwoFactor.php
228
     */
229
    public $two_factor;
230
231
    /**
232
     * @var array $idp_uids IdPs stored in the 'values' of the array
233
     */
234
    public $idp_uids;
235
236
    /**
237
     * @var string $client_name OAuth 2.0 client name
238
     */
239
    public $client_name;
240
241
    /**
242
     * @var string $client_id OAuth 2.0 client identifier
243
     */
244
    public $client_id;
245
246
    /**
247
     * @var string $client_home_uri OAuth 2.0 client home URL
248
     */
249
    public $client_home_uri;
250
251
    /**
252
     * @var array $client_callback_uris An array of OAuth 2.0 callback URLs
253
     */
254
    public $client_callback_uris;
255
256
    /**
257
     * @var string $dbservice URL The URL to use for the dbService
258
     */
259
    private $dbserviceurl;
260
261
    /**
262
     * __construct
263
     *
264
     * Default constructor.  All of the various class members are
265
     * initialized to 'null' or empty arrays.
266
     *
267
     * @param string $serviceurl (Optional) The URL of the database service
268
     *        servlet
269
     */
270
    public function __construct($serviceurl = self::DEFAULTDBSERVICEURL)
271
    {
272
        $this->clear();
273
        $this->setDBServiceURL($serviceurl);
274
    }
275
276
    /**
277
     * getDBServiceURL
278
     *
279
     * Returns the full URL of the database servlet used by the call()
280
     * function.
281
     *
282
     * @return string The URL of the database service servlet
283
     */
284
    public function getDBServiceURL()
285
    {
286
        return $this->dbserviceurl;
287
    }
288
289
    /**
290
     * setDBServiceURL
291
     *
292
     * Set the private variable $dbserviceurl to the full URL of the
293
     * database servlet, which is used by the call() function.
294
     *
295
     * @param string $serviceurl The URL of the database service servlet.
296
     */
297
    public function setDBServiceURL($serviceurl)
298
    {
299
        $this->dbserviceurl = $serviceurl;
300
    }
301
302
    /**
303
     * clear
304
     *
305
     * Set all of the class members to 'null' or empty arrays.
306
     */
307
    public function clear()
308
    {
309
        $this->clearUser();
310
        $this->clearPortal();
311
        $this->clearIdps();
312
        $this->clearClient();
313
    }
314
315
    /**
316
     * clearUser
317
     *
318
     * Set all of the class member variables associated with getUser()
319
     * to 'null'.
320
     */
321
    public function clearUser()
322
    {
323
        $this->status = null;
324
        $this->user_uid = null;
325
        $this->remote_user = null;
326
        $this->idp = null;
327
        $this->idp_display_name = null;
328
        $this->first_name = null;
329
        $this->last_name = null;
330
        $this->display_name = null;
331
        $this->email = null;
332
        $this->distinguished_name = null;
333
        $this->serial_string = null;
334
        $this->create_time = null;
335
        $this->two_factor = null;
336
        $this->affiliation = null;
337
        $this->ou = null;
338
        $this->member_of = null;
339
    }
340
341
    /**
342
     * clearPortal
343
     *
344
     * Set all of the class member variables associated with
345
     * getPortalParameters() to 'null'.
346
     */
347
    public function clearPortal()
348
    {
349
        $this->status = null;
350
        $this->oauth_token = null;
351
        $this->cilogon_callback = null;
352
        $this->cilogon_success = null;
353
        $this->cilogon_failure = null;
354
        $this->cilogon_portal_name = null;
355
    }
356
357
    /**
358
     * clearIdps
359
     *
360
     * Set the class member variable $idp_uids to an empty array.
361
     */
362
    public function clearIdps()
363
    {
364
        $this->status = null;
365
        $this->idp_uids = array();
366
    }
367
368
    /**
369
     * clearClient
370
     *
371
     * Set all of the class member variables associated with
372
     * getClient() to 'null'.
373
     */
374
    public function clearClient()
375
    {
376
        $this->status = null;
377
        $this->client_name = null;
378
        $this->client_id = null;
379
        $this->client_home_uri = null;
380
        $this->client_callback_uris = array();
381
    }
382
383
    /**
384
     * getUser
385
     *
386
     * This method calls the 'getUser' action of the servlet and sets
387
     * the class member variables associated with user info
388
     * appropriately.  If the servlet returns correctly (i.e. an HTTP
389
     * status code of 200), this method returns true.
390
     *
391
     * @param mixed $args Variable number of parameters: 1, or more.
392
     *        For 1 parameter : $uid (database user identifier)
393
     *        For more than 1 parameter, parameters can include:
394
     *            $remote_user, $idp, $idp_display_name,
395
     *            $first_name, $last_name, $display_name, $email,
396
     *            $eppn, $eptid, $openid, $oidc, $affiliation,
397
     *            $ou, $member_of
398
     *
399
     * @return bool True if the servlet returned correctly. Else false.
400
     */
401
    public function getUser(...$args)
402
    {
403
        $retval = false;
404
        $this->clearUser();
405
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
406
        $numargs = count($args);
407
        if ($numargs == 1) {
408
            $retval = $this->call('action=getUser&user_uid=' .
409
                urlencode($args[0]));
410
        } elseif ($numargs > 1) {
411
            $params = array('remote_user', 'idp', 'idp_display_name',
412
                            'first_name', 'last_name', 'display_name', 'email',
413
                            'eppn', 'eptid', 'open_id', 'oidc', 'affiliation',
414
                            'ou', 'member_of');
415
            $cmd = 'action=getUser';
416
            $attr_arr = array();
417
            for ($i = 0; $i < $numargs; $i++) {
418
                $arg = $args[$i];
419
                if (strlen($arg) > 0) {
420
                    if ($i >= 13) {
421
                        // Put params after $ou into JSON object
422
                        $attr_arr[$params[$i]] = $arg;
423
                    } else {
424
                        $cmd .= '&' . $params[$i] . '=';
425
                        if (($i >= 2) && ($i <= 5)) {
426
                            // Convert idp_display_name, first_name, last_name,
427
                            // and display_name to UTF-7
428
                            $cmd .= urlencode(iconv('UTF-8', 'UTF-7', $arg));
429
                        } else {
430
                            $cmd .= urlencode($arg);
431
                        }
432
                    }
433
                }
434
            }
435
            // If any elements in $attr_arr, append converted JSON object
436
            if (count($attr_arr) > 0) {
437
                if (($attr_json = json_encode($attr_arr, JSON_FORCE_OBJECT))
438
                    !== false) {
439
                    $cmd .= '&attr_json=' . urlencode($attr_json);
440
                }
441
            }
442
            // Add 'us_idp' parameter for InCommon/Google (1) or eduGAIN (0)
443
            $us_idp = 0;
444
            $idp = $args[1];
445
            $idp_display_name = $args[2];
446
            if ((Util::getIdpList()->isRegisteredByInCommon($idp)) ||
447
                (in_array($idp_display_name, Util::$oauth2idps))) {
448
                $us_idp = 1;
449
            }
450
            $cmd .= "&us_idp=$us_idp";
451
452
            $retval = $this->call($cmd);
453
        }
454
        return $retval;
455
    }
456
457
    /**
458
     * getLastArchivedUser
459
     *
460
     * This method calls the 'getLastArchivedUser' action of the
461
     * servlet and sets the class member variables associated with user
462
     * info appropriately.  If the servlet returns correctly (i.e. an
463
     * HTTP status code of 200), this method returns true.
464
     *
465
     * @param string $uid The database user identifier
466
     * @return bool True if the servlet returned correctly. Else false.
467
     */
468
    public function getLastArchivedUser($uid)
469
    {
470
        $this->clearUser();
471
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
472
        return $this->call('action=getLastArchivedUser&user_uid=' .
473
            urlencode($uid));
474
    }
475
476
    /**
477
     * removeUser
478
     *
479
     * This method calls the 'removeUser' action of the servlet and
480
     * sets the class member variable $status appropriately.  If the
481
     * servlet returns correctly (i.e. an HTTP status code of 200),
482
     * this method returns true.
483
     *
484
     * @param string $uid The database user identifier
485
     * @return bool True if the servlet returned correctly. Else false.
486
     */
487
    public function removeUser($uid)
488
    {
489
        $this->clearUser();
490
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
491
        return $this->call('action=removeUser&user_uid=' .
492
            urlencode($uid));
493
    }
494
495
    /**
496
     * getTwoFactorInfo
497
     *
498
     * This method calls the 'getTwoFactorInfo' action of the servlet
499
     * and sets the class member variables associated with the user's
500
     * two-factor info appropriately. If the servlet returns correctly
501
     * (i.e. an HTTP status code of 200), this method returns true.
502
     * Note that this method isn't strictly necessary since the
503
     * two_factor info data is returned when getUser is called.
504
     *
505
     * @param string $uid The database user identifier
506
     * @return bool True if the servlet returned correctly. Else false.
507
     */
508
    public function getTwoFactorInfo($uid)
509
    {
510
        $this->two_factor = null;
511
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
512
        return $this->call('action=getTwoFactorInfo&user_uid=' .
513
            urlencode($uid));
514
    }
515
516
    /**
517
     * setTwoFactorInfo
518
     *
519
     * This method calls the 'setTwoFactorInfo' action of the servlet
520
     * and sets the class member variable associated with the user's
521
     * two-factor info appropriately. If the servlet returns correctly
522
     * (i.e. an HTTP status code of 200), this method returns true.
523
     *
524
     * @param string $uid The database user identifier
525
     * @param string $two_factor (Optional) The two-factor info string.
526
     *        Defaults to empty string.
527
     * @return bool True if the servlet returned correctly. Else false.
528
     */
529
    public function setTwoFactorInfo($uid, $two_factor = '')
530
    {
531
        $this->two_factor = $two_factor;
532
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
533
        return $this->call('action=setTwoFactorInfo&user_uid=' .
534
            urlencode($uid) . '&two_factor=' . urlencode($two_factor));
535
    }
536
537
    /**
538
     * getPortalParameters
539
     *
540
     * This method calls the 'getPortalParameter' action of the servlet
541
     * and sets the class member variables associated with the portal
542
     * parameters appropriately. If the servlet returns correctly (i.e.
543
     * an HTTP status code of 200), this method returns true.
544
     *
545
     * @param string $oauth_token The database OAuth identifier token
546
     * @return bool True if the servlet returned correctly. Else false.
547
     */
548
    public function getPortalParameters($oauth_token)
549
    {
550
        $this->clearPortal();
551
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
552
        return $this->call('action=getPortalParameter&oauth_token=' .
553
            urlencode($oauth_token));
554
    }
555
556
    /**
557
     * getIdps
558
     *
559
     * This method calls the 'getAllIdps' action of the servlet and
560
     * sets the class member array $idp_uris to contain all of the
561
     * Idps in the database, stored in the 'values' of the array.  If
562
     * the servlet returns correctly (i.e. an HTTP status code of 200),
563
     * this method returns true.
564
     *
565
     * @return bool True if the servlet returned correctly. Else false.
566
     */
567
    public function getIdps()
568
    {
569
        $this->clearIdps();
570
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
571
        return $this->call('action=getAllIdps');
572
    }
573
574
    /**
575
     * setIdps
576
     *
577
     * This method calls the 'setAllIdps' action of the servlet using
578
     * the class memeber array $idp_uris as the source for the Idps to
579
     * be stored to the database.  Note that if this array is empty,
580
     * an error code will be returned in the status since at least one
581
     * IdP should be saved to the database.  If you want to pass an
582
     * array of Idps to be saved, see the setIdpsFromKeys($array) and
583
     * setIdpsFromValues($array) methods.  If the servlet returns
584
     * correctly (i.e. an HTTP status code of 200), this method
585
     * returns true.
586
     *
587
     * @return bool True if the servlet returned correctly. Else false.
588
     */
589
    public function setIdps()
590
    {
591
        $retval = false;
592
        $this->setDBServiceURL(static::DEFAULTDBSERVICEURL);
593
        $idpcount = count($this->idp_uids);
594
        $idpidx = 0;
595
        if ($idpcount > 0) {
596
            // Loop through the idp_uids in chunks of 50 to deal
597
            // with query parameter limit of http browsers/servers.
598
            while ($idpidx < $idpcount) { // Loop through all IdPs
599
                $fiftyidx = 0;
600
                $idplist = '';
601
                while (($fiftyidx < 50) && // Send 50 IdPs at a time
602
                       ($idpidx < $idpcount)) {
603
                    $idplist .=  '&idp_uid=' .
604
                                 urlencode($this->idp_uids[$idpidx]);
605
                    $fiftyidx++;
606
                    $idpidx++;
607
                }
608
                $cmd = 'action=setAllIdps' . $idplist;
609
                $retval = $this->call($cmd);
610
            }
611
        }
612
        return $retval;
613
    }
614
615
    /**
616
     * setIdpsFromKeys
617
     *
618
     * This is a convenience method which calls setIdps using a
619
     * passed-in array of IdPs stored as the keys of the array.  It
620
     * first sets the class member array $idp_uids appropriately and
621
     * then calls the setIdps() method. If the servlet returns
622
     * correctly (i.e. an HTTP status code of 200), this method
623
     * returns true.  See also setIdpsFromValues().
624
     *
625
     * @param array $idps An array of IdPs to be saved, stored in the
626
     *       'keys' of the array.
627
     * @return bool True if the servlet returned correctly. Else false.
628
     */
629
    public function setIdpsFromKeys($idps)
630
    {
631
        $this->clearIdps();
632
        foreach ($idps as $key => $value) {
633
            $this->idp_uids[] = $key;
634
        }
635
        return $this->setIdps();
636
    }
637
638
    /**
639
     * setIdpsFromValues
640
     *
641
     * This is a convenience method which calls setIdps using a
642
     * passed-in array of IdPs stored as the values of the array.  It
643
     * first sets the class member array $idp_uids appropriately and
644
     * then calls the setIdps() method. If the servlet returns
645
     * correctly (i.e. an HTTP status code of 200), this method
646
     * returns true.  See also setIdpsFromKeys().
647
     *
648
     * @param array $idps An array of IdPs to be saved, stored in the
649
     *        'values' of the array.
650
     * @return bool True if the servlet returned correctly. Else false.
651
     */
652
    public function setIdpsFromValues($idps)
653
    {
654
        $this->clearIdps();
655
        foreach ($idps as $value) {
656
            $this->idp_uids[] = $value;
657
        }
658
        return $this->setIdps();
659
    }
660
661
    /**
662
     * getClient
663
     *
664
     * This method calls the 'getClient' action of the Oauth 2.0
665
     * servlet and sets the class member variables associated with
666
     * client info appropriately.  If the servlet returns correctly
667
     * (i.e. an HTTP status code of 200), this method returns true.
668
     *
669
     * @param string $cid The Oauth 2.0 Client ID (client_id).
670
     * @return bool True if the servlet returned correctly. Else false.
671
     */
672
    public function getClient($cid)
673
    {
674
        $this->clearClient();
675
        $this->setDBServiceURL(static::OAUTH2DBSERVICEURL);
676
        return $this->call('action=getClient&client_id=' .
677
            urlencode($cid));
678
    }
679
680
    /**
681
     * setTransactionState
682
     *
683
     * This method calls the 'setTransactionState' action of the Oauth
684
     * 2.0 servlet to associate the Oauth 2.0 'code' with the database
685
     * user UID. This is necessary for the Oauth 2.0 server to be able
686
     * to return information about the user (name, email address) as
687
     * well as return a certificate for the user. If the servlet
688
     * returns correctly (i.e. an HTTP status code of 200), this method
689
     * returns true. Check the 'status' return value to verify that
690
     * the transaction state was set successfully.
691
     *
692
     * @param string $code The 'code' as returned by the OAuth 2.0 server.
693
     * @param string $uid The database user UID.
694
     * @param int The Unix timestamp of the user authentication.
695
     * @param string $loa (Optional) The Level of Assurance: '' = basic,
696
     *        'openid' =  OpenID Connect (e.g., Google),
697
     *        'http://incommonfederation.org/assurance/silver' = silver
698
     * @param string $myproxyinfo (Optional) the 'info:...' string to be
699
     *        passed to MyProxy.
700
     * @return bool True if the servlet returned correctly. Else false.
701
     */
702
    public function setTransactionState(
703
        $code,
704
        $uid,
705
        $authntime,
706
        $loa = '',
707
        $myproxyinfo = ''
708
    ) {
709
        $this->setDBServiceURL(static::OAUTH2DBSERVICEURL);
710
        return $this->call(
711
            'action=setTransactionState' .
712
            '&code=' . urlencode($code) .
713
            '&user_uid=' . urlencode($uid) .
714
            '&auth_time=' . urlencode($authntime) .
715
            '&loa=' . urlencode($loa) .
716
            ((strlen($myproxyinfo) > 0) ?
717
                ('&cilogon_info=' . urlencode($myproxyinfo)) : '')
718
        );
719
    }
720
721
    /**
722
     * call
723
     *
724
     * This method does the brunt of the work for calling the
725
     * dbService servlet.  The single parameter is a string of
726
     * 'key1=value1&key2=value2&...' containing all of the parameters
727
     * for the dbService.  If the servlet returns an HTTP status code
728
     * of 200, then this method will return true.  It parses the return
729
     * output for various 'key=value' lines and stores then in the
730
     * appropriate member variables, urldecoded of course.
731
     *
732
     * @param string $params A string containing 'key=value' pairs,
733
     *        separated by ampersands ('&') as appropriate for passing to a
734
     *        URL for a GET query.
735
     * @return bool True if the servlet returned correctly. Else false.
736
     */
737
    public function call($params)
738
    {
739
        $success = false;
740
741
        $attr_json = '';
742
        $ch = curl_init();
743
        if ($ch !== false) {
744
            $url = $this->getDBServiceURL() . '?' . $params;
745
            curl_setopt($ch, CURLOPT_URL, $url);
746
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
747
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
748
            $output = curl_exec($ch);
749
            if (curl_errno($ch)) { // Send alert on curl errors
750
                Util::sendErrorAlert(
751
                    'cUrl Error',
752
                    'cUrl Error    = ' . curl_error($ch) . "\n" .
753
                    "URL Accessed  = $url"
754
                );
755
            }
756
            if (!empty($output)) {
757
                $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
758
                if ($httpcode == 200) {
759
                    $success = true;
760
                    if (preg_match('/status=([^\r\n]+)/', $output, $match)) {
761
                        $this->status = (int)(urldecode($match[1]));
762
                    }
763
                    if (preg_match('/user_uid=([^\r\n]+)/', $output, $match)) {
764
                        $this->user_uid = urldecode($match[1]);
765
                    }
766
                    if (preg_match('/remote_user=([^\r\n]+)/', $output, $match)) {
767
                        $this->remote_user = urldecode($match[1]);
768
                    }
769
                    if (preg_match('/idp=([^\r\n]+)/', $output, $match)) {
770
                        $this->idp = urldecode($match[1]);
771
                    }
772
                    if (preg_match('/idp_display_name=([^\r\n]+)/', $output, $match)) {
773
                        $this->idp_display_name = urldecode($match[1]);
774
                    }
775
                    if (preg_match('/first_name=([^\r\n]+)/', $output, $match)) {
776
                        $this->first_name = urldecode($match[1]);
777
                    }
778
                    if (preg_match('/last_name=([^\r\n]+)/', $output, $match)) {
779
                        $this->last_name = urldecode($match[1]);
780
                    }
781
                    if (preg_match('/display_name=([^\r\n]+)/', $output, $match)) {
782
                        $this->display_name = urldecode($match[1]);
783
                    }
784
                    if (preg_match('/email=([^\r\n]+)/', $output, $match)) {
785
                        $this->email = urldecode($match[1]);
786
                    }
787
                    if (preg_match('/distinguished_name=([^\r\n]+)/', $output, $match)) {
788
                        $this->distinguished_name = urldecode($match[1]);
789
                    }
790
                    if (preg_match('/eppn=([^\r\n]+)/', $output, $match)) {
791
                        $this->eppn = urldecode($match[1]);
792
                    }
793
                    if (preg_match('/eptid=([^\r\n]+)/', $output, $match)) {
794
                        $this->eptid = urldecode($match[1]);
795
                    }
796
                    if (preg_match('/open_id=([^\r\n]+)/', $output, $match)) {
797
                        $this->open_id = urldecode($match[1]);
798
                    }
799
                    if (preg_match('/oidc=([^\r\n]+)/', $output, $match)) {
800
                        $this->oidc = urldecode($match[1]);
801
                    }
802
                    if (preg_match('/affiliation=([^\r\n]+)/', $output, $match)) {
803
                        $this->affiliation = urldecode($match[1]);
804
                    }
805
                    if (preg_match('/ou=([^\r\n]+)/', $output, $match)) {
806
                        $this->ou = urldecode($match[1]);
807
                    }
808
                    if (preg_match('/attr_json=([^\r\n]+)/', $output, $match)) {
809
                        $attr_json = urldecode($match[1]);
810
                    }
811
                    if (preg_match('/serial_string=([^\r\n]+)/', $output, $match)) {
812
                        $this->serial_string = urldecode($match[1]);
813
                    }
814
                    if (preg_match('/create_time=([^\r\n]+)/', $output, $match)) {
815
                        $this->create_time = urldecode($match[1]);
816
                    }
817
                    if (preg_match('/oauth_token=([^\r\n]+)/', $output, $match)) {
818
                        $this->oauth_token = urldecode($match[1]);
819
                    }
820
                    if (preg_match('/cilogon_callback=([^\r\n]+)/', $output, $match)) {
821
                        $this->cilogon_callback = urldecode($match[1]);
822
                    }
823
                    if (preg_match('/cilogon_success=([^\r\n]+)/', $output, $match)) {
824
                        $this->cilogon_success = urldecode($match[1]);
825
                    }
826
                    if (preg_match('/cilogon_failure=([^\r\n]+)/', $output, $match)) {
827
                        $this->cilogon_failure = urldecode($match[1]);
828
                    }
829
                    if (preg_match('/cilogon_portal_name=([^\r\n]+)/', $output, $match)) {
830
                        $this->cilogon_portal_name = urldecode($match[1]);
831
                    }
832
                    if (preg_match('/two_factor=([^\r\n]+)/', $output, $match)) {
833
                        $this->two_factor = urldecode($match[1]);
834
                    }
835
                    if (preg_match_all('/idp_uid=([^\r\n]+)/', $output, $match)) {
836
                        foreach ($match[1] as $value) {
837
                            $this->idp_uids[] = urldecode($value);
838
                        }
839
                    }
840
                    if (preg_match('/client_name=([^\r\n]+)/', $output, $match)) {
841
                        $this->client_name = urldecode($match[1]);
842
                    }
843
                    if (preg_match('/client_id=([^\r\n]+)/', $output, $match)) {
844
                        $this->client_id = urldecode($match[1]);
845
                    }
846
                    if (preg_match('/client_home_uri=([^\r\n]+)/', $output, $match)) {
847
                        $this->client_home_uri = urldecode($match[1]);
848
                    }
849
                    if (preg_match('/client_callback_uris=([^\r\n]+)/', $output, $match)) {
850
                        $this->client_callback_uris = explode(urldecode($match[1]), ',');
851
                    }
852
                }
853
            }
854
            curl_close($ch);
855
        }
856
857
        // Convert $attr_json into array and extract elements into class members
858
        if (strlen($attr_json) > 0) {
859
            $attr_arr = json_decode($attr_json, true);
860
            if (!is_null($attr_arr)) {
861
                if (isset($attr_arr['member_of'])) {
862
                    $this->member_of = $attr_arr['member_of'];
863
                }
864
            }
865
        }
866
867
        return $success;
868
    }
869
870
    /**
871
     * dump
872
     *
873
     * This is a convenience method which prints out all of the
874
     * non-null / non-empty member variables to stdout.
875
     */
876
    public function dump()
877
    {
878
        if (!is_null($this->status)) {
879
            echo "status=$this->status (" .
880
            array_search($this->status, static::$STATUS) . ")\n";
881
        }
882
        if (!is_null($this->user_uid)) {
883
            echo "user_uid=$this->user_uid\n";
884
        }
885
        if (!is_null($this->remote_user)) {
886
            echo "remote_user=$this->remote_user\n";
887
        }
888
        if (!is_null($this->idp)) {
889
            echo "idp=$this->idp\n";
890
        }
891
        if (!is_null($this->idp_display_name)) {
892
            echo "idp_display_name=$this->idp_display_name\n";
893
        }
894
        if (!is_null($this->first_name)) {
895
            echo "first_name=$this->first_name\n";
896
        }
897
        if (!is_null($this->last_name)) {
898
            echo "last_name=$this->last_name\n";
899
        }
900
        if (!is_null($this->display_name)) {
901
            echo "display_name=$this->display_name\n";
902
        }
903
        if (!is_null($this->email)) {
904
            echo "email=$this->email\n";
905
        }
906
        if (!is_null($this->distinguished_name)) {
907
            echo "distinguished_name=$this->distinguished_name\n";
908
        }
909
        if (!is_null($this->eppn)) {
910
            echo "eppn=$this->eppn\n";
911
        }
912
        if (!is_null($this->eptid)) {
913
            echo "eptid=$this->eptid\n";
914
        }
915
        if (!is_null($this->open_id)) {
916
            echo "open_id=$this->open_id\n";
917
        }
918
        if (!is_null($this->oidc)) {
919
            echo "oidc=$this->oidc\n";
920
        }
921
        if (!is_null($this->affiliation)) {
922
            echo "affiliation=$this->affiliation\n";
923
        }
924
        if (!is_null($this->ou)) {
925
            echo "ou=$this->ou\n";
926
        }
927
        if (!is_null($this->member_of)) {
928
            echo "member_of=$this->member_of\n";
929
        }
930
        if (!is_null($this->serial_string)) {
931
            echo "serial_string=$this->serial_string\n";
932
        }
933
        if (!is_null($this->create_time)) {
934
            echo "create_time=$this->create_time\n";
935
        }
936
        if (!is_null($this->oauth_token)) {
937
            echo "oauth_token=$this->oauth_token\n";
938
        }
939
        if (!is_null($this->cilogon_callback)) {
940
            echo "cilogon_callback=$this->cilogon_callback\n";
941
        }
942
        if (!is_null($this->cilogon_success)) {
943
            echo "cilogon_success=$this->cilogon_success\n";
944
        }
945
        if (!is_null($this->cilogon_failure)) {
946
            echo "cilogon_failure=$this->cilogon_failure\n";
947
        }
948
        if (!is_null($this->cilogon_portal_name)) {
949
            echo "cilogon_portal_name=$this->cilogon_portal_name\n";
950
        }
951
        if (!is_null($this->two_factor)) {
952
            echo "two_factor=$this->two_factor\n";
953
        }
954
        if (count($this->idp_uids) > 0) {
955
            uasort($this->idp_uids, 'strcasecmp');
956
            echo "idp_uids={\n";
957
            foreach ($this->idp_uids as $value) {
958
                echo "    $value\n";
959
            }
960
            echo "}\n";
961
        }
962
        if (!is_null($this->client_name)) {
963
            echo "client_name=$this->client_name\n";
964
        }
965
        if (!is_null($this->client_id)) {
966
            echo "client_id=$this->client_id\n";
967
        }
968
        if (!is_null($this->client_home_uri)) {
969
            echo "client_home_uri=$this->client_home_uri\n";
970
        }
971
        if (count($this->client_callback_uris) > 0) {
972
            uasort($this->client_callback_uris, 'strcasecmp');
973
            echo "client_callback_uris={\n";
974
            foreach ($this->client_callback_uris as $value) {
975
                echo "    $value\n";
976
            }
977
            echo "}\n";
978
        }
979
    }
980
}
981