33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6-
76declare (strict_types=1 );
87
98namespace Magento \AdminAdobeIms \Model \Authorization ;
109
10+ use Magento \AdminAdobeIms \Api \SaveImsUserInterface ;
1111use Magento \AdminAdobeIms \Exception \AdobeImsAuthorizationException ;
1212use Magento \AdminAdobeIms \Service \AdminLoginProcessService ;
1313use Magento \AdminAdobeIms \Service \AdminReauthProcessService ;
1414use Magento \AdminAdobeIms \Service \ImsConfig ;
1515use Magento \AdobeIms \Exception \AdobeImsOrganizationAuthorizationException ;
16+ use Magento \AdobeImsApi \Api \Data \TokenResponseInterface ;
17+ use Magento \AdobeImsApi \Api \Data \TokenResponseInterfaceFactory ;
1618use Magento \AdobeImsApi \Api \GetProfileInterface ;
1719use Magento \AdobeImsApi \Api \GetTokenInterface ;
1820use Magento \AdobeImsApi \Api \OrganizationMembershipInterface ;
1921use Magento \Framework \App \RequestInterface ;
2022use Magento \Framework \Exception \AuthenticationException ;
21- use Magento \AdminAdobeIms \ Api \ SaveImsUserInterface ;
23+ use Magento \Framework \ Exception \ AuthorizationException ;
2224
2325/**
2426 * Adobe IMS Auth Model for getting Admin Token
2830class AdobeImsAdminTokenUserService
2931{
3032 private const ADOBE_IMS_MODULE_NAME = 'adobe_ims_auth ' ;
33+ private const AUTHORIZATION_METHOD_HEADER_BEARER = 'bearer ' ;
3134
3235 /**
3336 * @var ImsConfig
@@ -64,6 +67,11 @@ class AdobeImsAdminTokenUserService
6467 */
6568 private RequestInterface $ request ;
6669
70+ /**
71+ * @var TokenResponseInterfaceFactory
72+ */
73+ private TokenResponseInterfaceFactory $ tokenResponseFactory ;
74+
6775 /**
6876 * @var SaveImsUserInterface
6977 */
@@ -77,6 +85,7 @@ class AdobeImsAdminTokenUserService
7785 * @param RequestInterface $request
7886 * @param GetTokenInterface $token
7987 * @param GetProfileInterface $profile
88+ * @param TokenResponseInterfaceFactory $tokenResponseFactory
8089 * @param SaveImsUserInterface $saveImsUser
8190 */
8291 public function __construct (
@@ -87,6 +96,7 @@ public function __construct(
8796 RequestInterface $ request ,
8897 GetTokenInterface $ token ,
8998 GetProfileInterface $ profile ,
99+ TokenResponseInterfaceFactory $ tokenResponseFactory ,
90100 SaveImsUserInterface $ saveImsUser
91101 ) {
92102 $ this ->adminImsConfig = $ adminImsConfig ;
@@ -96,6 +106,7 @@ public function __construct(
96106 $ this ->request = $ request ;
97107 $ this ->token = $ token ;
98108 $ this ->profile = $ profile ;
109+ $ this ->tokenResponseFactory = $ tokenResponseFactory ;
99110 $ this ->saveImsUser = $ saveImsUser ;
100111 }
101112
@@ -107,33 +118,23 @@ public function __construct(
107118 * @throws AdobeImsAuthorizationException
108119 * @throws AdobeImsOrganizationAuthorizationException
109120 * @throws AuthenticationException
121+ * @throws AuthorizationException
110122 */
111123 public function processLoginRequest (bool $ isReauthorize = false ): void
112124 {
113- if ($ this ->adminImsConfig ->enabled () && $ this -> request -> getParam ( ' code ' )
125+ if ($ this ->adminImsConfig ->enabled ()
114126 && $ this ->request ->getModuleName () === self ::ADOBE_IMS_MODULE_NAME ) {
115127 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 );
133133 } else {
134- $ this ->saveImsUser ->save ($ profile );
135- $ this ->adminLoginProcessService ->execute ($ tokenResponse , $ profile );
134+ throw new AuthenticationException (__ ('Unable to get Access Token. Please try again. ' ));
136135 }
136+
137+ $ this ->getLoggedIn ($ isReauthorize , $ tokenResponse );
137138 } catch (AdobeImsAuthorizationException $ e ) {
138139 throw new AdobeImsAuthorizationException (
139140 __ ('You don \'t have access to this Commerce instance ' )
@@ -147,4 +148,58 @@ public function processLoginRequest(bool $isReauthorize = false): void
147148 throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
148149 }
149150 }
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+ }
150205}
0 commit comments