3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
6
declare (strict_types=1 );
8
7
9
8
namespace Magento \AdminAdobeIms \Model \Authorization ;
13
12
use Magento \AdminAdobeIms \Service \AdminReauthProcessService ;
14
13
use Magento \AdminAdobeIms \Service \ImsConfig ;
15
14
use Magento \AdobeIms \Exception \AdobeImsOrganizationAuthorizationException ;
15
+ use Magento \AdobeImsApi \Api \Data \TokenResponseInterface ;
16
+ use Magento \AdobeImsApi \Api \Data \TokenResponseInterfaceFactory ;
16
17
use Magento \AdobeImsApi \Api \GetProfileInterface ;
17
18
use Magento \AdobeImsApi \Api \GetTokenInterface ;
18
19
use Magento \AdobeImsApi \Api \OrganizationMembershipInterface ;
27
28
class AdobeImsAdminTokenUserService
28
29
{
29
30
private const ADOBE_IMS_MODULE_NAME = 'adobe_ims_auth ' ;
31
+ private const AUTHORIZATION_METHOD_HEADER_BEARER = 'bearer ' ;
30
32
31
33
/**
32
34
* @var ImsConfig
@@ -63,6 +65,11 @@ class AdobeImsAdminTokenUserService
63
65
*/
64
66
private RequestInterface $ request ;
65
67
68
+ /**
69
+ * @var TokenResponseInterfaceFactory
70
+ */
71
+ private $ tokenResponseFactory ;
72
+
66
73
/**
67
74
* @param ImsConfig $adminImsConfig
68
75
* @param OrganizationMembershipInterface $organizationMembership
@@ -71,6 +78,7 @@ class AdobeImsAdminTokenUserService
71
78
* @param RequestInterface $request
72
79
* @param GetTokenInterface $token
73
80
* @param GetProfileInterface $profile
81
+ * @param TokenResponseInterfaceFactory $tokenResponseFactory
74
82
*/
75
83
public function __construct (
76
84
ImsConfig $ adminImsConfig ,
@@ -79,7 +87,8 @@ public function __construct(
79
87
AdminReauthProcessService $ adminReauthProcessService ,
80
88
RequestInterface $ request ,
81
89
GetTokenInterface $ token ,
82
- GetProfileInterface $ profile
90
+ GetProfileInterface $ profile ,
91
+ TokenResponseInterfaceFactory $ tokenResponseFactory
83
92
) {
84
93
$ this ->adminImsConfig = $ adminImsConfig ;
85
94
$ this ->organizationMembership = $ organizationMembership ;
@@ -88,6 +97,7 @@ public function __construct(
88
97
$ this ->request = $ request ;
89
98
$ this ->token = $ token ;
90
99
$ this ->profile = $ profile ;
100
+ $ this ->tokenResponseFactory = $ tokenResponseFactory ;
91
101
}
92
102
93
103
/**
@@ -101,29 +111,19 @@ public function __construct(
101
111
*/
102
112
public function processLoginRequest (bool $ isReauthorize = false ): void
103
113
{
104
- if ($ this ->adminImsConfig ->enabled () && $ this -> request -> getParam ( ' code ' )
114
+ if ($ this ->adminImsConfig ->enabled ()
105
115
&& $ this ->request ->getModuleName () === self ::ADOBE_IMS_MODULE_NAME ) {
106
116
try {
107
- $ code = $ this ->request ->getParam ('code ' );
108
-
109
- //get token from response
110
- $ tokenResponse = $ this ->token ->getTokenResponse ($ code );
111
- $ accessToken = $ tokenResponse ->getAccessToken ();
112
-
113
- //get profile info to check email
114
- $ profile = $ this ->profile ->getProfile ($ accessToken );
115
- if (empty ($ profile ['email ' ])) {
116
- throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
117
- }
118
-
119
- //check membership in organization
120
- $ this ->organizationMembership ->checkOrganizationMembership ($ accessToken );
121
-
122
- if ($ isReauthorize ) {
123
- $ this ->adminReauthProcessService ->execute ($ tokenResponse );
117
+ if ($ this ->request ->getHeader ('Authorization ' )) {
118
+ $ tokenResponse = $ this ->getRequestedToken ();
119
+ } elseif ($ this ->request ->getParam ('code ' )) {
120
+ $ code = $ this ->request ->getParam ('code ' );
121
+ $ tokenResponse = $ this ->token ->getTokenResponse ($ code );
124
122
} else {
125
- $ this -> adminLoginProcessService -> execute ( $ tokenResponse , $ profile );
123
+ throw new AuthenticationException ( __ ( ' Unable to get Access Token. Please try again. ' ) );
126
124
}
125
+
126
+ $ this ->getLoggedIn ($ isReauthorize , $ tokenResponse );
127
127
} catch (AdobeImsAuthorizationException $ e ) {
128
128
throw new AdobeImsAuthorizationException (
129
129
__ ('You don \'t have access to this Commerce instance ' )
@@ -137,4 +137,55 @@ public function processLoginRequest(bool $isReauthorize = false): void
137
137
throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
138
138
}
139
139
}
140
+
141
+ /**
142
+ * Get requested token using Authorization header
143
+ *
144
+ * @return \Magento\AdobeImsApi\Api\Data\TokenResponseInterface
145
+ * @throws AuthenticationException
146
+ */
147
+ private function getRequestedToken ()
148
+ {
149
+ $ authorizationHeaderValue = $ this ->request ->getHeader ('Authorization ' );
150
+ if (!$ authorizationHeaderValue ) {
151
+ throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
152
+ }
153
+
154
+ $ headerPieces = explode (" " , $ authorizationHeaderValue );
155
+ if (count ($ headerPieces ) !== 2 ) {
156
+ throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
157
+ }
158
+
159
+ $ tokenType = strtolower ($ headerPieces [0 ]);
160
+ if ($ tokenType !== self ::AUTHORIZATION_METHOD_HEADER_BEARER ) {
161
+ throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
162
+ }
163
+
164
+ $ tokenResponse ['access_token ' ] = $ headerPieces [1 ];
165
+ return $ this ->tokenResponseFactory ->create (['data ' => $ tokenResponse ]);
166
+ }
167
+
168
+ /**
169
+ * Responsible for logging in to Admin Panel
170
+ *
171
+ * @param bool $isReauthorize
172
+ * @param TokenResponseInterface $tokenResponse
173
+ * @return void
174
+ * @throws AuthenticationException
175
+ */
176
+ private function getLoggedIn (bool $ isReauthorize , TokenResponseInterface $ tokenResponse )
177
+ {
178
+ $ profile = $ this ->profile ->getProfile ($ tokenResponse ->getAccessToken ());
179
+ if (empty ($ profile ['email ' ])) {
180
+ throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
181
+ }
182
+
183
+ $ this ->organizationMembership ->checkOrganizationMembership ($ tokenResponse ->getAccessToken ());
184
+
185
+ if ($ isReauthorize ) {
186
+ $ this ->adminReauthProcessService ->execute ($ tokenResponse );
187
+ } else {
188
+ $ this ->adminLoginProcessService ->execute ($ tokenResponse , $ profile );
189
+ }
190
+ }
140
191
}
0 commit comments