|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace bSecure; |
|
4
|
|
|
use bSecure\Helpers\Constant; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class SSO. |
|
8
|
|
|
*/ |
|
9
|
|
|
class SSO |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var string The state to be used to verify successful authorization callback. */ |
|
12
|
|
|
public static $state = null; |
|
13
|
|
|
public static $stateDefinition = false; |
|
14
|
|
|
/** @var string The authCode to be used for receiving customer profile after successful client authorization. */ |
|
15
|
|
|
public static $authCode = null; |
|
16
|
|
|
public static $authCodeDefinition = false; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string The scope is to be used for authorization callback. */ |
|
19
|
|
|
private static $scope = Constant::SCOPE; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string The response_type is to be used for authorization callback. */ |
|
22
|
|
|
private static $response_type = Constant::RESPONSE_TYPE; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Sets the state to be used to verify successful authorization callback. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $state |
|
28
|
|
|
*/ |
|
29
|
|
|
private static function setState($state) |
|
30
|
|
|
{ |
|
31
|
|
|
self::$stateDefinition =true; |
|
32
|
|
|
self::$state = $state; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Sets the authCode to be used for receiving customer profile after successful client authorization. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $authCode |
|
39
|
|
|
*/ |
|
40
|
|
|
private static function setAuthCode($authCode) |
|
41
|
|
|
{ |
|
42
|
|
|
self::$authCodeDefinition =true; |
|
43
|
|
|
self::$authCode = $authCode; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Sets the authorization request link to be used for sso requests. |
|
48
|
|
|
* |
|
49
|
|
|
*/ |
|
50
|
|
|
private static function setAuthenticationLink() |
|
51
|
|
|
{ |
|
52
|
|
|
$clientId = bSecure::getClientId(); |
|
53
|
|
|
$scope = self::$scope; |
|
54
|
|
|
$response_type = self::$response_type; |
|
55
|
|
|
$state = self::$state; |
|
56
|
|
|
return bSecure::$loginBase.'?client_id='.$clientId.'&scope='.$scope.'&response_type='.$response_type.'&state='.$state; |
|
57
|
|
|
} |
|
58
|
|
|
/** |
|
59
|
|
|
* Sets the authentication request payload. |
|
60
|
|
|
* |
|
61
|
|
|
*/ |
|
62
|
|
|
private static function setAuthenticationPayload() |
|
63
|
|
|
{ |
|
64
|
|
|
$clientId = bSecure::getClientId(); |
|
65
|
|
|
if($clientId == null) |
|
66
|
|
|
{ |
|
67
|
|
|
$msg = 'No charges provided. (HINT: set your sub_total, discount and total amount using ' |
|
68
|
|
|
. '"bSecure::setCharges(<ARRAY>). See"' |
|
69
|
|
|
. Constant::DOCUMENTATION_LINK.' for details, ' |
|
70
|
|
|
. 'or email '.Constant::SUPPORT_EMAIL.' if you have any questions.'; |
|
71
|
|
|
throw new Exception\UnexpectedValueException($msg); |
|
72
|
|
|
} |
|
73
|
|
|
return [ |
|
74
|
|
|
"client_id" => $clientId, |
|
75
|
|
|
"scope" => self::$scope, |
|
76
|
|
|
"response_type" => self::$response_type, |
|
77
|
|
|
"state" => self::$state |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return array The customer object |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function customerProfile($authCode) |
|
85
|
|
|
{ |
|
86
|
|
|
self::setAuthCode($authCode); |
|
87
|
|
|
if(!self::$authCodeDefinition) |
|
88
|
|
|
{ |
|
89
|
|
|
$msg = 'No charges provided. (HINT: set your sub_total, discount and total amount using ' |
|
90
|
|
|
. '"bSecure::setCharges(<ARRAY>). See"' |
|
91
|
|
|
. Constant::DOCUMENTATION_LINK.' for details, ' |
|
92
|
|
|
. 'or email '.Constant::SUPPORT_EMAIL.' if you have any questions.'; |
|
93
|
|
|
throw new Exception\UnexpectedValueException($msg); |
|
94
|
|
|
} |
|
95
|
|
|
return SSOController::customerProfile([ |
|
|
|
|
|
|
96
|
|
|
"code" => $authCode, |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string the authentication request weblink used for sso requests |
|
103
|
|
|
* |
|
104
|
|
|
* @throws \bSecure\Exception\UnexpectedValueException if the request fails |
|
105
|
|
|
* |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function clientAuthenticate($state) |
|
108
|
|
|
{ |
|
109
|
|
|
self::setState($state); |
|
110
|
|
|
if(!self::$stateDefinition) |
|
111
|
|
|
{ |
|
112
|
|
|
$msg = 'No charges provided. (HINT: set your sub_total, discount and total amount using ' |
|
113
|
|
|
. '"bSecure::setCharges(<ARRAY>). See"' |
|
114
|
|
|
. Constant::DOCUMENTATION_LINK.' for details, ' |
|
115
|
|
|
. 'or email '.Constant::SUPPORT_EMAIL.' if you have any questions.'; |
|
116
|
|
|
throw new Exception\UnexpectedValueException($msg); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
else{ |
|
120
|
|
|
self::setAuthenticationPayload(); |
|
121
|
|
|
return self::setAuthenticationLink(); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |