getRequest(); $code = $request->getStr('code'); $client_phid = $request->getStr('client_id'); $client_secret = $request->getStr('client_secret'); $response = new PhabricatorOAuthResponse(); if (!$code) { return $response->setMalformed( 'Required parameter code missing.' ); } if (!$client_phid) { return $response->setMalformed( 'Required parameter client_id missing.' ); } if (!$client_secret) { return $response->setMalformed( 'Required parameter client_secret missing.' ); } $auth_code = id(new PhabricatorOAuthServerAuthorizationCode()) ->loadOneWhere('code = %s', $code); if (!$auth_code) { return $response->setNotFound( 'Authorization code '.$code.' not found.' ); } $user = id(new PhabricatorUser()) ->loadOneWhere('phid = %s', $auth_code->getUserPHID()); $server = new PhabricatorOAuthServer($user); $test_code = new PhabricatorOAuthServerAuthorizationCode(); $test_code->setClientSecret($client_secret); $test_code->setClientPHID($client_phid); $is_good_code = $server->validateAuthorizationCode($auth_code, $test_code); if (!$is_good_code) { return $response->setMalformed( 'Invalid authorization code '.$code.'.' ); } $client = id(new PhabricatorOAuthServerClient()) ->loadOneWhere('phid = %s', $client_phid); if (!$client) { return $response->setNotFound( 'Client with client_id '.$client_phid.' not found.' ); } $scope = AphrontWriteGuard::beginScopedUnguardedWrites(); $access_token = $server->generateAccessToken($client); if ($access_token) { $auth_code->delete(); $result = array('access_token' => $access_token->getToken()); return $response->setContent($result); } return $response->setMalformed('Request is malformed in some way.'); } }