Featured image of post KnpUOAuth2ClientBundleでSign in with Appleに対応する

KnpUOAuth2ClientBundleでSign in with Appleに対応する

Twitter ツイート Hatena Bookmark ブックマーク

KnpUOAuth2ClientBundleで「Sign in with Apple」の対応する際にちょっとハマったのでメモしておきます。
もともとKnpUOAuth2ClientBundleはデフォルトで「Sign in with Apple」に対応しています。
https://github.com/knpuniversity/oauth2-client-bundle/blob/master/README.md#configuration 上記のappleの部分を参考に設定すれば動くと思っていました。

動かない「Sign in with Apple」

ドキュメント通りに設定しても、どうしてもコールバック時にinvalid tokenエラーが発生していました。

CallbackがPOSTで送られてくる問題

通常OAuth2の認証でcallbackはGETとしてリクエストが帰ってきます。
これはFacebookやGoogleなど一般的なOAuth2認証はそうなっています。

しかし、Appleは違います。
POST形式となります。

OAuth2ClientがもともとGETしか想定してない

Callback時にアクセストークンを取得するコードが以下になります。 https://github.com/knpuniversity/oauth2-client-bundle/blob/e0d24f58a3462c0a39033812c2ffbab84f744c66/src/Client/OAuth2Client.php#L95 このコードを見てみるとわかると思いますが、stateの取得がGETパラメータしか対応できないのがわかるかと思います。
(ある意味間違いではないと思いますが)

OAuth2Clientが拡張しづらい問題

OAuth2Clientの実装は基本的にプロパティもメソッドもPrivateになっているので、継承しても機能を使うことができません。
OAuth2ClientInterfaceをimplementsした新しいクライアントを作ればいいのですが、基本的に修正したいコードが1行なのに大量にコピペしなきゃいけないということに…。
おそらくfetchUserFromToken, fetchUser のメソッドだけを拡張する想定で実装されてるのだとは思いますが、そのような表明がコードにないのも残念なポイントです。

どう対応するのか?

対応方法は以下のとおりです。

  1. 新しいAppleClientを作成する
  2. services.ymlに新しいAppleClientを作成するを登録する
  3. CompilerPassを使ってデフォルトのAppleClientを差し替える

1.新しいAppleClientを作成する

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php

namespace App\OAuth2Client;

use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface;
use KnpU\OAuth2ClientBundle\Exception\InvalidStateException;
use KnpU\OAuth2ClientBundle\Exception\MissingAuthorizationCodeException;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class AppleClient implements OAuth2ClientInterface
{
    const OAUTH2_SESSION_STATE_KEY = 'knpu.oauth2_client_state';

    /** @var AbstractProvider */
    private $provider;

    /** @var RequestStack */
    private $requestStack;

    /** @var bool */
    private $isStateless = false;

    public function __construct(AbstractProvider $provider, RequestStack $requestStack)
    {
        $this->provider = $provider;
        $this->requestStack = $requestStack;
    }

    public function setAsStateless(): void
    {
        $this->isStateless = true;
    }

    public function redirect(array $scopes, array $options)
    {
        if (!empty($scopes)) {
            $options['scope'] = $scopes;
        }

        $url = $this->provider->getAuthorizationUrl($options);

        // set the state (unless we're stateless)
        if (!$this->isStateless) {
            $this->getSession()->set(
                self::OAUTH2_SESSION_STATE_KEY,
                $this->provider->getState()
            );
        }

        return new RedirectResponse($url);
    }

    public function getAccessToken()
    {
        if (!$this->isStateless) {
            $expectedState = $this->getSession()->get(self::OAUTH2_SESSION_STATE_KEY);
            $actualState = $this->getCurrentRequest()->request->get('state');
            if (!$actualState || ($actualState !== $expectedState)) {
                throw new InvalidStateException('Invalid state');
            }
        }

        $code = $this->getCurrentRequest()->get('code');

        if (!$code) {
            throw new MissingAuthorizationCodeException('No "code" parameter was found (usually this is a query parameter)!');
        }

        return $this->provider->getAccessToken('authorization_code', [
            'code' => $code,
        ]);
    }

    public function fetchUserFromToken(AccessToken $accessToken)
    {
        return $this->provider->getResourceOwner($accessToken);
    }

    public function fetchUser()
    {
        /** @var AccessToken $token */
        $token = $this->getAccessToken();

        return $this->fetchUserFromToken($token);
    }

    public function getOAuth2Provider()
    {
        return $this->provider;
    }

    /**
     * @return \Symfony\Component\HttpFoundation\Request
     */
    private function getCurrentRequest()
    {
        $request = $this->requestStack->getCurrentRequest();

        if (!$request) {
            throw new \LogicException('There is no "current request", and it is needed to perform this action');
        }

        return $request;
    }

    /**
     * @return SessionInterface
     */
    private function getSession()
    {
        if (!$this->getCurrentRequest()->hasSession()) {
            throw new \LogicException('In order to use "state", you must have a session. Set the OAuth2Client to stateless to avoid state');
        }

        return $this->getCurrentRequest()->getSession();
    }
}

この $actualState = $this->getCurrentRequest()->request->get('state'); を書き換えるためだけにかなりコピペしなきゃいけないのは辛いです・・・。

2. services.ymlに新しいAppleClientを作成するを登録する

autowireが効かないので自分で設定します。

1
2
3
4
    App\OAuth2Client\AppleClient:
        arguments:
            $provider: '@knpu.oauth2.provider.apple'
        public: true

3.CompilerPassを使ってデフォルトのAppleClientを差し替える

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php

namespace App\DependencyInjection\Compiler;

use App\OAuth2Client\AppleClient;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class AppleOAuth2ClientPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container): void
    {
        $definition = $container->getDefinition(AppleClient::class);
        $container->setDefinition('knpu.oauth2.client.apple', $definition);
    }
}

こんな感じでオブジェクトを差し替えます。 あと、CompilerPassをkernelに登録することも忘れないようにしましょう。

1
2
3
4
5
// src/Kernel.php
    protected function build(ContainerBuilder $container)
    {
        $container->addCompilerPass(new AppleOAuth2ClientPass());
    }

最後に

SymfonyはCompilerPassなどを使うことによってDI周りをいろいろと差し替えることができるので便利です。
ただし、これは魔法でもあるので極力は使わないほうがいいかなとも個人的には思っています。

あと、今回 OAuth2Client の実装コードをみて改めて抽象クラスとはなにか?ということも考えさせられてので非常に良かったです。

参考

Sign In with Apple をサーバーサイドで実装するときのポイント

comments powered by Disqus
Built with Hugo
テーマ StackJimmy によって設計されています。