Laravel Passport輸出ID Token給API Gateway JWT Authorizer驗證用

What the heck is OIDC?

可能 OAuth2 本來就很複雜吧,再加上一層 OIDC 已經沒多少人能用人話講解了(汗
OAuth 2.0 and OpenID Connect (in plain English)

Laravel passport + ID Token

Laravel Passport 背後是用 thephpleague / oauth2-server 做的 OAuth2。
登入之後在 token endpoint 拿到的 access_token 雖然是 JWT,但不能直接丟給 API Gateway(因為少了kid、iss)
需要客製化 token endpoint,讓它能多丟出 ID Token。

詳細的作法可以看這篇Customising token response Laravel Passport

為了怕文章消失,我在這邊 copy 一份簡述:

1.Extend the BearerTokenResponse class, add the extra params you need in the response.
2.Create your own PassportServiceProvider class and override the makeAuthorizationServer() method in order to pass in your own BearerTokenResponse class.
3.Add your provider to the providers array in config/app.php
4.Exclude the passport package from laravel auto-discovery in composer.json

至於 BearerTokenResponse 可以這麼寫:

設定API Gateway、well-known

不過這樣還不夠,在設定 API Gateway 的 JWT Authorizer 時需要輸入「issuer URL」
接著 API Gateway 必須可以從下面這個網址抓到檔案:

{{ issuer URL }}/.well-known/openid-configuration

檔案的內容可以自己照著 Openid-configuration 裡面的 example 寫一份自己的
(只需要填 REQUIRED 的欄位,還有 token_endpoint 欄位即可)

    "issuer": "",
    "authorization_endpoint": "",
    "token_endpoint": "",
    "jwks_uri": "",
    "response_types_supported": [
    "subject_types_supported": [
    "id_token_signing_alg_values_supported": [

注意上面的 jwks_uri,這裡面的 URL 就是要自己產生的另外一個 JSON

    "keys": [
            "kid": "your_foobar_key",
            "kty": "RSA",
            "n": "foobarfoobarfoobarabcdefg",
            "e": "foobar",
            "use": "sig"



  • call “authorize endpoint”
  • redirect back
  • call “token endpoint”
  • call “openid endpoint” and save JWT as the Authorzation header
  • call API Gateway with Authorzation header

Add a Comment

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *