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 ;
10
9
10
+ use Magento \AdminAdobeIms \Api \SaveImsUserInterface ;
11
11
use Magento \AdminAdobeIms \Exception \AdobeImsAuthorizationException ;
12
12
use Magento \AdminAdobeIms \Service \AdminLoginProcessService ;
13
13
use Magento \AdminAdobeIms \Service \AdminReauthProcessService ;
14
14
use Magento \AdminAdobeIms \Service \ImsConfig ;
15
15
use Magento \AdobeIms \Exception \AdobeImsOrganizationAuthorizationException ;
16
+ use Magento \AdobeImsApi \Api \Data \TokenResponseInterface ;
17
+ use Magento \AdobeImsApi \Api \Data \TokenResponseInterfaceFactory ;
16
18
use Magento \AdobeImsApi \Api \GetProfileInterface ;
17
19
use Magento \AdobeImsApi \Api \GetTokenInterface ;
18
20
use Magento \AdobeImsApi \Api \OrganizationMembershipInterface ;
19
21
use Magento \Framework \App \RequestInterface ;
20
22
use Magento \Framework \Exception \AuthenticationException ;
21
- use Magento \AdminAdobeIms \ Api \ SaveImsUserInterface ;
23
+ use Magento \Framework \ Exception \ AuthorizationException ;
22
24
23
25
/**
24
26
* Adobe IMS Auth Model for getting Admin Token
28
30
class AdobeImsAdminTokenUserService
29
31
{
30
32
private const ADOBE_IMS_MODULE_NAME = 'adobe_ims_auth ' ;
33
+ private const AUTHORIZATION_METHOD_HEADER_BEARER = 'bearer ' ;
31
34
32
35
/**
33
36
* @var ImsConfig
@@ -64,6 +67,11 @@ class AdobeImsAdminTokenUserService
64
67
*/
65
68
private RequestInterface $ request ;
66
69
70
+ /**
71
+ * @var TokenResponseInterfaceFactory
72
+ */
73
+ private TokenResponseInterfaceFactory $ tokenResponseFactory ;
74
+
67
75
/**
68
76
* @var SaveImsUserInterface
69
77
*/
@@ -77,6 +85,7 @@ class AdobeImsAdminTokenUserService
77
85
* @param RequestInterface $request
78
86
* @param GetTokenInterface $token
79
87
* @param GetProfileInterface $profile
88
+ * @param TokenResponseInterfaceFactory $tokenResponseFactory
80
89
* @param SaveImsUserInterface $saveImsUser
81
90
*/
82
91
public function __construct (
@@ -87,6 +96,7 @@ public function __construct(
87
96
RequestInterface $ request ,
88
97
GetTokenInterface $ token ,
89
98
GetProfileInterface $ profile ,
99
+ TokenResponseInterfaceFactory $ tokenResponseFactory ,
90
100
SaveImsUserInterface $ saveImsUser
91
101
) {
92
102
$ this ->adminImsConfig = $ adminImsConfig ;
@@ -96,6 +106,7 @@ public function __construct(
96
106
$ this ->request = $ request ;
97
107
$ this ->token = $ token ;
98
108
$ this ->profile = $ profile ;
109
+ $ this ->tokenResponseFactory = $ tokenResponseFactory ;
99
110
$ this ->saveImsUser = $ saveImsUser ;
100
111
}
101
112
@@ -107,33 +118,23 @@ public function __construct(
107
118
* @throws AdobeImsAuthorizationException
108
119
* @throws AdobeImsOrganizationAuthorizationException
109
120
* @throws AuthenticationException
121
+ * @throws AuthorizationException
110
122
*/
111
123
public function processLoginRequest (bool $ isReauthorize = false ): void
112
124
{
113
- if ($ this ->adminImsConfig ->enabled () && $ this -> request -> getParam ( ' code ' )
125
+ if ($ this ->adminImsConfig ->enabled ()
114
126
&& $ this ->request ->getModuleName () === self ::ADOBE_IMS_MODULE_NAME ) {
115
127
try {
116
- $ code = $ this ->request ->getParam ('code ' );
117
-
118
- //get token from response
119
- $ tokenResponse = $ this ->token ->getTokenResponse ($ code );
120
- $ accessToken = $ tokenResponse ->getAccessToken ();
121
-
122
- //get profile info to check email
123
- $ profile = $ this ->profile ->getProfile ($ accessToken );
124
- if (empty ($ profile ['email ' ])) {
125
- throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
126
- }
127
-
128
- //check membership in organization
129
- $ this ->organizationMembership ->checkOrganizationMembership ($ accessToken );
130
-
131
- if ($ isReauthorize ) {
132
- $ this ->adminReauthProcessService ->execute ($ tokenResponse );
128
+ if ($ this ->request ->getHeader ('Authorization ' )) {
129
+ $ tokenResponse = $ this ->getRequestedToken ();
130
+ } elseif ($ this ->request ->getParam ('code ' )) {
131
+ $ code = $ this ->request ->getParam ('code ' );
132
+ $ tokenResponse = $ this ->token ->getTokenResponse ($ code );
133
133
} else {
134
- $ this ->saveImsUser ->save ($ profile );
135
- $ this ->adminLoginProcessService ->execute ($ tokenResponse , $ profile );
134
+ throw new AuthenticationException (__ ('Unable to get Access Token. Please try again. ' ));
136
135
}
136
+
137
+ $ this ->getLoggedIn ($ isReauthorize , $ tokenResponse );
137
138
} catch (AdobeImsAuthorizationException $ e ) {
138
139
throw new AdobeImsAuthorizationException (
139
140
__ ('You don \'t have access to this Commerce instance ' )
@@ -147,4 +148,58 @@ public function processLoginRequest(bool $isReauthorize = false): void
147
148
throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
148
149
}
149
150
}
151
+
152
+ /**
153
+ * Get requested token using Authorization header
154
+ *
155
+ * @return TokenResponseInterface
156
+ * @throws AuthenticationException
157
+ */
158
+ private function getRequestedToken (): TokenResponseInterface
159
+ {
160
+ $ authorizationHeaderValue = $ this ->request ->getHeader ('Authorization ' );
161
+ if (!$ authorizationHeaderValue ) {
162
+ throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
163
+ }
164
+
165
+ $ headerPieces = explode (" " , $ authorizationHeaderValue );
166
+ if (count ($ headerPieces ) !== 2 ) {
167
+ throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
168
+ }
169
+
170
+ $ tokenType = strtolower ($ headerPieces [0 ]);
171
+ if ($ tokenType !== self ::AUTHORIZATION_METHOD_HEADER_BEARER ) {
172
+ throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
173
+ }
174
+
175
+ $ tokenResponse ['access_token ' ] = $ headerPieces [1 ];
176
+ return $ this ->tokenResponseFactory ->create (['data ' => $ tokenResponse ]);
177
+ }
178
+
179
+ /**
180
+ * Responsible for logging in to Admin Panel
181
+ *
182
+ * @param bool $isReauthorize
183
+ * @param TokenResponseInterface $tokenResponse
184
+ * @return void
185
+ * @throws AdobeImsAuthorizationException
186
+ * @throws AuthenticationException
187
+ * @throws AuthorizationException
188
+ */
189
+ private function getLoggedIn (bool $ isReauthorize , TokenResponseInterface $ tokenResponse ): void
190
+ {
191
+ $ profile = $ this ->profile ->getProfile ($ tokenResponse ->getAccessToken ());
192
+ if (empty ($ profile ['email ' ])) {
193
+ throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
194
+ }
195
+
196
+ $ this ->organizationMembership ->checkOrganizationMembership ($ tokenResponse ->getAccessToken ());
197
+
198
+ if ($ isReauthorize ) {
199
+ $ this ->adminReauthProcessService ->execute ($ tokenResponse );
200
+ } else {
201
+ $ this ->saveImsUser ->save ($ profile );
202
+ $ this ->adminLoginProcessService ->execute ($ tokenResponse , $ profile );
203
+ }
204
+ }
150
205
}
0 commit comments