顯示具有 phpstorm 標籤的文章。 顯示所有文章
顯示具有 phpstorm 標籤的文章。 顯示所有文章

2022年3月16日 星期三

快速移動工作視窗/分頁

移動視窗到另外一個屏幕

Windows + Shift + Left Arrow - 移動當前視窗到左邊
Windows + Shift + Right Arrow - 移動當前視窗到右邊

Sublime

https://superuser.com/a/971603  Sublime Text, move tabs with keyboard
安裝 MoveTab 插件 
Ctrl+Shift+PgUp - 將當前tab往左移
Ctrl+Shift+PgDn - 將當前tab往右移

Chrome

https://superuser.com/a/730415  Keyboard shortcuts to arrange Chrome tabs on Windows?
安裝 Rearrange Tabs 插件,然後在 chrome://extensions/shortcuts 中設定熱鍵
Ctrl+Shift+PgUp - 將當前tab往左移
Ctrl+Shift+PgDn - 將當前tab往右移


PHPStorm / WebStorm

https://stackoverflow.com/a/54210051  Intellij: Move tab left and right
安裝 TabMover 插件
Control+Alt+Shift+Left Arrow - 往左移動tab
Control+Alt+Shift+Right Arrow - 往右移動tab
Control+Alt+Shift+Up Arrow - 移動tab到第一個
Control+Alt+Shift+Down Arrow - 移動tab到最後一個

Tabby

是的,我放棄了使用 ConEmu64 來當做 git bash 的Tab解決方案,轉向了Tabby 。為了更舒服的工作體驗

安裝

無難度,下載官網中 github 上的的 tabby-1.0.173-setup.exe 安裝檔

配置

Terminal 

Terminal => Startup => 關閉 Restore terminal tabs on app start (因為我們後面要使用 Workspace Manager 插件管理頁簽)

Profiles & connections

在Git-Bash的profile上複製出一個自己的bash(Git-Bash-user),可客製化起始路徑




Hotkeys

Next Tab 新增 Ctrl+PageDown
Previous Tab 新增 Ctrl+PageUp

Tab 1 新增 Ctr+1
Tab 2 新增 Ctr+2
Tab 3 新增 Ctr+3
Tab 4 新增 Ctr+4
Tab 5 新增 Ctr+5
Tab 6 新增 Ctr+6
Tab 7 新增 Ctr+7
Tab 8 新增 Ctr+8
Tab 9 新增 Ctr+9
Tab 10 新增 Ctr+0

New Tab: Git-Bash-user 新增 Ctrl+T

Move tab to the left - Ctrl+Shift+PageUp
Move tab to the right - Ctrl+Shift+PageDown
(預設的,不用設置)



Plugins

搜尋安裝workspace-manager

Workspace manager

新增配置,設定tab路徑,參考github上的範例,點擊Open config file可以找到 workspace-config.yaml 配置文件。選中你新增的配置然後啟用Run as startup
例:
- title: workerman-chat
  color: '#03fccf'
  commands:
     - cd /c/projects/127.0.0.1/workerman-chat
- title: blog.example.com
  color: '#fc036b'
  commands:
     - cd /c/projects/127.0.0.1/blog.example.com
- title: bagisto
  color: '#302a57'
  commands:
     - cd /c/projects/127.0.0.1/bagisto

ps. Tab 的顏色可以在 https://htmlcolorcodes.com/ 這裡找色碼
重啟tabby後會自動打開設定中的tabs

supervisor

透過Tabby 和 Workspace manager 的腳本執行 php artisan queue:listen 可以在windows替代supervisor,可惜tabby還不能像chrome一樣把tab拉到一個新window去






















2020年9月11日 星期五

不同git庫不同目錄同一個PHPStorm專案使用XDebug

開一個新空白專案

 File => New Project => PHP Empty Project

用ConEmu64 + Git Bash 到剛剛開的PHP專案下拉取不同專案
$ git clone git@github.com:kalecgos0616/project_index.git
$ git clone git@github.com:kalecgos0616/project_codes.git

配置SFTP

不同git不同目錄的code更新到伺服器不同路徑
C:\Users\PhpstormProjects\project_workspace\project_index => /var/www/html/project_index
C:\Users\PhpstormProjects\project_workspace\project_codes => /var/www/html/project_codes




配置XDebug

Open edit Run/Debug configurations dialog => Add New Configuration => PHP Remote Debug => 配置Server

Host 必須設 開發機上的vhost域名:example.com
配置本地和遠程對應的路徑
如果還是不能斷點,在開發機上對本機測試XDebug 端口有沒有通
# telnet 192.168.1.7 9000
Trying 192.168.1.7...
telnet: connect to address 192.168.1.7: Connection timed out
沒有通要用 Windows - CentOS 遠端XDebug調試PHP 的 2. 打開Windows的port 

結果

成功斷點


2020年4月2日 星期四

mongoDB 安裝和使用


安裝

新增repo檔案並加入

# vim /etc/yum.repos.d/mongodb.repo
[MongoDB]
name=MongoDB Repository
baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

安裝

# yum install mongodb-org

啟動  - For CentOS 8/7

# systemctl start mongod.service

開機自動啟動 - For CentOS 8/7

# systemctl enable mongod.service

查版本

# mongod --version
db version v4.2.5
git version: 2261279b51ea13df08ae708ff278f0679c59dc32
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

使用

# mongo
MongoDB shell version v4.2.5
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("317a2efd-8d1e-4781-abcf-38d8a0907eb4") }
MongoDB server version: 4.2.5
Welcome to the MongoDB shell.
...
> use mydb;
switched to db mydb
> db.test.save( { a: 1 } )
WriteResult({ "nInserted" : 1 })
>  db.test.find()
{ "_id" : ObjectId("5e84098b7cf1f4aa0558d5e6"), "a" : 1 }


使用

客戶端使用PHPStorm的Plugins: Mongo Plugin

配置連線

因為 IP+端口不通,可使用SSH通道,URL: IP(不需指定port) 





參考資料

https://tecadmin.net/install-mongodb-on-centos/  How to Install MongoDB 4.2 on CentOS/RHEL 8/7/6






2019年12月13日 星期五

laravel 6 心得

環境

laradock php7.3
laravel 6.6.0
mysql 8.0.16 ( on Windows 10 )

Passport

參考資料

https://learnku.com/docs/laravel/6.x/api-authentication/5429  API 认证
官方強烈建議使用 Laravel Passport 來實現提供API身份驗證
https://learnku.com/laravel/t/22586  使用 Laravel Passport 处理 API 认证
https://medium.com/techcompose/create-rest-api-in-laravel-with-authentication-using-passport-133a1678a876  Create REST API in Laravel with authentication using Passport(原文)

安裝

$ composer require laravel/passport

跑Migration

設定MySQL server用戶權限(以Navicat為例)

使用者 => 新增使用者
在MySQL 8.0,php7.3 插件必須選擇 mysql_native_password ,否則跑migrate時會報錯:
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
https://stackoverflow.com/a/50027581  php mysqli_connect: authentication method unknown to the client [caching_sha2_password]
https://mysqlserverteam.com/upgrading-to-mysql-8-0-default-authentication-plugin-considerations/  Upgrading to MySQL 8.0 : Default Authentication Plugin Considerations
因為當下的 php mysqli extension 不支持新的 caching_sha2 authentication 功能,除非升級php 7.4
伺服器權限:全部授予 => 儲存

建立資料庫、設置.env MySQL連線


運行數據庫遷移

$ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (1.05 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (1.2 seconds)
Migrating: 2016_06_01_000001_create_oauth_auth_codes_table
Migrated:  2016_06_01_000001_create_oauth_auth_codes_table (1.83 seconds)
Migrating: 2016_06_01_000002_create_oauth_access_tokens_table
Migrated:  2016_06_01_000002_create_oauth_access_tokens_table (1.59 seconds)
Migrating: 2016_06_01_000003_create_oauth_refresh_tokens_table
Migrated:  2016_06_01_000003_create_oauth_refresh_tokens_table (1.37 seconds)
Migrating: 2016_06_01_000004_create_oauth_clients_table
Migrated:  2016_06_01_000004_create_oauth_clients_table (1.15 seconds)
Migrating: 2016_06_01_000005_create_oauth_personal_access_clients_table
Migrated:  2016_06_01_000005_create_oauth_personal_access_clients_table (0.69 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.55 seconds)

新增了以下資料表:
failed_jobs
migrations
oauth_access_tokens
oauth_auth_codes
oauth_clients
oauth_personal_access_clients
oauth_refresh_tokens
password_resets
users

生成密鑰

$ php artisan passport:install
Encryption keys generated successfully.
Personal access client created successfully.
Client ID: 1
Client secret: xLFj4jeEdfNJtxcwPUqHojrU0d6F6D0kO1x2yyxx
Password grant client created successfully.
Client ID: 2
Client secret: KUwHMd5XxmrikxqxBFzJDZnNTDcW9kXZY1SKK2T7

密鑰保存在 oauth_clients 表的 Laravel Personal Access Client 和 Laravel Password Grant Client 中

修改代碼


diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php
index 30490683..90ce09b8 100644
--- a/app/Providers/AuthServiceProvider.php
+++ b/app/Providers/AuthServiceProvider.php
@@ -4,6 +4,7 @@ namespace App\Providers;

 use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
 use Illuminate\Support\Facades\Gate;
+use Laravel\Passport\Passport;

 class AuthServiceProvider extends ServiceProvider
 {
@@ -25,6 +26,6 @@ class AuthServiceProvider extends ServiceProvider
     {
         $this->registerPolicies();

-        //
+        Passport::routes();
     }
 }
diff --git a/app/User.php b/app/User.php
index e79dab7f..45db5e3e 100644
--- a/app/User.php
+++ b/app/User.php
@@ -5,10 +5,11 @@ namespace App;
 use Illuminate\Contracts\Auth\MustVerifyEmail;
 use Illuminate\Foundation\Auth\User as Authenticatable;
 use Illuminate\Notifications\Notifiable;
+use Laravel\Passport\HasApiTokens;

 class User extends Authenticatable
 {
-    use Notifiable;
+    use Notifiable, HasApiTokens;

     /**
      * The attributes that are mass assignable.
diff --git a/config/auth.php b/config/auth.php
index aaf982bc..04c6eec2 100644
--- a/config/auth.php
+++ b/config/auth.php
@@ -42,7 +42,7 @@ return [
         ],

         'api' => [
-            'driver' => 'token',
+            'driver' => 'passport',
             'provider' => 'users',
             'hash' => false,
         ],
diff --git a/routes/api.php b/routes/api.php
index c641ca5e..254958fe 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -16,3 +16,17 @@ use Illuminate\Http\Request;
 Route::middleware('auth:api')->get('/user', function (Request $request) {
     return $request->user();
 });
+
+Route::group([
+    'prefix' => 'auth'
+], function () {
+    Route::post('login', 'AuthController@login');
+    Route::post('signup', 'AuthController@signup');
+
+    Route::group([
+      'middleware' => 'auth:api'
+    ], function() {
+        Route::get('logout', 'AuthController@logout');
+        Route::get('user', 'AuthController@user');
+    });
+});
s

  1. 將 Laravel\Passport\HasApiTokens 加入 App\User 模型
  2. AuthServiceProvider boot()中調用Passport::routes()
  3. config/auth.php guards.api.driver 設為 passport
  4. routes/api.php 中添加API路由

創建控制器

$ php artisan make:controller AuthController
app/Http/Controllers/AuthController.php 加入
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\User;

class AuthController extends Controller
{
    /**
     * Create user
     *
     * @param  [string] name
     * @param  [string] email
     * @param  [string] password
     * @param  [string] password_confirmation
     * @return [string] message
     */
    public function signup(Request $request)
    {
        $request->validate([
            'name' => 'required|string',
            'email' => 'required|string|email|unique:users',
            'password' => 'required|string|confirmed'
        ]);

        $user = new User([
            'name' => $request->name,
            'email' => $request->email,
            'password' => bcrypt($request->password)
        ]);

        $user->save();

        return response()->json([
            'message' => 'Successfully created user!'
        ], 201);
    }

    /**
     * Login user and create token
     *
     * @param  [string] email
     * @param  [string] password
     * @param  [boolean] remember_me
     * @return [string] access_token
     * @return [string] token_type
     * @return [string] expires_at
     */
    public function login(Request $request)
    {
        $request->validate([
            'email' => 'required|string|email',
            'password' => 'required|string',
            'remember_me' => 'boolean'
        ]);

        $credentials = request(['email', 'password']);

        if(!Auth::attempt($credentials))
            return response()->json([
                'message' => 'Unauthorized'
            ], 401);

        $user = $request->user();

        $tokenResult = $user->createToken('Personal Access Token');
        $token = $tokenResult->token;

        if ($request->remember_me)
            $token->expires_at = Carbon::now()->addWeeks(1);

        $token->save();

        return response()->json([
            'access_token' => $tokenResult->accessToken,
            'token_type' => 'Bearer',
            'expires_at' => Carbon::parse(
                $tokenResult->token->expires_at
            )->toDateTimeString()
        ]);
    }

    /**
     * Logout user (Revoke the token)
     *
     * @return [string] message
     */
    public function logout(Request $request)
    {
        $request->user()->token()->revoke();

        return response()->json([
            'message' => 'Successfully logged out'
        ]);
    }

    /**
     * Get the authenticated User
     *
     * @return [json] user object
     */
    public function user(Request $request)
    {
        return response()->json($request->user());
    }
}

s

測試(使用Postman)

Header加入
Content-Type:application/json
X-Requested-With:XMLHttpRequest

註冊

http://app.test:5566/api/auth/signup

登錄

http://app.test:5566/api/auth/login

用戶

將登錄拿到的 access_token 放到 Bearer Token
http://app.test:5566/api/auth/user

登出

http://app.test:5566/api/auth/logout


底層如何運作?

登錄

Auth::attempt($credentials);

$this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials);
SessionGuard.php:349, Illuminate\Auth\SessionGuard->attempt()
return $query->first();
EloquentUserProvider.php:131, Illuminate\Auth\EloquentUserProvider->retrieveByCredentials()
=> 沒驗證密碼,直接取users對象
if ($this->hasValidCredentials($user, $credentials)) {
SessionGuard.php:354, Illuminate\Auth\SessionGuard->attempt()
return ! is_null($user) && $this->provider->validateCredentials($user, $credentials);
SessionGuard.php:377, Illuminate\Auth\SessionGuard->hasValidCredentials()
return $this->hasher->check($plain, $user->getAuthPassword());
EloquentUserProvider.php:145, Illuminate\Auth\EloquentUserProvider->validateCredentials()
=> 檢查密碼
$this->login($user, $remember);
SessionGuard.php:355, Illuminate\Auth\SessionGuard->attempt()
$this->setUser($user);
SessionGuard.php:423, Illuminate\Auth\SessionGuard->login()
=>設置用戶

$user = $request->user();

return $this->user;
SessionGuard.php:123, Illuminate\Auth\SessionGuard->user()

$tokenResult = $user->createToken('Personal Access Token');

return Container::getInstance()->make(PersonalAccessTokenFactory::class)->make(
    $this->getKey(), $name, $scopes
);
HasApiTokens.php:67, App\User->createToken()
$this->createRequest($this->clients->personalAccessClient(), $userId, $scopes)
PersonalAccessTokenFactory.php:71, Laravel\Passport\PersonalAccessTokenFactory->make()
return json_decode($this->server->respondToAccessTokenRequest(
    $request, new Response
)->getBody()->__toString(), true);
PersonalAccessTokenFactory.php:114, Laravel\Passport\PersonalAccessTokenFactory->dispatchRequestToAuthorizationServer()
$tokenResponse = $grantType->respondToAccessTokenRequest(
    $request,
    $this->getResponseType(),
    $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()]
);
AuthorizationServer.php:198, League\OAuth2\Server\AuthorizationServer->respondToAccessTokenRequest()
// Issue and persist access token
$accessToken = $this->issueAccessToken(
    $accessTokenTTL, $client,
    $this->getRequestParameter('user_id', $request), $scopes
);
PersonalAccessGrant.php:29, Laravel\Passport\Bridge\PersonalAccessGrant->respondToAccessTokenRequest()
$accessToken->setIdentifier($this->generateUniqueIdentifier());
AbstractGrant.php:440, Laravel\Passport\Bridge\PersonalAccessGrant->issueAccessToken()
return bin2hex(random_bytes($length));
AbstractGrant.php:550, Laravel\Passport\Bridge\PersonalAccessGrant->generateUniqueIdentifier()
=> 將存入 oauth_access_tokens.id 
$this->accessTokenRepository->persistNewAccessToken($accessToken);
AbstractGrant.php:442, Laravel\Passport\Bridge\PersonalAccessGrant->issueAccessToken()
$this->tokenRepository->create([
    'id' => $accessTokenEntity->getIdentifier(),
    'user_id' => $accessTokenEntity->getUserIdentifier(),
    'client_id' => $accessTokenEntity->getClient()->getIdentifier(),
    'scopes' => $this->scopesToArray($accessTokenEntity->getScopes()),
    'revoked' => false,
    'created_at' => new DateTime,
    'updated_at' => new DateTime,
    'expires_at' => $accessTokenEntity->getExpiryDateTime(),
]);
AccessTokenRepository.php:57, Laravel\Passport\Bridge\AccessTokenRepository->persistNewAccessToken()
=> 存入 oauth_access_tokens 表
$token = tap($this->findAccessToken($response), function ($token) use ($userId, $name) {
    $this->tokens->save($token->forceFill([
        'user_id' => $userId,
        'name' => $name,
    ]));
});
PersonalAccessTokenFactory.php:74, Laravel\Passport\PersonalAccessTokenFactory->make()
$response
‌array (
  'token_type' => 'Bearer',
  'expires_in' => 31535998,
  'access_token' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiZmE0OGEyNzYxN2FkNjlhZDlkMTBlZDkzZmExMzFkZTc4MDU2ODBjYzQ0YjYwMDEyNjIwM2E1MTE1ZmExYTFhZTM5ZmEyMDBhZjIzZTNlNTgiLCJpYXQiOjE2MDM3NjQxMDEsIm5iZiI6MTYwMzc2NDEwMSwiZXhwIjoxNjM1MzAwMDk5LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.fSBAzUlHwfwctJBjnM_kiOn48Imp7gcqrMkfJeVdXtE8tFmcNGiFeT3DvtK_0zYCF7qWJM5ZUsOcOEjBhttVgsMPEOjv9Cb7kVdipE2GUX4d4x6Bhw1xCrmYKbNFw4rY3G9J2tySuLZR85ZmIYGf0LvKNkwWvdeS3SZJeh00FAhN3g9ssVgw5NjxzV9T65G5_X-9yDg6GzRQhWNU_GoX1qYhet8dUM1SQTtaTorpjVF5Xk0bEFXyjqkQfiRHiQxBb5YKIuRZu_653Gw8gs1HEv2252CWh7YsGY2iM-5kH3PPz9DpE9GYelC0ULWcWWA7uYknJ7IjXfDhgQKjV_RYpmOIC-EpidazuFd4x1T4ZPIn-ZTjh0wEsPdbReoXagValXByCmykMqAEP9-qPQVG05w_qWoHW_OmV8spnOLz-xZnwmVirughgQiJihN3z1fmnYZTrrsuMZ5pzeG3LNeKDDx1KrYV6m-42JREcHeXVWfl0RgMyCh4d4CkjksNAVB_t756aA4eAzgldGIVtVMuAtkLroS6rtkMQqVVkSfzhjix9fsRN45-7ZKbLCbSi665rl0Bs_XZ0MDs-9LJXkQqSe5DEvvlVoSmLjxUaGkXWfnbkLck2NIAhR3OOYcDq2TEiS749qzaaPEf_nEL17M4mHuDdI4o24NsFBt-gZ5CcHM',
)
return $this->tokens->find(
    $this->jwt->parse($response['access_token'])->getClaim('jti')
);
PersonalAccessTokenFactory.php:126, Laravel\Passport\PersonalAccessTokenFactory->findAccessToken()
$this->tokens Type:
\Laravel\Passport\TokenRepository
$this->jwt Type:
\Lcobucci\JWT\Parser
=> 由此方法可以用\Laravel\Passport\TokenRepository \Lcobucci\JWT\Parser 從 access_token 找回 oauth_access_tokens 

用戶

如何用access_token身份驗證?

routes/api.php
Route::group([
  'middleware' => 'auth:api'
], function() {
    Route::get('user', 'AuthController@user');
});
app/Http/Kernel.php
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
];
Stack:
$this->authenticate($request, $guards);
Authenticate.php:41, App\Http\Middleware\Authenticate->handle()
$this->auth->guard($guard)->check()
Authenticate.php:62, App\Http\Middleware\Authenticate->authenticate()
return ! is_null($this->user());
GuardHelpers.php:60, Illuminate\Auth\RequestGuard->check()
return $this->user = call_user_func(
    $this->callback, $this->request, $this->getProvider()
);
RequestGuard.php:58, Illuminate\Auth\RequestGuard->user()
return (new TokenGuard(
    $this->app->make(ResourceServer::class),
    Auth::createUserProvider($config['provider']),
    $this->app->make(TokenRepository::class),
    $this->app->make(ClientRepository::class),
    $this->app->make('encrypter')
))->user($request);
PassportServiceProvider.php:283, Laravel\Passport\PassportServiceProvider->Laravel\Passport\{closure:/var/www/coolapp/vendor/laravel/passport/src/PassportServiceProvider.php:276-284}()
return $this->authenticateViaBearerToken($request);
TokenGuard.php:94, Laravel\Passport\Guards\TokenGuard->user()
if (! $psr = $this->getPsrRequestViaBearerToken($request)) {
TokenGuard.php:131, Laravel\Passport\Guards\TokenGuard->authenticateViaBearerToken()
return $this->server->validateAuthenticatedRequest($psr);
TokenGuard.php:184, Laravel\Passport\Guards\TokenGuard->getPsrRequestViaBearerToken()
return $this->getAuthorizationValidator()->validateAuthorization($request);
ResourceServer.php:84, League\OAuth2\Server\ResourceServer->validateAuthenticatedRequest()
if ($token->verify(new Sha256(), $this->publicKey->getKeyPath()) === false) {
BearerTokenValidator.php:72, League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator->validateAuthorization()
$this->publicKey->getKeyPath()
‌file:///var/www/coolapp/storage/oauth-public.key
=> 使用 storage/oauth-public.key 驗證 access_token 是否合法
// Ensure access token hasn't expired
$data = new ValidationData();
$data->setCurrentTime(time());
if ($token->validate($data) === false) {
BearerTokenValidator.php:83, League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator->validateAuthorization()
=> 檢查token有無過期
// Check if token has been revoked
if ($this->accessTokenRepository->isAccessTokenRevoked($token->getClaim('jti'))) {
BearerTokenValidator.php:88, League\OAuth2\Server\AuthorizationValidators\BearerTokenValidator->validateAuthorization()
=> 檢查token是否被revoked

密碼授權令牌(grant_type=password)

curl --location --request POST 'http://app.test:5566/oauth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'client_id=2' \
--data-urlencode 'client_secret=client_secret' \
--data-urlencode 'username=username@github.com' \
--data-urlencode 'password=password' \
--data-urlencode 'scope='

返回錯誤:

Symfony\Component\Debug\Exception\FatalThrowableError: Class 'Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory' not found in file /var/www/coolapp/vendor/laravel/framework/src/Illuminate/Routing/RoutingServiceProvider.php on line 131

原因

https://github.com/sfelix-martins/passport-multiauth/issues/124#issuecomment-596418813  Class 'Symfony\\Bridge\\PsrHttpMessage\\Factory\\DiactorosFactory' not found
這個問題將在 laravel 6.18+ / 7.x 修正

解法:

更新laravel
$ composer update



刷新令牌(grant_type=refresh_token)

https://stackoverflow.com/a/59334090   How do I get a refresh token in Laravel Passport?

使用【密碼授權令牌】獲取 access_tokenrefresh_token 

{
    "token_type": "Bearer",
    "expires_in": 31536000,
    "access_token": "access_token.xxx.xxx",
    "refresh_token": "refresh_token_xxx"
}

使用 refresh_token 獲取【刷新令牌】

curl --location --request POST 'http://app.test:5566/oauth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=refresh_token' \
--data-urlencode 'refresh_token=the-refresh-token' \
--data-urlencode 'client_id=2' \
--data-urlencode 'client_secret=client_secret' \
--data-urlencode 'scope='
(原本的會失效,oauth_access_tokens.revoked = 1)

將授權碼轉換為訪問令牌

準備兩個專案

http://app.test:5566/  => laravel 6 ,有安裝passport
http://app3.test:5566/  => laravel 8,無安裝passport

前端快速上手

$ php artisan vendor:publish --tag=passport-components

註冊組件

resources/js/app.js
Vue.component(
    'passport-clients',
    require('./components/passport/Clients.vue').default
);

Vue.component(
    'passport-authorized-clients',
    require('./components/passport/AuthorizedClients.vue').default
);

Vue.component(
    'passport-personal-access-tokens',
    require('./components/passport/PersonalAccessTokens.vue').default
);
在view  resources/views/home.blade.php 中使用
<passport-clients></passport-clients>
<passport-authorized-clients></passport-authorized-clients>
<passport-personal-access-tokens></passport-personal-access-tokens>

在app.test新增用戶的oauth_clients

ps. 綠色那塊是 <passport-clients></passport-clients>  




原理

http://app3.test:5566/ 透過 http://app.test:5566/ 登錄後的授權,在 http://app3.test:5566/ 上使用用戶在app.test生成的oauth_clients(Client ID = 3)獲取 http://app.test:5566/  的token,進而獲取 http://app.test:5566/ 上的資料


在laradock的 workspace和php-fpm 容器新增hosts


diff --git a/docker-compose.yml b/docker-compose.yml
index bc737c0..62152fb 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -165,6 +165,7 @@ services:
         - ./php-worker/supervisord.d:/etc/supervisord.d
       extra_hosts:
         - "dockerhost:${DOCKER_HOST_IP}"
+        - "app.test:192.168.1.9"
       ports:
         - "${WORKSPACE_SSH_PORT}:22"
         - "${WORKSPACE_BROWSERSYNC_HOST_PORT}:3000"
@@ -264,6 +265,7 @@ services:
         - "9000"
       extra_hosts:
         - "dockerhost:${DOCKER_HOST_IP}"
+        - "app.test:192.168.1.9"
       environment:
         - PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
         - DOCKER_HOST=tcp://docker-in-docker:2376
app.test:192.168.1.9 => hyper-v的IP:192.168.1.9,非填laradock容器IP

實作

https://github.com/laravel/passport/issues/221#issuecomment-269828055  API Authentication Error: {"error":"invalid_client","message":"Client authentication failed"}
http://app3.test:5566/ (要改在laravel 8專案,非安裝passport的 http://app.test:5566/ 的 routes/web.php 新增 

  
Route::get('/redirect', function () {
    $query = http_build_query([
        'client_id' => '3',
        'redirect_uri' => 'http://app3.test:5566/callback',
        'response_type' => 'code',
        'scope' => '',
    ]);

    return redirect('http://app.test:5566/oauth/authorize?'.$query);
});

Route::get('/callback', function (\Illuminate\Http\Request $request) {
    $http = new GuzzleHttp\Client;

    $response = $http->post('http://app.test:5566/oauth/token', [
        'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => '3',
            'client_secret' => 'rR2P1HBeysX5NO9noSfMc3BNpEIQyANExxx',
            'redirect_uri' => 'http://app3.test:5566/callback',
            'code' => $request->code,
        ],
    ]);

    return json_decode((string) $response->getBody(), true);
});
  
授權後會轉跳到 http://app3.test:5566/callback?code=def50200ff1e6e1d070750700d964b58c208bcfc69ad68e12c16340c0bf80550de3f8c8c68609f0414365e55a2272a69f99d6a23226ccc01b58079cb3f17d8f800f6c4ef27563da3e98082c53d5abc9b4d1f3d4a6802274b4017fc91a553a297f7ce97b3e81d70c0033642328d0e16cf364edeefad089a476e62ca687d545719f72af9222eb0db3ccb825fc58aefc3bd1edff579fde203704975d66c7984d8873203e610b572c02e6254bffa79417897025fed73a890034300fd91e298bea302e3026f2e9af610d8b2c7848b996acb46574d2c0b75af3c1eca107ad50f56fa25a13a87361f0777ec8ae25e67f7a96751f95047eac0142807406c0cc1a2f61dfeb8eee7a246e89fd74c2c742d3e571a1f6cc7dbac5a1327c6dcde8597028dec48f58c67ee5fa1e3ba48b2326a3e9758e8f1561716e2579bca05f78c3ea0a6801aada0f1112cb0b35d86f838dc0ec85d4e88a4679b6d3cda388af5b0dd2547  
以在 http://app3.test:5566/ 上獲取 該用戶(user_id=1,client_id=3) 的token 。
=》oauth_clients會新增client_id=3的token
app3.test 的 /redirect 路由中的 redirect_uri 必須和 app.test 系統上oauth_clients 該token的oauth_clients.redirect 一致,否則會驗證失敗












2019年8月24日 星期六

phpstorm心得

起因:

因為sublime 的 Xdebug 插件已經很久沒在更新了
https://github.com/martomo/SublimeTextXdebug
所以在新版的php和xdebug中會有不兼容的情況
https://github.com/martomo/SublimeTextXdebug/issues/172#issuecomment-363770737 
而且workerman 的events.php 裡面因為xdebug版本太低斷不到,而xdebug版本太高sublime又不兼容。所以轉向有在維護更新的phpstorm


熱鍵

Settings => ctrl + alt + s
Settings => Keymap => 選Sublime Text

搜尋某個熱鍵榜定了什麼功能(有無被使用)
Keymap => 點擊搜尋圖示 => 輸入熱鍵。ex. ctrl+shift+p 查出已經榜訂了Find Action
打開Version Control 面板(看git log) => alt + 9
Sublime: ( set_mark + select_to_mark ) => ( Find Action )Toggle Sticky Selection 
Sublime: alignment ( ctrl+alt+a ) => ( Find Action ) Reformat Code
Sublime: Emmet - Go to Matching Pair => ctrl + m ( Move Caret to Matching Brace )
快速加入try / catch: 選擇代碼範圍 => ( Find Action ) Surround With => try / catch ... finally 要手動加
對某段(HTML)代碼縮排 => 選擇後 => ( Find Action ) Auto-Indent Lines - Ctrl+Alt+I

複製純文本(貼上word時不帶樣式)

ctrl  + shift + p (Find Action) => Copy as Plain Text  

找檔案中下一個error

ctrl  + shift + p (Find Action) => Next Highlighted Error    



自定義熱鍵

Resume Program(XDebug Run,跳到下一個breakpoint):F9
https://stackoverflow.com/questions/14223631/in-phpstorm-how-do-you-untab-to-the-previous-tabstop/14746035
Unindent:Shift + Tab

Next Line Bookmark in Editor

原本2021.2.2原本 F2 是在同一個文件中跳到下一個書籤,2022.1.1 是會跨文件跳書籤。如果要維持在同一個文件中跳書籤:
Next Line Bookmark:F2 => Next Line Bookmark in Editor:F2

Toggle Sticky Selection: ctrl+alt+shift+s   
=> 避免使用shift + s,因為中文輸入時按shift + s打不出S

Path with line number 

在某個新版後,在Find in Files(Ctrl+Shift+F)中Copy Reference 不再帶行號
Copy Reference:Ctrl+Alt+Shift+C => Path with line number:Ctrl+Alt+Shift+C 


樣式:

配色方案(color scheme)
使用sublime 預設的Monokai
File => Settings => Editor => Color Scheme
因為弱報錯提示不明顯,所以不使用 Monokai ,如圖:

使用PHPStorm預設的Darcula?

斷點時string和int的顏色區別不明顯,所以不使用Darcula




使用Monokai Pro Theme

Settings => Plugins => Marketplace 下載安裝 Monokai Pro Theme
Appearance & Behavior => Appearance => Theme 改回Darcular
Editor => Color Scheme 選擇 Monokai Pro  (非 Monokai Pro(Classic))




字型大小:

Settings => Editor => Font

字型大小(外觀Appearance):

改變編輯器(Editor)以外的字型和大小
Settings => Appearance & Behavior => Appearance 


改變前

改變後


版本控制(以github 為例)

1. 安裝git bash => PHPStorm 會自動偵測到git路徑,因為git也加Windows PowerShell 的環境變量上了

2. git bash 新增公鑰和私鑰
$ ssh-keygen -C "username@email.com" -t rsa
然後把公鑰 .ssh/id_rsa.pub 上傳到github上
ps. 現在github可以開私人project了

3. 拉取代碼
Find Action.. => Checkout from version control =>  Git => 設定路徑

ps. 效果等同在 git bash上直接 git clone下來


Live Template

如同sublime 的 sinppet,

例:
epc
Applicable in ... => 該template 在什麼語言的什麼地方會提示
echo "<pre>\$VARIABLE$:";
print_r($VARIABLE$);
echo "</pre>";

Editor

General 

https://stackoverflow.com/questions/20486542/selecting-entire-variable-with-double-click  Selecting entire variable with double click
點兩下變數不選$:Settings => Editor => General => Smart Keys => Select variable name without '$' sign on double click (預設沒勾,勾了就不選$)

避免打開太多tab使得PHPstorm把tab關掉

Editor | General | Editor Tabs | Tab closing policy | tab limit 設一個大一點的數

增加剪貼板(clipboard )歷史記錄

https://stackoverflow.com/a/10337122  How to increase PHPStorm clipboard history size?
Settings (Preferences on Mac) | Editor | Maximum number of contents to keep in clipboard.


自動在頭部use命名空間

https://blog.csdn.net/vcit102/article/details/117081161  phpstorm 自动在头部 use 命名空间
Editor | General | Auto Import | PHP | (勾選)Enable auto-import in file scope




Code Style

縮排風格,如:將 function的左大括號不換行


註解comment 不要從第一行開始

https://stackoverflow.com/a/48694397  How to set the style of line comment in phpstorm

Preferences > Editor > Code Style > PHP > (tab) Code Generation > Comment Code > (uncheck) Line comment at first column + 勾選"Add a space at comment start"

https://intellij-support.jetbrains.com/hc/en-us/community/posts/206332339-Tabs-and-Indents-are-2-spaces-despite-setting-at-4-spaces?page=1#community_comment_206876035  Tabs and Indents are 2 spaces despite setting at 4 spaces
如果 Reformat Code 不生效,請嘗試
Settings => Editor => Code Style => 取消勾選 "Detect and use existing file indents for editing"

讓 array 的key和value對齊

https://stackoverflow.com/a/29795404  Stop PhpStorm from aligning associative arrays
Settings | Editor | Code Style | PHP | Wrapping and Braces --> Array initializer | Align key-value pairs

讓 class的 const 和value 對齊

Settings | Editor | Code Style | PHP | Wrapping and Braces --> Class field/constant groups | Align constants

Diff

選擇文件與當前文件比較
ctrl + shift + p => compare with  

剪貼簿與當前文件比較
ctrl + shift + p => compare with  Clipboard

underscore.js template

在Settings 中的 Plugins => Marketplace => 搜尋EJS => install 安裝,裝完須重啟IDE
(也可以離線安裝 https://plugins.jetbrains.com/plugin/7296-ejs )


設置特定文件為EJS格式:Settings => Editor => File Types => EJS => 新增文件名
.env 我用 INI Config
.env - Plugins下載 .env files support 

Deployment

Settings => Deployment => Connection
配置SFTP 
Root path: 專案路徑
Encoding for client-server communication: UTF-8
Mappings:
Deployment path: /
Excluded Paths:
本地的 .idea、vendor (PHPStorm 資料夾)不要上傳

Settings => Deployment => Options
Upload changed files automatically to default server: Always
ps. 所有git 操作都在Windows上 PHPStorm的git 或 ConEmu64 + git bash,如果是從Server上Deployment下載下來的話,會下載完又馬上同步上Server(有點坑)
ps2.  Server 分支不用管,全看Windows上 git bash的分支。除非弄亂了再兩邊切到同一個分支的同一個commit再繼續工作
ps3. 如果項目沒git,Upload changed files automatically to default server等先 Deployment  => Download from x.x.x.x 下載完之後再改Always,第一次下載完就不會馬上同步上去

上傳時報錯:Failed to change timestamp of the file 'your.php'

Settings => Deployment => Options
Preserve files timestamps:取消勾選

在Find in Files (Ctrl+Shift+F)中換行

直接點圖Soft-Wrap Preview Editor  

在所選裡面搜尋search in selection

Ctrl+f搜尋逗號(,)=>選擇範圍 => Search In Selection => Select All Occurrences 





XDebug

遠程調試

適用於:本機http調試(xampp、phpstudy)、虛擬機或內網測試機http調試+cli調試
如果忽然不能斷點,檢查本機IP是否變動,更改php.ini的 xdebug.client_host 為本機IP 

Open edit Run/Debug configurations dialog
Add New Configuration -> PHP Remote Debug
IDE key(session id): PHPSTORM   (搭配 chrome 的 Xdebug helper)
Server 不需要配,打開 Start Listening for PHP Debug Connections 和 Xdebug helper 會自動import
如果server 上裝了兩個PHP(5.5 + 7.3 ) 都裝了XDebug,但是只有 5.5的能進斷點,7.3進不了 => 解法:不要裝在同一台,hyper-v 開虛擬機裝PHP 7.3環境。或把php 7.3 + XDebug 跑在laradock上
(可選)Settings => Languages & Framworks => PHP => 配置 PHP language level 和 CLI Interpreter (本機php環境選擇 Local Path to Interpreter...)

Settings => Languages & Framworks => PHP => Debug => External connections => Break at first line in PHP scripts 勿勾選,以免workerman 調試時一直斷在新進程開始處



laradock 中找不到路徑
解法:
在servers中配置laradock的server

對物件斷點

以laravel passport 登錄時
vendor/league/oauth2-server/src/AuthorizationServer.php:192 
為例,$this->enabledGrantTypes跑foreach時,只需要在 personal_access 時斷點
在192行設置斷點,Condition:
strpos("\Laravel\Passport\Bridge\PersonalAccessGrant", get_class($grantType)) !== false

註:
get_class($grantType) => 因為foreach沒指定index,這邊用get_class去取出類命名空間
‌Laravel\Passport\Bridge\PersonalAccessGrant 
strpos => 判斷是否子字串
false => 0也是判斷成功,false才是失敗

\Laravel\Passport\Bridge\PersonalAccessGrant 怎麼快速複製出來?
展開 $this->enabledGrantTypes => 右鍵 => Copy Type
更多:
Jump To Type Source => 打開 vendor/laravel/passport/src/Bridge/PersonalAccessGrant.php
More (Ctrl + Shift + F8) => 顯示所有斷點

本地調試

 適用於:本機cli(腳本、爬蟲),勿使用git bash(斷不到)

Add configuration ...

Add New Configuration  => PHP Script 
設定 Name, File, Interpreter 

設定 CLI  Interpreter 


如果沒設 CLI Interpreter  會報這個錯誤(Debugger extension is not detected )
然後點擊【Debug】
開始快樂斷點


PHPStorm的XDebug也會有bug

https://www.reddit.com/r/phpstorm/comments/un5e97/debug_console_output_only_shows_array_item_count/  Debug console output only shows array item count instead of contents
reddit 上的網友遇到了這個問題,斷點時在console打印array或object只會顯示array的長度,估計是使用最新版本2022.1.1,如圖:
有網友回復他到YouTrack(jetbrains的jira)回報問題

Debug console output only shows array item count instead of contents

官方回復會在 2022.2 修正這個問題

Debug console cannot evaluate arrays or objects

不降版本可以用print_r($a, true) 
https://youtrack.jetbrains.com/issue/WI-66067/Debug-console-cannot-evaluate-arrays-or-objects#focus=Comments-27-6090147.0-0  
PhpStorm 2021.3.2 是最後正常的版本,2021.3.3後就會有這個問題




Docker

.idea

jet brains 系列編輯器會把IDE配置存在項目的.idea 目錄下
如果有人把.idea 加到git 且推到master 分支上,你拉下來用PHPStorm 打開項目時會吃到他的設定。
解法:
關掉PHPStorm ,切到開發分支dev(別的分支 .idea/ 有被gitignore),刪除.idea/ ,然後打開PHPStorm解決

composer

在windows上的PHPStorm直接執行composer而不用 terminal 連上服務器執行

在windows上安裝composer

參考 https://getcomposer.org/download/ 打開 XAMPP for Windows 的終端安裝
# php -v
PHP 7.4.1 (cli) (built: Dec 17 2019 19:24:02) ( ZTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

...
放在 C:\Users\bear\Downloads 下是不行的
bear@BEAR-PC C:\Users\bear\Downloads
# php composer-setup.php
All settings correct for using Composer
The installation directory "C:\Users\bear\Downloads" is not writable

bear@BEAR-PC C:\bear
# php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

bear@BEAR-PC C:\bear
# php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified

bear@BEAR-PC C:\bear
# php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 1.10.1) successfully installed to: C:\bear\composer.phar
Use it: php composer.phar

bear@BEAR-PC C:\bear
# php -r "unlink('composer-setup.php');"

在PHPStorm中設定composer

Settings => Tools => Command Line Tool Support => Add(+) => Choose tool: Composer
Path to PHP executable:  C:\xampp\php\php.exe => 即使和server php版本不一樣,用XAMPP 的 PHP 7.4 沒事
Path to composer.phar or composer: C:\bear\composer.phar => 官網上下載
Alias: c => composer

使用方法(會自動 auto-complete):
Run Command: composer dump-autoload
Windows 的 git bash 一樣要設 git config --global core.autocrlf false 避免 composer dump-autoload 後會有 CRLF will be replaced by LF in xxx 錯誤

windows + PHPStorm + git bash 避免CRLF will be replaced by LF in xxx 錯誤

Settings => Editor => Code Style => General => Line Separator: Unix and macOS(\n)
composer install 或 composer require 安裝插件時只在Linux 上執行
就是確保所有新建文件的Line Separator是\n


artisan

Settings => Tools => Command Line Tool Support => Add(+) => Choose tool: Tool based on Symfony Console.
Path to PHP executable: C:\xampp\php\php.exe
Path to script: C:\PhpstormProjects\project\artisan
Alias: php artisan
如果報錯:
Problem
Failed to parse output as xml: ParseError at [row,col]:[2,1]
Message: 前言中不允许有内容。.
Command
d2564ee6-4324-47ee-b9f2-6bb9789fff37 D:\path\laravel\artisan list --format=xml
Output

Warning: require(D:\path\laravel/vendor/autoload.php): Failed to open stream: No such file or directory in D:\path\laravel\artisan on line 18

Fatal error: Uncaught Error: Failed opening required 'D:\path\laravel/vendor/autoload.php' (include_path='.;C:\php\pear') in D:\path\laravel\artisan:18
Stack trace:
#0 {main}
  thrown in D:\path\laravel\artisan on line 18
檢查下vendor目錄,是不是沒有composer install


Plugins

Case conversion

不用這個,改用String Manipulation

.env files support

EJS

Mongo Plugin

Quick File Preview

SQL Formatter

Sublime的 SqlBeautifier 替代品,但還是覺得Navicat查詢的美化SQL做的比較好

String Manipulation

熱鍵:alt+m
https://stackoverflow.com/a/36477843  Auto increment number for multiple lines in PhpStorm
Sublime的Text Pastry替代品,但用起來沒那麼順手,還是開sublime好。也有 Case conversion功能

.ignore

Ideolog

Rainbow Brackets

Php Inspections (EA Extended)

熱鍵:alt+enter - show context action

GitToolBox

每一行後面顯示git commit(時間 作者 commit內容)


laravel idea

自動生成Model

ctrl+shift+p => laravel =>Code Generation => Create Model 




自動提示DB字段

新增fruits表
CREATE TABLE `fruits` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '名字',
  `class` tinyint(1) NOT NULL DEFAULT '1' COMMENT '等級',
  `quantity` tinyint(2) NOT NULL DEFAULT '0' COMMENT '數量',
  `created_at` datetime NOT NULL COMMENT '創建時間',
  `updated_at` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
s

設定Database
Database(View=>Tool Window) => + => Data Source => MySQL 
第一次打開需要安裝Driver
顯示你專案的資料庫





Helper Code Parameters... 


使用laravel idea Create Model  後(Models/Fruit.php)
先Laravel => Generate Helper Code,然後在 $fillable 中  alt+insert (Generate)

新增字段後。Database先Refresh,然後Generate Helper Code,這樣新增的字段才生效





Languages & Framworks

避免composer.json改變專案的PHP版本

https://stackoverflow.com/a/51222031  How can I set the PHP version in PHPStorm?
PHP => Composer => 取消勾選:Synchronize IDE settings with composer.json


和sublime比較

列模式貼上比sublime強

將剪貼板中的 2 行的內容:
Title1
Title2
取代目標內容的title
- title: Example Title 1
  color: '#03fccf'
  profile: CMD (clink)
  commands:
     - ls
     - cd ..
- title: Example Title 2
  color: '#fc036b'
  profile: 1
- title: Example Title 3
  color: '#302a57'

當剪貼板中的行數(2) 不等於取代目標內容(3)

Sublime


PHPStorm


選擇偶數行

https://stackoverflow.com/a/16273692  How can I select every other line with multiple cursors in Sublime Text?
正則搜索:.*\n.*\n  ,alt+enter 全選, 左鍵 ← ,下鍵 ↓ 






WebStorm

避免切視窗時自動保存文件

不然vue 會自動重新編譯,等手動ctrl+s 保存時再編譯
https://stackoverflow.com/a/15605487  How to Turn off automatic saving on web storm
File | Settings | Appearance & Behavior | System Settings > 取消勾選:
Save files when switching to a different application or a built-in terminal