<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>プライベートチャンネル &#8211; FITSブログ</title>
	<atom:link href="http://blog.fits-inc.jp/tag/%E3%83%97%E3%83%A9%E3%82%A4%E3%83%99%E3%83%BC%E3%83%88%E3%83%81%E3%83%A3%E3%83%B3%E3%83%8D%E3%83%AB/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.fits-inc.jp</link>
	<description>Webシステムの受託開発でお困りなら</description>
	<lastBuildDate>Fri, 27 Mar 2020 15:41:25 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>http://blog.fits-inc.jp/wp-content/uploads/2024/06/cropped-image-5-32x32.png</url>
	<title>プライベートチャンネル &#8211; FITSブログ</title>
	<link>http://blog.fits-inc.jp</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>LaravelのPUSH通知を独自認証したプライベートなチャンネルで受け取る</title>
		<link>http://blog.fits-inc.jp/2020/03/27/laravel%e3%81%aepush%e9%80%9a%e7%9f%a5%e3%82%92%e7%8b%ac%e8%87%aa%e8%aa%8d%e8%a8%bc%e3%81%97%e3%81%9f%e3%83%97%e3%83%a9%e3%82%a4%e3%83%99%e3%83%bc%e3%83%88%e3%81%aa%e3%83%81%e3%83%a3%e3%83%b3%e3%83%8d/</link>
		
		<dc:creator><![CDATA[FITS Admin]]></dc:creator>
		<pubDate>Fri, 27 Mar 2020 15:38:56 +0000</pubDate>
				<category><![CDATA[技術ブログ]]></category>
		<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PUSH]]></category>
		<category><![CDATA[プライベートチャンネル]]></category>
		<guid isPermaLink="false">https://blog.fits-inc.jp/?p=376</guid>

					<description><![CDATA[Laravel のPUSH 通知を独自の認証機構を持ったプライベートなチャンネルでやりとりするまでの実装手順をまとめました。・独自の認証ルート定義・独自の認証機構定義・チャンネルのプライベート化（サーバー／クライアント両 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Laravel のPUSH 通知を独自の認証機構を持ったプライベートなチャンネルでやりとりするまでの実装手順をまとめました。<br>・独自の認証ルート定義<br>・独自の認証機構定義<br>・チャンネルのプライベート化（サーバー／クライアント両方）<br>が必要です。<br>※前提条件としてPUSH通知用のドライバとしてPusherを、PUSHサーバーとしてLaravel WebSockets を利用しています。</p>



<h2 class="wp-block-heading">認証ルート定義</h2>



<p>Laravel公式ドキュメント: Laravel 5.7 ブロードキャスト:  認証ルート定義 <br><a href="https://readouble.com/laravel/5.7/ja/broadcasting.html#defining-authorization-routes">https://readouble.com/laravel/5.7/ja/broadcasting.html#defining-authorization-routes</a> <br>上記のドキュメントに従い、PUSHクライアントを認証するためのルーティングを設定します。デフォルトですと</p>



<pre class="wp-block-code"><code>// app/Providers/BroadcastServiceProvider.php
class BroadcastServiceProvider extends ServiceProvider
{
    ...
    public function boot()
    {
        Broadcast::routes();
        ...
    }
}</code></pre>



<p>として、Broadcast::routes で定義されています。ですが、LaravelEchoクライアントがPUSH通信の認可に使用するエンドポイントは独自に定義、設定できます。なので、認可のための独自のルーティングをルーティングファイルに設定し、さらに独自の認証機構も組み込みましょう。<br>※APIサーバーにて認可させるため、api.phpに追加し、さらに認証用の独自ミドルウェアを設定しています</p>



<pre class="wp-block-code"><code>// routes/api.php
// push/auth への POST を
// 独自の認証ミドルウェア push.auth で認証したうえで
// PushController の auth アクションで認可させる
Route::group(['middleware' => 'push.auth'], function () {
    Route::group(['prefix' => 'push'], function () {
        Route::post('auth', 'PushController@auth');
    });
});</code></pre>



<h2 class="wp-block-heading">認証ミドルウェアの実装</h2>



<p>こちらは通常のミドルウェア同様に Kernel.php のミドルウェアテーブルに追加したうえで、実装してください</p>



<pre class="wp-block-code"><code>// app/Http/Kernel.php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
    ...
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        ...
        'push.auth' => \App\Http\Middleware\PushAuthenticate::class,
        ...
    ];
    ...
}</code></pre>



<pre class="wp-block-code"><code>// app/Http/Middleware/PushAuthenticate.php
&lt;?php

namespace App\Http\Middleware;

class PushAuthenticate
{
...
    public function handle($request, Closure $next)
    {
        $key = $request->input('key');
        // do something authentication
        ...
        return $next($request);
    }
}</code></pre>



<h2 class="wp-block-heading">認可のエンドポイント実装</h2>



<p>BroadcastingController の auth アクションと同様の実装を行います。Pusherの auth の結果をJSONで返すだけでよいです。</p>



<pre class="wp-block-code"><code>// app/Http/Controllers/PushController.php

class PushController extends BaseController
{
    public function auth(Request $request, Broadcaster $broadcaster)
    {
        // pusher用の認証情報 $key, $secret, $appIdを取得する
        ...
        // pusher の認証を行い、その結果を返す
        $pusher = new Pusher($key, $secret, $appId);
        $auth = $pusher->presence_auth(
            $request->input('channel_name'),
            $request->input('socket_id'),
            'xxx');
        $auth = json_decode($auth);
        return response()->json($auth);
    }
}</code></pre>



<h2 class="wp-block-heading">LaravelEcho 側に認可用エンドポイント設定</h2>



<p>特に何もしなければLaraveEchoのクライアントは /broadcasting/auth エンドポイントで認証を行うため、authEndpoint オプションを設定、認証用のエンドポイントを変更します。Laravel 標準の auth を使わず独自認証を使うため、クライアントからの認証キーをURLに含めて送らせ認証させています</p>



<pre class="wp-block-code"><code>// resoutes/js/bootstrap.js
...
window.Echo = new Echo({
    broadcaster: 'pusher',
    // 認証情報としてクライアントからクエリストリングでキーを送る
    // key はクライアント側に適宜定義する
    authEndpoint: '/api/push/auth?key=' + key,
    ...    
});</code></pre>



<h2 class="wp-block-heading">LaravelEchoクライアントからのlisten時にプライベートチャンネルを指定</h2>



<p>Laravel公式ドキュメント: Laravel 5.7 ブロードキャスト:  イベントのリッスン  のプライベートチャンネルのイベントをリッスンしたい場合は～に従い下記のように書き換えます。<br><a href="https://readouble.com/laravel/5.7/ja/broadcasting.html#listening-for-events">https://readouble.com/laravel/5.7/ja/broadcasting.html#listening-for-events</a></p>



<pre class="wp-block-code"><code>// 修正前
Echo.channel('channel').listen('Event', e => {...});</code></pre>



<pre class="wp-block-code"><code>// 修正後
Echo.private('channel').listen('Event', e => {...});</code></pre>



<h2 class="wp-block-heading"> Laravel の PUSH 機能を使ったシステム構築をお考えなら</h2>



<p>　宮城、仙台に限らず、PUSH機能を使ったシステム導入にお困りの方や、システムの構築可能なベンダーをお探しならば、ぜひ弊社までご相談ください。</p>



<p>宮城県仙台市のシステム開発会社FITS<br><a href="https://www.fits-inc.jp/">https://www.fits-inc.jp/</a></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
