# Ory Self Hosting to Login Gitea Elantris [TOC] --- ## 前言 用 Docker 整合 Ory Kratos + Hydra 服務,並應用在 Gitea OAuth2 登入流程。 - Docker: https://docs.docker.com/get-docker/ - Docker File: https://github.com/shodgson/ory-kratos-hydra-integration-demo - Gitea: https://docs.gitea.com/category/installation - (Optional) Ory Hydra CLI: https://www.ory.sh/docs/hydra/self-hosted/install ## Step 1. Docker 按照 [README.md](https://github.com/shodgson/ory-kratos-hydra-integration-demo?tab=readme-ov-file#set-up) 將 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: - [oryd/kratos:v1.1.0](https://hub.docker.com/r/oryd/kratos) 負責會員管理 - [oryd/kratos-selfservice-ui-node:v1.1.0](https://hub.docker.com/r/oryd/kratos-selfservice-ui-node) 官方展示 kratos + hyrda 服務的前端範例,主要使用了 Express.js 以及 [Ory Elements](https://github.com/ory/elements) 實作各項流程與畫面 - [oryd/hydra:v2.2.0](https://hub.docker.com/r/oryd/hydra) 負責擔當 OAuth2 Provider 的角色 基本上設定都不需要調整,可以注意一下 `kratos.yml` 裡的 `oauth2_provider` 與 `hydra.yml` 裡 `identity_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 ```sh 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](https://github.com/lwhsu/sso-dev/blob/main/hydra/create-oauth2-client.sh) - 如果沒有安裝 Hydra CLI 的話,輸入的指令要改成 `docker-compose -f quickstart.yml exec /hydra` - 需要注意的參數是 `redirect-uri` 其中的 **hydra-client** 是等一下 Gitea 設定時需要對應的名稱 - 建立 client 後記錄下 **CLIENT ID**、**CLIENT SECRET** 兩個欄位 ## Step 3. Gitea 透過套件管理器直接裝是最方便的方式,我是按照 [Installation from package](https://docs.gitea.com/installation/install-from-package) 用 Homebrew 安裝並啟用服務,相關的檔案會放在 `/usr/local/var/gitea`。 ``` brew install gitea brew services start gitea ``` 第一次打開 http://127.0.0.1:3000 時會看到安裝設置頁面,為了方便我只有把資料庫改成 SQLite3 其他都預設即可。  在 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](#Step-2-建立-Hydra-Client) 建立的 **CLIENT ID** | | Client Secret | 在 [Step 2](#Step-2-建立-Hydra-Client) 建立的 **CLIENT SECRET** | | OpenID Connect Auto Discovery URL | http://127.0.0.1:4444/.well-known/openid-configuration | | Additional Scopes | 其他 scope,例如 offline,profile,email |  ## Step 4. OAuth2 登入流程 1. 透過 Demo UI 註冊會員:http://127.0.0.1:4455/registration 2. Gitea 登入頁面會出現 Sign in with hydra-client 的按鈕  4. 跳轉 http://127.0.0.1:4455/login 登入會員 5. 跳轉 http://127.0.0.1:4455/consent 授權同意  6. 跳轉 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 額外安裝起來才成功。
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up