ba76f0a9fca1503a8af385098576edb040956285
[WP-Plugins/glm-member-db-registrations.git] /
1 <?php
2 namespace PayPal\Auth\Openid;
3 use PayPal\Core\PPConstants;
4 use PayPal\Common\PPApiContext;
5 use PayPal\Core\PPConstants;
6 class PPOpenIdSession {
7
8         /**
9          * Returns the PayPal URL to which the user must be redirected to
10          * start the authentication / authorization process.
11          *
12          * @param string $redirectUri Uri on merchant website to where
13          *                              the user must be redirected to post paypal login
14          * @param array $scope The access privilges that you are requesting for
15          *                              from the user. Pass empty array for all scopes.
16          *                              See https://developer.paypal.com/webapps/developer/docs/classic/loginwithpaypal/ht_OpenIDConnect/#parameters for more
17          * @param PPApiContext $apiContext Optional API Context
18          */
19         public static function getAuthorizationUrl($redirectUri, $scope, $apiContext=null) {
20
21                 if(is_null($apiContext)) {
22                         $apiContext = new PPApiContext();
23                 }
24                 $config = $apiContext->getConfig();
25
26                 $scope = count($scope) != 0 ? $scope : array('openid', 'profile', 'address', 'email', 'phone', 'https://uri.paypal.com/services/paypalattributes');
27                 if(!in_array('openid', $scope)) {
28                         $scope[] = 'openid';
29                 }
30                 $params = array(
31                                 'client_id' => $config['acct1.ClientId'],
32                                 'response_type' => 'code',
33                                 'scope' => implode(" ", $scope),
34                                 'redirect_uri' => $redirectUri
35                 );
36                 return sprintf("%s/v1/authorize?%s", self::getBaseUrl($config), http_build_query($params));
37         }
38
39
40         /**
41          * Returns the URL to which the user must be redirected to
42          * logout from the OpenID provider (i.e. PayPal)
43          *
44          * @param string $redirectUri Uri on merchant website to where
45          *                              the user must be redirected to post logout
46          * @param string $idToken id_token from the TokenInfo object
47          * @param PayPal/Rest/APIContext $apiContext Optional API Context
48          */
49         public static function getLogoutUrl($redirectUri, $idToken, $apiContext=null) {
50                 
51                 if(is_null($apiContext)) {
52                         $apiContext = new PPApiContext();
53                 }
54                 $config = $apiContext->getConfig();
55                 
56                 PPConstants::OPENID_REDIRECT_LIVE_URL;
57                 $params = array(
58                                 'id_token' => $idToken,
59                                 'redirect_uri' => $redirectUri,
60                                 'logout' => 'true'
61                 );
62                 return sprintf("%s/v1/endsession?%s", self::getBaseUrl($config), http_build_query($params));
63         }
64
65         private static function getBaseUrl($config) {
66
67                 if(array_key_exists('openid.RedirectUri', $config)) {
68                         return $config['openid.RedirectUri'];
69                 } else if (array_key_exists('mode', $config)) {
70                         switch(strtoupper($config['mode'])) {
71                                 case 'SANDBOX':
72                                         return PPConstants::OPENID_REDIRECT_SANDBOX_URL;
73                                 case 'LIVE':
74                                         return PPConstants::OPENID_REDIRECT_LIVE_URL;   
75                         }
76                 }
77                 return ;
78         }
79 }