@@ -2182,11 +2182,138 @@ public function testCreateOAuth2AccountSession(): void
21822182 ]), [
21832183 'success ' => 'http://localhost/v1/mock/tests/general/oauth2/success ' ,
21842184 'failure ' => 'http://localhost/v1/mock/tests/general/oauth2/failure ' ,
2185+ ], followRedirects: false );
2186+
2187+ $ this ->assertEquals (301 , $ response ['headers ' ]['status-code ' ]);
2188+ $ this ->assertStringStartsWith ('http://localhost/v1/mock/tests/general/oauth2 ' , $ response ['headers ' ]['location ' ]);
2189+
2190+ $ oauthClient = new Client ();
2191+ $ oauthClient ->setEndpoint ('' );
2192+ $ response = $ oauthClient ->call (Client::METHOD_GET , $ response ['headers ' ]['location ' ], followRedirects: false );
2193+
2194+ $ this ->assertEquals (301 , $ response ['headers ' ]['status-code ' ]);
2195+ $ this ->assertStringStartsWith ('http://appwrite:/v1/account/sessions/oauth2/callback/mock/ ' . $ this ->getProject ()['$id ' ] . '?code= ' , $ response ['headers ' ]['location ' ]);
2196+
2197+ $ response = $ oauthClient ->call (Client::METHOD_GET , $ response ['headers ' ]['location ' ], followRedirects: false );
2198+
2199+ $ this ->assertEquals (301 , $ response ['headers ' ]['status-code ' ]);
2200+ $ this ->assertStringStartsWith ('http://appwrite:/v1/account/sessions/oauth2/mock/redirect?code= ' , $ response ['headers ' ]['location ' ]);
2201+
2202+ $ response = $ oauthClient ->call (Client::METHOD_GET , $ response ['headers ' ]['location ' ], followRedirects: false );
2203+
2204+ $ this ->assertEquals (301 , $ response ['headers ' ]['status-code ' ]);
2205+
2206+ $ this ->assertArrayHasKey ('a_session_ ' . $ this ->getProject ()['$id ' ] . '_legacy ' , $ response ['cookies ' ]);
2207+ $ this ->assertArrayHasKey ('a_session_ ' . $ this ->getProject ()['$id ' ], $ response ['cookies ' ]);
2208+
2209+ $ oauthUserCookie = $ response ['cookies ' ]['a_session_ ' . $ this ->getProject ()['$id ' ]];
2210+ $ this ->assertNotEmpty ($ oauthUserCookie );
2211+
2212+ $ response = $ oauthClient ->call (Client::METHOD_GET , $ response ['headers ' ]['location ' ], followRedirects: false );
2213+
2214+ $ this ->assertEquals (200 , $ response ['headers ' ]['status-code ' ]);
2215+ $ this ->assertEquals ('success ' , $ response ['body ' ]['result ' ]);
2216+
2217+ // Ensure user is authenticated
2218+ $ response = $ this ->client ->call (Client::METHOD_GET , '/account ' , [
2219+ 'x-appwrite-project ' => $ this ->getProject ()['$id ' ],
2220+ 'cookie ' => 'a_session_ ' . $ this ->getProject ()['$id ' ] . '= ' . $ oauthUserCookie ,
21852221 ]);
2222+ $ this ->assertEquals (200 , $ response ['headers ' ]['status-code ' ]);
2223+ $ this ->
assertEquals (
'[email protected] ' ,
$ response[
'body ' ][
'email ' ]);
2224+
2225+ $ oauthUserId = $ response ['body ' ]['$id ' ];
2226+ $ this ->assertNotEmpty ($ oauthUserId );
2227+
2228+ // Ensure session looks as expected
2229+ $ response = $ this ->client ->call (Client::METHOD_GET , '/account/sessions/current ' , [
2230+ 'x-appwrite-project ' => $ this ->getProject ()['$id ' ],
2231+ 'cookie ' => 'a_session_ ' . $ this ->getProject ()['$id ' ] . '= ' . $ oauthUserCookie ,
2232+ ]);
2233+ $ this ->assertEquals (200 , $ response ['headers ' ]['status-code ' ]);
2234+ $ this ->assertEquals ($ oauthUserId , $ response ['body ' ]['userId ' ]);
2235+ $ this ->assertEquals ('mock ' , $ response ['body ' ]['provider ' ]);
2236+
2237+ // Same sign-in again, but this time with oauth2 token flow
2238+ $ response = $ this ->client ->call (Client::METHOD_GET , '/account/tokens/oauth2/ ' . $ provider , array_merge ([
2239+ 'origin ' => 'http://localhost ' ,
2240+ 'content-type ' => 'application/json ' ,
2241+ 'x-appwrite-project ' => $ this ->getProject ()['$id ' ],
2242+ ]), [
2243+ 'success ' => 'http://localhost/v1/mock/tests/general/oauth2/success ' ,
2244+ 'failure ' => 'http://localhost/v1/mock/tests/general/oauth2/failure ' ,
2245+ ], followRedirects: false );
2246+
2247+ $ this ->assertEquals (301 , $ response ['headers ' ]['status-code ' ]);
2248+ $ this ->assertStringStartsWith ('http://localhost/v1/mock/tests/general/oauth2 ' , $ response ['headers ' ]['location ' ]);
2249+
2250+ $ oauthClient = new Client ();
2251+ $ oauthClient ->setEndpoint ('' );
2252+ $ response = $ oauthClient ->call (Client::METHOD_GET , $ response ['headers ' ]['location ' ], followRedirects: false );
2253+
2254+ $ this ->assertEquals (301 , $ response ['headers ' ]['status-code ' ]);
2255+ $ this ->assertStringStartsWith ('http://appwrite:/v1/account/sessions/oauth2/callback/mock/ ' . $ this ->getProject ()['$id ' ] . '?code= ' , $ response ['headers ' ]['location ' ]);
2256+
2257+ $ response = $ oauthClient ->call (Client::METHOD_GET , $ response ['headers ' ]['location ' ], followRedirects: false );
2258+
2259+ $ this ->assertEquals (301 , $ response ['headers ' ]['status-code ' ]);
2260+ $ this ->assertStringStartsWith ('http://appwrite:/v1/account/sessions/oauth2/mock/redirect?code= ' , $ response ['headers ' ]['location ' ]);
2261+
2262+ $ response = $ oauthClient ->call (Client::METHOD_GET , $ response ['headers ' ]['location ' ], followRedirects: false );
2263+
2264+ $ this ->assertEquals (301 , $ response ['headers ' ]['status-code ' ]);
2265+ $ this ->assertStringStartsWith ('http://localhost/v1/mock/tests/general/oauth2/success?secret= ' , $ response ['headers ' ]['location ' ]);
2266+
2267+ $ oauthParamsString = \parse_url ($ response ['headers ' ]['location ' ], PHP_URL_QUERY );
2268+ $ oauthParams = [];
2269+ \parse_str ($ oauthParamsString , $ oauthParams );
2270+
2271+ $ this ->assertNotEmpty ($ oauthParams ['secret ' ]);
2272+ $ this ->assertNotEmpty ($ oauthParams ['userId ' ]);
2273+
2274+ $ response = $ oauthClient ->call (Client::METHOD_GET , $ response ['headers ' ]['location ' ], followRedirects: false );
21862275
21872276 $ this ->assertEquals (200 , $ response ['headers ' ]['status-code ' ]);
21882277 $ this ->assertEquals ('success ' , $ response ['body ' ]['result ' ]);
21892278
2279+ // Claim session
2280+ $ response = $ this ->client ->call (Client::METHOD_POST , '/account/sessions/token ' , [
2281+ 'origin ' => 'http://localhost ' ,
2282+ 'content-type ' => 'application/json ' ,
2283+ 'x-appwrite-project ' => $ this ->getProject ()['$id ' ],
2284+ ], [
2285+ 'userId ' => $ oauthParams ['userId ' ],
2286+ 'secret ' => $ oauthParams ['secret ' ],
2287+ ]);
2288+
2289+ $ this ->assertEquals (201 , $ response ['headers ' ]['status-code ' ]);
2290+ $ this ->assertEquals ('mock ' , $ response ['body ' ]['provider ' ]);
2291+
2292+ $ this ->assertArrayHasKey ('a_session_ ' . $ this ->getProject ()['$id ' ] . '_legacy ' , $ response ['cookies ' ]);
2293+ $ this ->assertArrayHasKey ('a_session_ ' . $ this ->getProject ()['$id ' ], $ response ['cookies ' ]);
2294+
2295+ $ oauthUserCookie = $ response ['cookies ' ]['a_session_ ' . $ this ->getProject ()['$id ' ]];
2296+ $ this ->assertNotEmpty ($ oauthUserCookie );
2297+
2298+ $ response = $ this ->client ->call (Client::METHOD_GET , '/account ' , [
2299+ 'x-appwrite-project ' => $ this ->getProject ()['$id ' ],
2300+ 'cookie ' => 'a_session_ ' . $ this ->getProject ()['$id ' ] . '= ' . $ oauthUserCookie ,
2301+ ]);
2302+ $ this ->assertEquals (200 , $ response ['headers ' ]['status-code ' ]);
2303+ $ this ->
assertEquals (
'[email protected] ' ,
$ response[
'body ' ][
'email ' ]);
2304+
2305+ $ oauthUserId = $ response ['body ' ]['$id ' ];
2306+ $ this ->assertNotEmpty ($ oauthUserId );
2307+
2308+ // Ensure session looks as expected
2309+ $ response = $ this ->client ->call (Client::METHOD_GET , '/account/sessions/current ' , [
2310+ 'x-appwrite-project ' => $ this ->getProject ()['$id ' ],
2311+ 'cookie ' => 'a_session_ ' . $ this ->getProject ()['$id ' ] . '= ' . $ oauthUserCookie ,
2312+ ]);
2313+ $ this ->assertEquals (200 , $ response ['headers ' ]['status-code ' ]);
2314+ $ this ->assertEquals ($ oauthUserId , $ response ['body ' ]['userId ' ]);
2315+ $ this ->assertEquals ('mock ' , $ response ['body ' ]['provider ' ]);
2316+
21902317 /**
21912318 * Test for Failure when disabled
21922319 */
0 commit comments