# User Api Key 클라이언트가 유효한 auth\_redirect를 등록할 수 있는 가능성

**URL:** <https://meta.discourse.org/t/feasibility-of-allowing-a-user-api-key-client-to-register-a-valid-auth-redirect/312901>\
**Category:** Development\
**Created:** [6월 20, 2024, 4:01오후 UTC](https://meta.discourse.org/t/feasibility-of-allowing-a-user-api-key-client-to-register-a-valid-auth-redirect/312901 "2024-06-20T16:01:11Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![angus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/angus/32/341715_2.png) [@angus](https://meta.discourse.org/u/angus)\
**Post date:** [6월 20, 2024, 4:01오후 UTC](https://meta.discourse.org/t/feasibility-of-allowing-a-user-api-key-client-to-register-a-valid-auth-redirect/312901/1 "2024-06-20T16:01:11Z")

</div>

사용자 API 키 클라이언트가 키 생성을 위해 유효한 `auth_redirect`를 등록할 수 있도록 하는 것이 실현 가능한지 고려하고 있습니다.

### 현재 기능

현재 사용자 API 키 리디렉션은 호스트 사이트에서 사이트 설정 `allowed_user_api_auth_redirects`를 통해 등록해야 합니다. 키 생성 요청과 함께 전송되는 모든 `auth_redirect`는 해당 사이트 설정 목록과 대조되어 검증됩니다([여기 참조](https://github.com/discourse/discourse/blob/main/app/models/user_api_key.rb#L72)). 이는 오픈 리디렉션(다양한 보안 위험을 초래할 수 있음)을 방지하는 합리적인 보호 조치입니다.

### 동인(Efficient Cause)

이러한 고찰의 동인은 ActivityPub 플러그인의 사용자 인증 기능입니다. 이 기능은 사용자가 ActivityPub 액터의 소유권을 증명하여 해당 액터의 활동이 특정 인스턴스에서 사용자의 계정과 연관되도록 합니다. 그 모습은 다음과 같습니다.

https://www.loom.com/embed/247f2817d6ae41378794e1e7699872aa?sid=fd684e22-204a-449a-9fc9-5134509909a5

현재 이는 모든 Mastodon 서버가 OAuth 클라이언트의 프로그래매틱 생성을 허용하는 OAuth 제공자이므로 Mastodon과 함께 작동합니다. 플로우의 작동 방식은 다음과 같습니다:

1. 사용자는 자신의 계정이 있는 Mastodon 도메인을 식별합니다.
2. 해당 도메인에서 처음 인증하는 경우, 플러그인은 그 Mastodon 서버에서 OAuth 앱을 생성하고, 리디렉션을 등록하며, OAuth 플로우를 위한 클라이언트 자격 증명을 얻습니다.
3. 플러그인은 2단계에서 생성한(또는 이전에 생성한) OAuth 앱을 사용하여 사용자와 OAuth 플로우를 수행합니다.

목표는 User Api Keys를 사용하여 Discourse에서 실질적으로 동일한 기능을 갖게 하는 것입니다. 이는 이미 이 PR에서 구현되었습니다.

> <https://github.com/discourse/discourse-activity-pub/pull/93>
>
> @pmusaraj This is ready to go once https://github.com/discourse/discourse-activi…ty-pub/pull/84 is merged.
> 
> \- Authorizations now have a dedicated model and table (existing mastodon authorizations will be migrated)
> \- Discourse actor authorizations are now supported.
> \- The interface in user preferences has been improved.
> 
> \### Notes
> 
> To authorize a remote Discourse user, the ActivityPub auth redirect for the user's domain has to be in the site setting \`allowed user api auth redirects\`:
> 
> \`\`\`
> \[url\]/ap/auth/redirect/discourse
> \`\`\`
> For example:
> \`\`\`
> https://meta.discourse.org/ap/auth/redirect/discourse
> \`\`\`

이 플로우에는 호스트 사이트가 클라이언트의 도메인 또는 리디렉션을 `allowed_user_api_auth_redirects`에 이미 추가해 두어야 합니다.

### 가능한 변경 사항

현재 제가 생각하고 있는 것은 `discourse/discourse`에 다음과 같은 내용을 수행하는 PR을 만드는 것입니다.

#### user\_api\_key\_client 테이블 생성

컬럼:

- `user_api_key`에서 이관된 `client_id` 및 `application_name`
- `redirect_uri` (선택 사항)
- `public_key` (선택 사항)

`client_id`와 `application_name`을 사용하는 기존 모든 기능은 `user_api_key_client`를 통해 이전과 동일하게 작동할 것입니다.

#### 클라이언트 등록 루트 추가

```plaintext
post "/user-api-key/register" => "user_api_keys#register"

```

필수 매개변수:

- `application_name`
- `client_id`
- `public_key`
- `auth_redirect`

액션:

- 매개변수를 검증하고 `user_api_key_client`에 저장
- 성공 시 성공 코드를 반환

#### 생성 작업에서 등록된 클라이언트 사용 허용

UserApiKeyController의 `create` 액션을 다음과 같이 변경합니다.

- `client_id`가 `user_api_key_client`에 존재하고 `auth_redirect`와 `public_key`를 모두 가진 경우:
  - 저장된 `auth_redirect`와 `auth_redirect` 사용 여부를 검증
  - 저장된 `public_key`를 사용하여 반환 페이로드를 암호화

- 그렇지 않으면 기존 기능을 따릅니다.

cc @pmusaraj

---

_[View the full topic](https://meta.discourse.org/t/feasibility-of-allowing-a-user-api-key-client-to-register-a-valid-auth-redirect/312901)._
