Open3

Laravel

bz0bz0

laravel-enum

  • valueobject内でenumによるバリデーションに利用
  • 定数が色々なところに散らないことがメリットか
bz0bz0
  • stateless():セッション状態の検証を無効
  • 認証済かを判別するためにパラメータのcodeとstate(セッションのstateとパラメータのstateチェック:stateless()が呼び出されていればチェックしない)を元にgithub認証を行っています。
    • codeは10分の期限つきの一時コード
    • stateはクロスサイトリクエストフォージェリ対策の文字列
  • Socialite::driver('github')->stateless()->user()が何をしている?
    • app/vendor/laravel/socialite/src/Two/AbstractProvider.php / user()
      • $this->hasInvalidState():セッションのstateチェック
      • $this->getAccessTokenResponse($this->getCode()):githubのアクセストークンを取得
        • $this->getCode():パラメータのcodeを取得している
      • $this->getUserByToken:アクセストークンからgithubユーザ情報を取得
      • mapUserToObject:githubユーザ情報を$this->userにセット

https://github.com/login/oauth/authorize?client_id=243b6628f78e41fbccf0&redirect_uri=https%3A%2F%2Flocalhost%2Fauth%2Fgithub%2Fcallback&scope=user%3Aemail%2Cread%3Auser%2Cpublic_repo&response_type=code&state=eTyn2ErlQzHhQUvl9qtQNVSy0dRDVcKFdcERbkuP

app/vendor/laravel/socialite/src/Two/AbstractProvider.php

    public function user()
    {
        if ($this->user) {
            return $this->user;
        }

        if ($this->hasInvalidState()) {
            throw new InvalidStateException;
        }

        $response = $this->getAccessTokenResponse($this->getCode());

        $this->user = $this->mapUserToObject($this->getUserByToken(
            $token = Arr::get($response, 'access_token')
        ));

        return $this->user->setToken($token)
                    ->setRefreshToken(Arr::get($response, 'refresh_token'))
                    ->setExpiresIn(Arr::get($response, 'expires_in'));
    }
    protected function hasInvalidState()
    {
        //stateless()が呼び出されていればfalseが返る
        if ($this->isStateless()) {
            return false;
        }

        //セッションのstateを取得し削除
        $state = $this->request->session()->pull('state');
        //stateが空またはURLパラメータのstateと異なる場合trueを返す
        return empty($state) || $this->request->input('state') !== $state;
    }
    public function getAccessTokenResponse($code)
    {
        //https://github.com/login/oauth/access_tokenへリクエストしアクセストークンを取得する
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
            'headers' => ['Accept' => 'application/json'],
            'form_params' => $this->getTokenFields($code),
        ]);

        return json_decode($response->getBody(), true);
    }
bz0bz0
  • provider :認証方法
  • driver:認証状態の管理方法
    • 多くの場合はセッション認証 (session)
    • セッションを利用できないAPI認証の場合は、トークン認証 (token)
  • middlewareGroup:App\Providers\RouteServiceProviderサービスプロバイダによって、対応するwebおよびapiルートファイル内のルートに自動的に適用される
  • $middleware:すべてのRequestに対して実行されるmiddleware
  • アクセスしたときに作成されるLaravelに関連するCookie
    • XSRF-TOKEN
    • laravel_session
  • クッキーがNext.js側で取得できない
    • httponly属性を指定すると、HTTPテキスト内のスクリプトからCookieをアクセスできなくなる
    • chromeがダメ、firefoxならOKなのはなぜ?
    • remember_web_xxxはconfig/session.phpのhttp_onlyをfalseにしてもhttp_onlyが無効にならない
  • SessionGuard
    • app/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php
    • クッキーのlogin_web_xxxxからUserのIDを取得する
    • 対象クッキー名は'login_'.$this->name.'_'.sha1(static::class)
    • UserのIDがあれば成功、なければ失敗(401)