1 <?php
2 namespace PayPal\Api;
3
4
5 use PayPal\Core\PayPalConstants;
6 use PayPal\Rest\ApiContext;
7
8 class OpenIdSession
9 {
10
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 public static function getAuthorizationUrl($redirectUri, $scope, $clientId, $nonce = null, $state = null, $apiContext = null)
27 {
28 $apiContext = $apiContext ? $apiContext : new ApiContext();
29 $config = $apiContext->getConfig();
30
31 if ($apiContext->get($clientId)) {
32 $clientId = $apiContext->get($clientId);
33 }
34
35 $clientId = $clientId ? $clientId : $apiContext->getCredential()->getClientId();
36
37 $scope = count($scope) != 0 ? $scope : array('openid', 'profile', 'address', 'email', 'phone',
38 'https://uri.paypal.com/services/paypalattributes', 'https://uri.paypal.com/services/expresscheckout');
39 if (!in_array('openid', $scope)) {
40 $scope[] = 'openid';
41 }
42
43 $params = array(
44 'client_id' => $clientId,
45 'response_type' => 'code',
46 'scope' => implode(" ", $scope),
47 'redirect_uri' => $redirectUri
48 );
49
50 if ($nonce) {
51 $params['nonce'] = $nonce;
52 }
53 if ($state) {
54 $params['state'] = $state;
55 }
56 return sprintf("%s/signin/authorize?%s", self::getBaseUrl($config), http_build_query($params));
57 }
58
59
60 61 62 63 64 65 66 67 68 69
70 public static function getLogoutUrl($redirectUri, $idToken, $apiContext = null)
71 {
72
73 if (is_null($apiContext)) {
74 $apiContext = new ApiContext();
75 }
76 $config = $apiContext->getConfig();
77
78 $params = array(
79 'id_token' => $idToken,
80 'redirect_uri' => $redirectUri,
81 'logout' => 'true'
82 );
83 return sprintf("%s/webapps/auth/protocol/openidconnect/v1/endsession?%s", self::getBaseUrl($config), http_build_query($params));
84 }
85
86 87 88 89 90 91
92 private static function getBaseUrl($config)
93 {
94
95 if (array_key_exists('openid.RedirectUri', $config)) {
96 return $config['openid.RedirectUri'];
97 } else if (array_key_exists('mode', $config)) {
98 switch (strtoupper($config['mode'])) {
99 case 'SANDBOX':
100 return PayPalConstants::OPENID_REDIRECT_SANDBOX_URL;
101 case 'LIVE':
102 return PayPalConstants::OPENID_REDIRECT_LIVE_URL;
103 }
104 }
105 return null;
106 }
107 }
108