@@ -69,33 +69,23 @@ public function isConnected()
6969 */
7070 public function connect ()
7171 {
72- //when no code param redirect to Microsoft
73- if (! request ()->has ('tenant ' )) {
74- $ url = config ('msgraph.tenantUrlAuthorize ' ).'? ' .http_build_query ([
75- 'client_id ' => config ('msgraph.clientId ' ),
76- 'redirect_uri ' => config ('msgraph.redirectUri ' ),
77- ]);
72+ try {
73+ $ params = [
74+ 'scope ' => 'https://graph.microsoft.com/.default ' ,
75+ 'client_id ' => config ('msgraph.clientId ' ),
76+ 'client_secret ' => config ('msgraph.clientSecret ' ),
77+ 'grant_type ' => 'client_credentials ' ,
78+ ];
7879
79- return redirect ()->away ($ url );
80- } elseif (request ()->has ('tenant ' )) {
81- // With the authorization code, we can retrieve access tokens and other data.
82- try {
83- $ params = [
84- 'scope ' => 'https://graph.microsoft.com/.default ' ,
85- 'client_id ' => config ('msgraph.clientId ' ),
86- 'client_secret ' => config ('msgraph.clientSecret ' ),
87- 'grant_type ' => 'client_credentials '
88- ];
80+ $ token = $ this ->dopost (config ('msgraph.tenantUrlAccessToken ' ), $ params );
8981
90- $ token = $ this ->dopost (config ('msgraph.tenantUrlAccessToken ' ), $ params );
82+ // Store token
83+ return $ this ->storeToken ($ token ->access_token , '' , $ token ->expires_in );
9184
92- $ this ->storeToken ($ token ->access_token , '' , $ token ->expires_in );
9385
94- return redirect (config ('msgraph.msgraphLandingUri ' ));
95- } catch (Exception $ e ) {
96- throw new Exception ($ e ->getMessage ());
97- }
98- }
86+ } catch (Exception $ e ) {
87+ throw new Exception ($ e ->getMessage ());
88+ }
9989 }
10090
10191 /**
@@ -106,37 +96,25 @@ public function connect()
10696 */
10797 public function getAccessToken ($ returnNullNoAccessToken = null )
10898 {
109- //use id if passed otherwise use logged-in user
99+ // Admin token will be stored without user_id
110100 $ token = MsGraphToken::where ('user_id ' , null )->first ();
111101
112102 // Check if tokens exist otherwise run the oauth request
113103 if (! isset ($ token ->access_token )) {
114- //don 't redirect simply return null when no token found with this option
104+ // Don 't request new token, simply return null when no token found with this option
115105 if ($ returnNullNoAccessToken == true ) {
116106 return null ;
117107 }
118-
119- return redirect ( config ( ' msgraph.redirectUri ' )) ;
108+ // Run the oath request and return new token
109+ return $ this -> connect ()-> access_token ;
120110 }
121111
122112 // Check if token is expired
123113 // Get current time + 5 minutes (to allow for time differences)
124114 $ now = time () + 300 ;
125115 if ($ token ->expires <= $ now ) {
126116 // Token is expired (or very close to it) so let's refresh
127-
128- $ params = [
129- 'scope ' => 'https://graph.microsoft.com/.default ' ,
130- 'client_id ' => config ('msgraph.clientId ' ),
131- 'client_secret ' => config ('msgraph.clientSecret ' ),
132- 'grant_type ' => 'client_credentials '
133- ];
134-
135- $ token = $ this ->dopost (config ('msgraph.tenantUrlAccessToken ' ), $ params );
136-
137- $ newToken = $ this ->storeToken ($ token ->access_token , '' , $ token ->expires_in );
138-
139- return $ newToken ->access_token ;
117+ return $ this ->connect ()->access_token ;
140118 } else {
141119 // Token is still valid, just return it
142120 return $ token ->access_token ;
@@ -162,10 +140,11 @@ public function getTokenData()
162140 */
163141 protected function storeToken ($ access_token , $ refresh_token , $ expires )
164142 {
165- //cretate a new record or if the user id exists update record
143+ //Create or update a new record for admin token
166144 return MsGraphToken::updateOrCreate (['user_id ' => null ], [
145+ 'email ' => 'application_token ' , // Placeholder name
167146 'access_token ' => $ access_token ,
168- 'expires ' => $ expires ,
147+ 'expires ' => ( time () + $ expires) ,
169148 'refresh_token ' => $ refresh_token ,
170149 ]);
171150 }
@@ -247,7 +226,7 @@ protected static function dopost($url, $params)
247226 } catch (ClientException $ e ) {
248227 return json_decode (($ e ->getResponse ()->getBody ()->getContents ()));
249228 } catch (Exception $ e ) {
250- return json_decode ($ e ->getResponse (), true );
229+ throw new Exception ($ e ->getMessage () );
251230 }
252231 }
253232
0 commit comments