0

私はsymfony2でモバイルアプリ用のAPIを構築しています。FOSAuthServerBundle をセットアップし、正しく構成しました。

http://blog.tankist.de/blog/2013/07/18/oauth2-explained-part-3-using-oauth2-with-your-bare-hands/

クライアント資格情報を使用して tokan を生成することもできます。また、ユーザーを Outh ログイン ページにリダイレクトすることもできます。認証と承認の部分で立ち往生しています。現在の Web サイトとして、独自のエンティティ メンバーとして、Web サイト ユーザーとして。誰かが FOSAuth を使用した認証と承認プロセスの例を投稿できますか? 私はsymfonyの初心者です。

どんな助けでも大歓迎です。

4

1 に答える 1

0

ポストAPIのカスタムログインアクションを作成しました。ポスト API 承認にサードパーティのバンドルは使用しませんでした。ただし、プロジェクトで次のサードパーティ バンドルを使用して REST API を提供しています。ここでカスタム ログイン アクションを共有しました。参考になるかもしれません。私が使用した次のバンドル: JMSSerializerBundle、FOSRestBundle、NelmioApiDocBundle、NelmioCorsBundle、FOSUserBundle

 <?php
    namespace Yoursite\ApiBundle\Controller;

    use FOS\RestBundle\View\View;
    use Nelmio\ApiDocBundle\Annotation\ApiDoc;
    use FOS\UserBundle\FOSUserEvents;
    use Symfony\Component\HttpFoundation\Request;

    class UserApiController extends Controller
    {

    /**
     * Response header status code
     *
     * @var int
     */
    private $statusCode = 200;

    /**
     * This action is used for user signin through api
     *
     * @ApiDoc(
     *  resource=true,
     *  description="User Post action for signin"
     * )
     *
     * @param $request
     * @return View view instance
     */
    public function postAuthSigninAction(Request $request) {
        $responseArr= array();
        $view = new View();
        $this->statusCode = 200;
        $userData = json_decode($request->getContent(), true);

        $email = $userData['email'];
        $password = $userData['password'];

        $um = $this->get('fos_user.user_manager');
        $user = $um->findUserByUsernameOrEmail($email);

        if (!$user instanceof \YourSite\UserBundle\Entity\User) {
            $responseArr['meta']['error'] = "The email address or password is incorrect.";
            $this->statusCode = 401;
            return $view->create($responseArr, $this->statusCode);
        }

        $encoder_service = $this->get('security.encoder_factory');
        $encoder = $encoder_service->getEncoder($user);
        $encoded_pass = $encoder->encodePassword($password, $user->getSalt());

        if ($encoded_pass != $user->getPassword()) {
            $responseArr['meta']['error'] = "The email address or password is incorrect.";
            $this->statusCode = 401;
            return $view->create($responseArr, $this->statusCode);
        }

        $user->setToken(md5($password.time()));
        $this->getDoctrine()->getManager()->persist($user);
        $this->getDoctrine()->getManager()->flush();
        $responseArr['data'] = $user;
        return $view->create($responseArr, $this->statusCode);
    }
    }
    ?>
于 2015-03-04T08:24:58.840 に答える