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+ $ https = $ _SERVER ['HTTPS ' ];
30+
31+ $ oauthParams ['redirect_uri ' ] = $ authCfg ['redirect_uri ' ];
32+ if ( isset ($ https ) ) {
33+ $ oauthParams ['redirect_uri ' ] =
34+ str_replace ('http:// ' , 'https:// ' , $ oauthParams ['redirect_uri ' ]);
35+ }
36+
37+ // Step #1 - Get the token
38+ $ curl = curl_init ();
39+ curl_setopt ($ curl , CURLOPT_URL , $ authCfg ['token_url ' ]);
40+ curl_setopt ($ curl , CURLOPT_POST , 1 );
41+ curl_setopt ($ curl , CURLOPT_HTTPHEADER , array ('Accept: application/json ' ));
42+ curl_setopt ($ curl , CURLOPT_POSTFIELDS , http_build_query ($ oauthParams ));
43+ curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
44+ curl_setopt ($ curl , CURLOPT_COOKIESESSION , true );
45+ curl_setopt ($ curl , CURLOPT_SSL_VERIFYPEER , false );
46+ $ result_curl = curl_exec ($ curl );
47+
48+ if ( $ result_curl === false ) {
49+ echo 'Curl error: ' . curl_error ($ curl );
50+ echo '<pre> ' ;
51+ var_dump (curl_getinfo ($ curl ));
52+ echo '</pre> ' ;
53+ return false ;
54+ }
55+ curl_close ($ curl );
56+ $ tokenInfo = json_decode ($ result_curl );
57+
58+ // If token is received start session
59+ if (isset ($ tokenInfo ->access_token )) {
60+
61+ $ tokens = explode ('. ' , $ tokenInfo ->id_token );
62+ if (count ($ tokens ) != 3 )
63+ return false ;
64+
65+ $ base64payload = $ tokens [1 ];
66+
67+ $ payload = json_decode (base64_decode ($ base64payload ));
68+ if ($ payload ==false ){
69+ return false ;
70+ }
71+
72+ $ result ->options = new stdClass ();
73+ $ result ->options ->givenName = $ payload ->name ;
74+ $ result ->options ->familyName = $ payload ->name ;
75+ $ result ->options ->user = $ payload ->email ;
76+ $ result ->options ->auth = 'oauth ' ;
77+ return $ result ;
78+ }
79+ $ result ->status ['msg ' ] = 'An error occurred during getting token ' ;
80+ $ result ->status ['status ' ] = tl::ERROR ;
81+
82+ return $ result ;
83+ }
0 commit comments