Compare commits
11 Commits
f9e674591a
...
f7ca7079b1
Author | SHA1 | Date | |
---|---|---|---|
|
f7ca7079b1 | ||
|
d5a64ef246 | ||
|
f9a7767e2a | ||
|
95bc222c56 | ||
|
3756ab2851 | ||
|
e840500c8a | ||
|
0a0d4f0b17 | ||
|
6d9500207e | ||
|
f10a883cc8 | ||
|
019c361236 | ||
|
31be20921d |
45
README.md
45
README.md
|
@ -5,17 +5,48 @@
|
|||
|
||||
|
||||
|
||||
执行
|
||||
|
||||
|
||||
|
||||
### 配置步骤
|
||||
|
||||
1. 找侯小贝配置登录auth信息 ***AppId*** ***AppSecret*** ***guard***
|
||||
2. 执行
|
||||
```bash
|
||||
composer config repositories.php-auth-client vcs git@git.int.haowumc.com:composer/php-auth-client.git
|
||||
composer require paidian/php-auth-client
|
||||
composer require paidian/auth-client
|
||||
```
|
||||
|
||||
### 服务端代码使用
|
||||
|
||||
### 代码中启用
|
||||
|
||||
* 注册服务
|
||||
|
||||
1. 删除路由验证用户登录信息中间件,如无请忽略。
|
||||
2. 删除代码中验证权限代码 `$this->middleware(CheckRole::class);` 如无请忽略。
|
||||
3. 获取登录者信息由 `$this->user = app('request')->user('auth');` 变更为 `$this->user`
|
||||
4. 给前端提供获取当前登录者信息接口, 如有请忽略
|
||||
5. 在需要验证登录信息的控制器中配置如下代码(ERP项目为例)
|
||||
```php
|
||||
$app->register(PdAuth\PdAuthServiceProvider::class);
|
||||
use \PdAuth\Controller;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
//这里配置的是ERP对应的guard
|
||||
$guard = "erp";
|
||||
$this->auth($guard);
|
||||
}
|
||||
```
|
||||
####获取当前登录用户信息
|
||||
```php
|
||||
$this->user
|
||||
```
|
||||
|
||||
#####上线需配置ENV 上线操作者配置
|
||||
```env
|
||||
RPC_AUTH_URI=http://auth.in.haowumc.com
|
||||
AUTH_ERP_SECRET=123456
|
||||
```
|
||||
|
||||
###前端代码使用
|
||||
1. 获取登录者信息 未登录 http code 返回 401和登录地址 ___客户端需重新定义redirect地址___
|
||||
2. 用户扫码返回 ***pd_code*** ***app_id***
|
||||
3. 获取token地址 `api/auth/token.json` 请求方式:get 参数: ***pd_code*** ***app_id***
|
||||
4. 获取退出登录地址 `/api/auth/logout` 请求方式:get 参数:无
|
|
@ -30,5 +30,13 @@ return [
|
|||
'id' => '100007',
|
||||
'secret' => env('AUTH_PAYMENT_SECRET','123456'),
|
||||
],
|
||||
'xiaoke' => [
|
||||
'id' => '100005',
|
||||
'secret' => env('AUTH_XIAOKE_SECRET','123456'),
|
||||
],
|
||||
'finance' => [
|
||||
'id' => '100003',
|
||||
'secret' => env('AUTH_FINANCE_SECRET','123456'),
|
||||
],
|
||||
],
|
||||
];
|
15
src/Auth.php
15
src/Auth.php
|
@ -97,6 +97,21 @@ class Auth
|
|||
$this->id = $this->config['apps']['op']['id'];
|
||||
$this->secret = $this->config['apps']['op']['secret'];
|
||||
break;
|
||||
case 'payment':
|
||||
case 'paymeny_api':
|
||||
$this->id = $this->config['apps']['payment']['id'];
|
||||
$this->secret = $this->config['apps']['payment']['secret'];
|
||||
break;
|
||||
case 'xiaoke':
|
||||
case 'xiaoke_api':
|
||||
$this->id = $this->config['apps']['xiaoke']['id'];
|
||||
$this->secret = $this->config['apps']['xiaoke']['secret'];
|
||||
break;
|
||||
case 'finance':
|
||||
$this->id = $this->config['apps']['finance']['id'];
|
||||
$this->secret = $this->config['apps']['finance']['secret'];
|
||||
break;
|
||||
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
|
|
@ -41,8 +41,11 @@ class Authenticate
|
|||
{
|
||||
//登录状态检测
|
||||
if ($this->auth->guard($guard)->guest()) {
|
||||
$redirect = $request->input('redirect', $request->header('referer'));
|
||||
// if ($request->isXmlHttpRequest()) {
|
||||
$redirect = $request->input('redirect');
|
||||
if ($request->isXmlHttpRequest()) {
|
||||
if( $redirect == null ){
|
||||
$redirect = $request->header('referer');
|
||||
}
|
||||
return response()->json([
|
||||
'code' => config('pdauth.code.unauthorized', 401),
|
||||
'msg' => 'Unauthorized',
|
||||
|
@ -50,9 +53,12 @@ class Authenticate
|
|||
'url' => app('pd.auth')->connect($redirect),
|
||||
],
|
||||
],401);
|
||||
// } else {
|
||||
// return redirect(app('pd.auth')->connect($redirect));
|
||||
// }
|
||||
} else {
|
||||
if( $redirect == null ){
|
||||
$redirect = $request->getSchemeAndHttpHost()."/api/auth/token.html";
|
||||
}
|
||||
return redirect(app('pd.auth')->connect($redirect));
|
||||
}
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace PdAuth;
|
||||
|
||||
use Illuminate\Contracts\Encryption\DecryptException;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use PdAuth\Middleware\Authenticate;
|
||||
|
@ -69,6 +70,14 @@ class PdAuthServiceProvider extends ServiceProvider
|
|||
])->withCookie($cookie);
|
||||
});
|
||||
|
||||
$this->app['router']->get('api/auth/token.html', function (Request $request) {
|
||||
$code = $request->input('pd_code');
|
||||
$id = $request->input('app_id');
|
||||
$token = app('pd.auth')->choose(null, $id)->getAccessToken($code);
|
||||
$cookie = new Cookie(Authenticate::CookieName, $token['access_token'], strtotime($token['expired_at']));
|
||||
return RedirectResponse::create('/')->withCookie($cookie);
|
||||
});
|
||||
|
||||
$this->app['router']->get('api/auth/logout', function (Request $request) {
|
||||
$cookie = new Cookie(Authenticate::CookieName, '', time());
|
||||
return response()->json([
|
||||
|
|
Loading…
Reference in New Issue
Block a user