1+ <?php
2+ /**
3+ * TestLink Open Source Project - http://testlink.sourceforge.net/
4+ * This script is distributed under the GNU General Public License 2 or later.
5+ *
6+ * @filesource dex.php
7+ *
8+ * Dex OAUTH API (authentication)
9+ *
10+ * @internal revisions
11+ * @since 1.9.20
12+ *
13+ */
14+
15+ // Get token
16+ function oauth_get_token ($ authCfg , $ code ) {
17+
18+ $ result = new stdClass ();
19+ $ result ->status = array ('status ' => tl::OK , 'msg ' => null );
20+
21+ // Params to get token
22+ $ oauthParams = array (
23+ 'code ' => $ code ,
24+ 'client_id ' => $ authCfg ['oauth_client_id ' ],
25+ 'client_secret ' => $ authCfg ['oauth_client_secret ' ],
26+ 'grant_type ' => $ authCfg ['oauth_grant_type ' ]
27+ );
28+
29+ $ oauthParams ['redirect_uri ' ] = $ authCfg ['redirect_uri ' ];
30+ if ( isset ($ _SERVER ['HTTPS ' ]) ) {
31+ $ oauthParams ['redirect_uri ' ] =
32+ str_replace ('http:// ' , 'https:// ' , $ oauthParams ['redirect_uri ' ]);
33+ }
34+
35+ // Step #1 - Get the token
36+ $ curl = curl_init ();
37+ curl_setopt ($ curl , CURLOPT_URL , $ authCfg ['token_url ' ]);
38+ curl_setopt ($ curl , CURLOPT_POST , 1 );
39+ curl_setopt ($ curl , CURLOPT_HTTPHEADER , array ('Accept: application/json ' ));
40+ curl_setopt ($ curl , CURLOPT_POSTFIELDS , http_build_query ($ oauthParams ));
41+ curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
42+ curl_setopt ($ curl , CURLOPT_COOKIESESSION , true );
43+ curl_setopt ($ curl , CURLOPT_SSL_VERIFYPEER , false );
44+ $ result_curl = curl_exec ($ curl );
45+
46+ if ( $ result_curl === false ) {
47+ echo 'Curl error: ' . curl_error ($ curl );
48+ echo '<pre> ' ;
49+ var_dump (curl_getinfo ($ curl ));
50+ echo '</pre> ' ;
51+ return false ;
52+ }
53+ curl_close ($ curl );
54+ $ tokenInfo = json_decode ($ result_curl );
55+
56+ // If token is received start session
57+ if (isset ($ tokenInfo ->access_token )) {
58+
59+ $ tokens = explode ('. ' , $ tokenInfo ->id_token );
60+ if (count ($ tokens ) != 3 )
61+ return false ;
62+
63+ $ base64payload = $ tokens [1 ];
64+
65+ $ payload = json_decode (base64_decode ($ base64payload ));
66+ if ($ payload ==false ){
67+ return false ;
68+ }
69+
70+ $ result ->options = new stdClass ();
71+ $ result ->options ->givenName = $ payload ->name ;
72+ $ result ->options ->familyName = $ payload ->name ;
73+ $ result ->options ->user = $ payload ->email ;
74+ $ result ->options ->auth = 'oauth ' ;
75+ return $ result ;
76+ }
77+ $ result ->status ['msg ' ] = 'An error occurred during getting token ' ;
78+ $ result ->status ['status ' ] = tl::ERROR ;
79+
80+ return $ result ;
81+ }
0 commit comments