* New translations kubernetes.md (English) * New translations kubernetes.md (Indonesian) * New translations manual.md (French) * New translations manual.md (Italian) * New translations manual.md (Korean) * New translations manual.md (Polish) * New translations manual.md (Chinese Simplified) * New translations manual.md (Chinese Traditional) * New translations manual.md (English) * New translations manual.md (Indonesian) * New translations cdn.md (French) * New translations cdn.md (Italian) * New translations cdn.md (Korean) * New translations cdn.md (Polish) * New translations cdn.md (Chinese Simplified) * New translations cdn.md (Chinese Traditional) * New translations cdn.md (English) * New translations cdn.md (Indonesian) * New translations nginx.md (French) * New translations nginx.md (Italian) * New translations nginx.md (Korean) * New translations nginx.md (Polish) * New translations nginx.md (Chinese Simplified) * New translations nginx.md (Chinese Traditional) * New translations nginx.md (English) * New translations nginx.md (Indonesian) * New translations push-docker-hub.md (French) * New translations push-docker-hub.md (Italian) * New translations push-docker-hub.md (Korean) * New translations push-docker-hub.md (Polish) * New translations push-docker-hub.md (Chinese Simplified) * New translations push-docker-hub.md (Chinese Traditional) * New translations push-docker-hub.md (English) * New translations push-docker-hub.md (Indonesian) * New translations scale-out.md (French) * New translations scale-out.md (Italian) * New translations scale-out.md (Korean) * New translations scale-out.md (Polish) * New translations scale-out.md (Chinese Simplified) * New translations scale-out.md (Chinese Traditional) * New translations scale-out.md (English) * New translations scale-out.md (Indonesian) * New translations troubleshooting.md (French) * New translations troubleshooting.md (Italian) * New translations troubleshooting.md (Korean) * New translations troubleshooting.md (Polish) * New translations troubleshooting.md (Chinese Simplified) * New translations troubleshooting.md (Chinese Traditional) * New translations troubleshooting.md (English) * New translations troubleshooting.md (Indonesian) * New translations disable-timelines.md (French) * New translations disable-timelines.md (Italian) * New translations disable-timelines.md (Korean) * New translations disable-timelines.md (Polish) * New translations disable-timelines.md (Chinese Simplified) * New translations disable-timelines.md (Chinese Traditional) * New translations disable-timelines.md (English) * New translations disable-timelines.md (Indonesian) * New translations 1.index.md (French) * New translations 1.index.md (Italian) * New translations 1.index.md (Korean) * New translations 1.index.md (Polish) * New translations 1.index.md (Chinese Simplified) * New translations 1.index.md (Chinese Traditional) * New translations 1.index.md (English) * New translations 1.index.md (Indonesian) * New translations libraries.md (French) * New translations libraries.md (Italian) * New translations libraries.md (Korean) * New translations libraries.md (Polish) * New translations libraries.md (Chinese Simplified) * New translations libraries.md (Chinese Traditional) * New translations libraries.md (English) * New translations libraries.md (Indonesian) * New translations permission.md (French) * New translations permission.md (Italian) * New translations permission.md (Korean) * New translations permission.md (Polish) * New translations permission.md (Chinese Simplified) * New translations permission.md (Chinese Traditional) * New translations permission.md (English) * New translations permission.md (Indonesian) * New translations 1.index.md (French) * New translations 1.index.md (Italian) * New translations 1.index.md (Korean) * New translations 1.index.md (Polish) * New translations 1.index.md (Chinese Simplified) * New translations 1.index.md (Chinese Traditional) * New translations 1.index.md (English) * New translations 1.index.md (Indonesian) * New translations 1.index.md (French) * New translations 1.index.md (Italian) * New translations 1.index.md (Korean) * New translations 1.index.md (Polish) * New translations 1.index.md (Chinese Simplified) * New translations 1.index.md (Chinese Traditional) * New translations 1.index.md (English) * New translations 1.index.md (Indonesian) * New translations global-timeline.md (French) * New translations global-timeline.md (Italian) * New translations global-timeline.md (Korean) * New translations global-timeline.md (Polish) * New translations global-timeline.md (Chinese Simplified) * New translations global-timeline.md (Chinese Traditional) * New translations global-timeline.md (English) * New translations global-timeline.md (Indonesian) * New translations home-timeline.md (French) * New translations home-timeline.md (Italian) * New translations home-timeline.md (Korean) * New translations home-timeline.md (Polish) * New translations home-timeline.md (Chinese Simplified) * New translations home-timeline.md (Chinese Traditional) * New translations home-timeline.md (English) * New translations home-timeline.md (Indonesian) * New translations hybrid-timeline.md (French) * New translations hybrid-timeline.md (Italian) * New translations hybrid-timeline.md (Korean) * New translations hybrid-timeline.md (Polish) * New translations hybrid-timeline.md (Chinese Simplified) * New translations hybrid-timeline.md (Chinese Traditional) * New translations hybrid-timeline.md (English) * New translations hybrid-timeline.md (Indonesian) * New translations index.md (French) * New translations index.md (Italian) * New translations index.md (Korean) * New translations index.md (Polish) * New translations index.md (Chinese Simplified) * New translations index.md (Chinese Traditional) * New translations index.md (English) * New translations index.md (Indonesian) * New translations local-timeline.md (French) * New translations local-timeline.md (Italian) * New translations local-timeline.md (Korean) * New translations local-timeline.md (Polish) * New translations local-timeline.md (Chinese Simplified) * New translations local-timeline.md (Chinese Traditional) * New translations local-timeline.md (English) * New translations local-timeline.md (Indonesian) * New translations main.md (French) * New translations main.md (Italian) * New translations main.md (Korean) * New translations main.md (Polish) * New translations main.md (Chinese Simplified) * New translations main.md (Chinese Traditional) * New translations main.md (English) * New translations main.md (Indonesian) * New translations note-capture-events.md (French) * New translations note-capture-events.md (Italian) * New translations note-capture-events.md (Korean) * New translations note-capture-events.md (Polish) * New translations note-capture-events.md (Chinese Simplified) * New translations note-capture-events.md (Chinese Traditional) * New translations note-capture-events.md (English) * New translations note-capture-events.md (Indonesian) * New translations 2.miauth.md (French) * New translations 2.miauth.md (Italian) * New translations 2.miauth.md (Korean) * New translations 2.miauth.md (Polish) * New translations 2.miauth.md (Chinese Simplified) * New translations 2.miauth.md (Chinese Traditional) * New translations 2.miauth.md (English) * New translations 2.miauth.md (Indonesian) * New translations 1.index.md (French) * New translations 1.index.md (Italian) * New translations 1.index.md (Korean) * New translations 1.index.md (Polish) * New translations 1.index.md (Chinese Simplified) * New translations 1.index.md (Chinese Traditional) * New translations 1.index.md (English) * New translations 1.index.md (Indonesian) * New translations create-plugin.md (French) * New translations create-plugin.md (Italian) * New translations create-plugin.md (Korean) * New translations create-plugin.md (Polish) * New translations create-plugin.md (Chinese Simplified) * New translations create-plugin.md (Chinese Traditional) * New translations create-plugin.md (English) * New translations create-plugin.md (Indonesian) * New translations plugin-api-reference.md (French) * New translations plugin-api-reference.md (Italian) * New translations plugin-api-reference.md (Korean) * New translations plugin-api-reference.md (Polish) * New translations plugin-api-reference.md (Chinese Simplified) * New translations plugin-api-reference.md (Chinese Traditional) * New translations plugin-api-reference.md (English) * New translations plugin-api-reference.md (Indonesian) * New translations publish-on-your-website.md (French) * New translations publish-on-your-website.md (Italian) * New translations publish-on-your-website.md (Korean) * New translations publish-on-your-website.md (Polish) * New translations publish-on-your-website.md (Chinese Simplified) * New translations publish-on-your-website.md (Chinese Traditional) * New translations publish-on-your-website.md (English) * New translations publish-on-your-website.md (Indonesian) * New translations 5.releases.md (French) * New translations 5.releases.md (Italian) * New translations 5.releases.md (Korean) * New translations 5.releases.md (Polish) * New translations 5.releases.md (Chinese Simplified) * New translations 5.releases.md (Chinese Traditional) * New translations 5.releases.md (English) * New translations 5.releases.md (Indonesian) * New translations endpoints.md (French) * New translations endpoints.md (Italian) * New translations endpoints.md (Korean) * New translations endpoints.md (Polish) * New translations endpoints.md (Chinese Simplified) * New translations endpoints.md (Chinese Traditional) * New translations endpoints.md (English) * New translations endpoints.md (Indonesian) * New translations 1.index.md (French) * New translations 1.index.md (Italian) * New translations 1.index.md (Korean) * New translations 1.index.md (Polish) * New translations 1.index.md (Chinese Simplified) * New translations 1.index.md (Chinese Traditional) * New translations 1.index.md (English) * New translations 1.index.md (Indonesian) * New translations 3.oauth.md (French) * New translations 3.oauth.md (Italian) * New translations 3.oauth.md (Korean) * New translations 3.oauth.md (Polish) * New translations 3.oauth.md (Chinese Simplified) * New translations 3.oauth.md (Chinese Traditional) * New translations 3.oauth.md (English) * New translations 3.oauth.md (Indonesian) * New translations aiscript.md (French) * New translations aiscript.md (Italian) * New translations aiscript.md (Korean) * New translations aiscript.md (Polish) * New translations aiscript.md (Chinese Simplified) * New translations aiscript.md (Chinese Traditional) * New translations aiscript.md (English) * New translations aiscript.md (Indonesian) * New translations 50.app.md (French) * New translations 50.app.md (Italian) * New translations 50.app.md (Korean) * New translations 50.app.md (Polish) * New translations 50.app.md (Chinese Simplified) * New translations 50.app.md (Chinese Traditional) * New translations 50.app.md (English) * New translations 50.app.md (Indonesian) * New translations 7.become-a-sponsor.md (French) * New translations 7.become-a-sponsor.md (Italian) * New translations 7.become-a-sponsor.md (Korean) * New translations 7.become-a-sponsor.md (Polish) * New translations 7.become-a-sponsor.md (Chinese Simplified) * New translations 7.become-a-sponsor.md (Chinese Traditional) * New translations 7.become-a-sponsor.md (English) * New translations 7.become-a-sponsor.md (Indonesian)
7.3 KiB
description |
---|
v2023.9.0以降で使用できる、OAuth2.0方式での認証方法について説明しています。 |
OAuth方式でのアクセストークン取得方式
アプリケーションを利用するユーザー(以下単に「ユーザー」と呼びます)のアクセストークンを取得するには、以下の手順で発行をリクエストします。
:::tip
以下に説明する方法は、OAuth 2.0と呼ばれるものです。普通のOAuthはアプリを作成しますが、IndieAuthの拡張でアプリ作成なしで使えるようになっています。
OAuth方式は使えるライブラリが多いので、出来ればライブラリを使うのをおすすめします。
現在、この方式を使うためにはウェブページが必要になります。どうしてもウェブページを用意できない場合、もしくはMisskey 2023.9.0以前のバージョンをサポートしたい場合、以下の方式を使ってください。
:::
Step 1
アプリ紹介のためのウェブページを作ります。ページがHTTPSアドレスでアクセスできるようにしてください。ページのとこかに以下のようなHTMLコードを書きます。
<!-- (必須項目)hrefのアドレスが認証コードの転送先になります。 -->
<link rel='redirect_uri' href='/redirect'>
<!-- ユーザーに見せるアプリの名前になります。なかったらこのページのアドレスが名前になります。 -->
<div class='h-app'>
<a href="/" class="u-url p-name">My Misskey App</a>
</div>
あとでredirect_uri
のアドレスに認証コードが転送されます。
Step 2
PKCE code_verifier
とcode_challenge
文字列, 及びstate
文字列を生成します。
code_verifier
の場合は最低43字、最高128字でアルファベット大・小文字及び-._~
の中の文字に限られます。code_challenge
文字列はcode_verifier
文字列をSHA256アルゴリズムでハッシュしてbase64urlでエンコードした結果を使います。state
文字列には特別な制限はありません。ランダムな文字列を使います。
:::danger
この文字列は毎回生成し、使いまわさないようにしてください。
:::
:::tip
pkce-challengeとかのライブラリを使ったり、OAuthライブラリのPKCE機能を使うのがおすすめです。
:::
:::tip{label='例'}
import crypto from "node:crypto";
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~";
const codeVerifier = new Array(128)
.fill(0)
.map(() => chars[Math.floor(chars.length * Math.random())])
.join("");
console.log('code_verifier', codeVerifier);
const codeChallenge = crypto
.createHash("sha256")
.update(codeVerifier, "ascii")
.digest("base64url");
console.log('code_challenge', codeChallenge);
const state = crypto.randomUUID();
console.log('state', state);
:::
Step 3
相手サーバーのOAuth情報を取得します。データはJSON形式になっています。
https://{host}/.well-known/oauth-authorization-server
{host}
の部分は、ユーザーのサーバーのホストに置き換えます。通常ホストはユーザーが入力します。
ここではauthorization_endpoint
とtoken_endpoint
を使います。
:::tip
次のステップで使われるscope
の情報もscopes_supported
で確認できます。
:::
Step 4
アプリケーション認証フォームをユーザーのブラウザで表示させます。認証フォームは、以下の形式のURLで開くことができます:
{authorization_endpoint}?client_id={client_id}&response_type=code&redirect_uri={redirect_uri}&scope={scope}&code_challenge={code_challenge}&code_challenge_method=S256&state={state}
ここで、
{authorization_endpoint}
の部分は、前の情報取得で得たアドレスに置き換えます。{client_id}
の部分は、アプリの紹介ページのアドレスに置き換えます。{code_challenge}
の部分は、前に生成したcode_challenge
文字列に置き換えますcode_challenge_method
の部分は常にS256
にします。{redirect_uri}
の部分は、紹介ページで使っている配達先のアドレスに置き換えます。{scope}
の部分は、アプリケーションが要求する権限に置き換えます。要求する権限を{state}
の部分は、前に生成したstate
文字列に置き換えます。
:::tip{label='例'}
https://misskey.local/oauth/authorize?client_id=http%3A%2F%2Fexample.com&code_challenge=C6hwMO2bmIzg3nqppTE9b79fvuOjlrKmH2xNiZSMHzw&code_challenge_method=S256&response_type=code&redirect_uri=http%3A%2F%2Fexample.com%2Fredirect&scope=write%3Anotes&state=87c11f05-86eb-4eb2-9057-f6a98fc5e9ab
:::
Step 5
ユーザーがアプリケーションアクセスを許可したら、redirect_uri
のアドレスに認証コードがURLパラメータの形式で転送されます。
名前 | 説明 |
---|---|
code |
ユーザーの認証コード。 |
state |
認証リクエストに使われたstate 文字列。 |
:::tip{label='例'}
https://example.com/redirect?code=...&state=87c11f05-86eb-4eb2-9057-f6a98fc5e9ab
:::
state
文字列がちゃんと一致しているのか確認して、次のステップに進みます。
Step 6
転送された認証コードを使ってアクセストークンをPOSTでリクエストします。リクエスト先はtoken_endpoint
になります。データ形式はapplication/json
とapplication/x-www-form-urlencoded
を使えます。各パラメータは以下のようになります。
名前 | 説明 |
---|---|
grant_type |
常にauthorization_code にします。 |
client_id |
認証リクエストに使われたclient_id 文字列。 |
redirect_uri |
認証リクエストに使われたredirect_uri 文字列。 |
scope |
認証リクエストに使われたscope 文字列。 |
code |
取得した認証コード。 |
code_verifier |
前に生成したcode_verifier 文字列。 |
:::tip{label='例'}
const res = await fetch(endpoint, {
method: "POST",
body: JSON.stringify({
grant_type: "authorization_code",
client_id: "https://example.com",
redirect_uri: "https://example.com/redirect",
scope: "write:notes",
code: "...",
code_verifier: "hjjbCYDmDpSLjirkO-PrfWKsRhDdJr-PAEGRClRwzUKlmFIIIrZNmSvUIraeIa~WqbqQnfbJV-Hc_IfuQkesBYUpukUi~lInDfU_AZjoZqbU.ioQTRzaFfZFfGnT-OAA",
}),
headers: {
"Content-Type": "application/json"
}
});
:::
レスポンスはJSONオブジェクト形式で、そこからaccess_token
を取得して使います。