Try   HackMD

Ory Self Hosting to Login Gitea

Elantris


前言

用 Docker 整合 Ory Kratos + Hydra 服務,並應用在 Gitea OAuth2 登入流程。

Step 1. Docker

按照 README.md 將 docker container 裝起來:

git clone git@github.com:shodgson/ory-kratos-hydra-integration-demo.git
cd ory-kratos-hydra-integration-demo
docker compose -f quickstart.yml up --build

這個 Docker File 包含的 Docker Image:

基本上設定都不需要調整,可以注意一下 kratos.yml 裡的 oauth2_providerhydra.ymlidentity_provider。預設用到的 Port:

Port Description
4433 Kratos public URL
4434 Kratos admin URL
4444 Hydra public URL
4445 Hydra admin URL
4455 Demo UI
5555 Hydra token user

Step 2. 建立 Hydra Client

hydra create oauth2-client \
    --endpoint http://127.0.0.1:4445 \
    --name "hydra-client" \
    --grant-type authorization_code,refresh_token \
    --response-type code \
    --scope openid,offline \
    --redirect-uri http://127.0.0.1:3000/user/oauth2/hydra-client/callback \
    --token-endpoint-auth-method client_secret_basic \
    --subject-type public
  • lwhsu/sso-dev create-oauth2-client.sh
  • 如果沒有安裝 Hydra CLI 的話,輸入的指令要改成 docker-compose -f quickstart.yml exec /hydra
  • 需要注意的參數是 redirect-uri 其中的 hydra-client 是等一下 Gitea 設定時需要對應的名稱
  • 建立 client 後記錄下 CLIENT IDCLIENT SECRET 兩個欄位

Step 3. Gitea

透過套件管理器直接裝是最方便的方式,我是按照 Installation from package 用 Homebrew 安裝並啟用服務,相關的檔案會放在 /usr/local/var/gitea

brew install gitea
brew services start gitea

第一次打開 http://127.0.0.1:3000 時會看到安裝設置頁面,為了方便我只有把資料庫改成 SQLite3 其他都預設即可。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在 Gitea 第一個註冊的使用者會是管理員,可以進到網站後台設定頁面,新增 OAuth2 Client http://127.0.0.1:3000/admin/auths/new

Name Value
Authentication Type OAuth2
Authentication Name hydra-client(對應 redirect uri)
OAuth2 Provider OpenID Connect
Client ID (Key) Step 2 建立的 CLIENT ID
Client Secret Step 2 建立的 CLIENT SECRET
OpenID Connect Auto Discovery URL http://127.0.0.1:4444/.well-known/openid-configuration
Additional Scopes 其他 scope,例如 offline,profile,email

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Step 4. OAuth2 登入流程

  1. 透過 Demo UI 註冊會員:http://127.0.0.1:4455/registration
  2. Gitea 登入頁面會出現 Sign in with hydra-client 的按鈕
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  3. 跳轉 http://127.0.0.1:4455/login 登入會員
  4. 跳轉 http://127.0.0.1:4455/consent 授權同意
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  5. 跳轉 http://127.0.0.1:3000/user/link_account 回到 Gitea 連結或建立新的帳號

後記

雖然我有嘗試把 gitea 的 docker image 也包進 quickstart.yml 裡面隨著 kratos、hydra 一起安裝並啟動服務,但其實我沒有搞懂 docker network 的設定,導致在 Gitea 裡新增 OAuth2 Client 時設定 OpenID Connect Auto Discovery URL 後會遇到 dial tcp 127.0.0.1:4444: connect: connection refused 的問題無法啟用 OAuth2 登入,最後使用 Homebrew 將 Gitea 額外安裝起來才成功。