如何将身份验证委派给外部API服务器?

时间:2017-10-04 14:59:40

标签: php laravel authentication laravel-5

我有两个环境:

  • A :基于Laravel的应用程序API服务器
    (我对此拥有完全权限)
  • B A 的外部API服务器(我无权在没有他/她身份验证的情况下访问用户)

users表只有两列,idexternal_user_idid A 创建,external_user_id B 创建。

如果我实施自定义提供程序,我需要遵循两个合同:

<?php

interface UserProvider
{
    public function retrieveById($identifier);
    public function retrieveByToken($identifier, $token);
    public function updateRememberToken(Authenticatable $user, $token);
    public function retrieveByCredentials(array $credentials);
    public function validateCredentials(Authenticatable $user, array $credentials);
}

interface Authenticatable
{
    public function getAuthIdentifierName();
    public function getAuthIdentifier();
    public function getAuthPassword();
    public function getRememberToken();
    public function setRememberToken($value);
    public function getRememberTokenName();
}

但是,有一个非常棘手的问题:如何在验证用户之前调用retrieveByCredentials()

1 个答案:

答案 0 :(得分:0)

  • 在每个方法上抛出BadMethodCallException
  • 绝不使用Auth::attempt()
  • 仅使用Auth::login()

<?php

class MyUserProvider extends Provider implements UserProvider
{
    public function retrieveById($identifier)
    {
        throw new \BadMethodCallException();
    }
    public function retrieveByToken($identifier, $token)
    {
        throw new \BadMethodCallException();
    }
    public function updateRememberToken(Authenticatable $user, $token)
    {
        throw new \BadMethodCallException();
    }
    public function retrieveByCredentials(array $credentials)
    {
        throw new \BadMethodCallException();
    }
    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        throw new \BadMethodCallException();
    }
}

class User extends Model implements Authenticatable
{
    public function getAuthIdentifierName()
    {
        throw new \BadMethodCallException();
    }
    public function getAuthIdentifier()
    {
        throw new \BadMethodCallException();
    }
    public function getAuthPassword()
    {
        throw new \BadMethodCallException();
    }
    public function getRememberToken()
    {
        throw new \BadMethodCallException();
    }
    public function setRememberToken($value)
    {
        throw new \BadMethodCallException();
    }
    public function getRememberTokenName()
    {
        throw new \BadMethodCallException();
    }
}