6
6
7
7
- ** このパッケージは不安定版です**
8
8
- パッチリリースを含め予告なく破壊的変更が行われる場合があります
9
- - [ TypeDoc] ( https://macropygia.github.io/elysia-openid-client/ )
9
+ - Links: [ GitHub ] ( https://github.com/macropygia/elysia-openid-client ) / [ npm ] ( https://www.npmjs.com/package/elysia-openid-client ) / [ TypeDoc] ( https://macropygia.github.io/elysia-openid-client/ )
10
10
11
11
## 仕様・制限事項
12
12
@@ -38,23 +38,26 @@ import Elysia from "elysia";
38
38
import { OidcClient } from " elysia-openid-client" ;
39
39
40
40
const rp = await OidcClient .create ({
41
- baseUrl: " https://app.example.com" ,
42
- issuerUrl: " https://issuer.example.com" ,
41
+ baseUrl: " https://app.example.com" , // RPのURL
42
+ issuerUrl: " https://issuer.example.com" , // OPのURL
43
43
clientMetadata: {
44
44
client_id: " client-id" ,
45
45
client_secret: " client-secret" ,
46
46
},
47
47
});
48
- const endpoints = rp .getEndpoints ();
49
- const hook = rp .getAuthHook ();
48
+ const endpoints = rp .getEndpoints (); // エンドポイントプラグイン
49
+ const hook = rp .getAuthHook (); // フックプラグイン
50
50
51
- console .log (rp .issuerMetadata );
51
+ console .log (rp .issuerMetadata ); // OPのメタデータを表示
52
52
53
53
new Elysia ()
54
- .use (endpoints )
55
- .guard ((app ) =>
54
+ .use (endpoints ) // エンドポイントを挿入
55
+ .guard ((app ) => // この内側が要認証エリア
56
56
app
57
- .use (hook )
57
+ .use (hook ) // 認証・認可用のonBeforeHandleフックを挿入
58
+ .onBeforeHandle (({ sessionStatus , sessionClaims }) => {
59
+ // ユーザー名・メール・グループ等による認可処理
60
+ })
58
61
.get (" /" , ({ sessionStatus }) => sessionStatus ? " Logged in" : " Restricted" )
59
62
.get (" /status" , ({ sessionStatus }) => sessionStatus )
60
63
.get (" /claims" , ({ sessionClaims }) => sessionClaims ),
@@ -64,7 +67,7 @@ new Elysia()
64
67
.listen (80 );
65
68
```
66
69
67
- - [ その他のサンプル] ( https://github.com/macropygia/elysia-openid-client/tree/main/examples )
70
+ - [ その他のサンプル] ( https://github.com/macropygia/elysia-openid-client/tree/main/__examples__ )
68
71
69
72
## 設定
70
73
@@ -105,29 +108,87 @@ const rp = await OidcClient.create(options);
105
108
- [ OIDCClientLogger] ( https://macropygia.github.io/elysia-openid-client/interfaces/types.OIDCClientLogger.html )
106
109
- 本文書の ` ロガー ` の項を参照
107
110
- ` ClientMetadata `
108
- - ` openid-client ` の [ 型定義 ] ( https://github.com/panva/node-openid-client/blob/main/types/index.d.ts )
111
+ - ` openid-client ` の [ ` ClientMetadata ` の型定義 ] ( https://github.com/panva/node-openid-client/blob/main/types/index.d.ts )
109
112
- および ` OpenID Connect Dynamic Client Registration 1.0 ` の [ Client Metadata] ( https://openid.net/specs/openid-connect-registration-1_0.html#ClientMetadata ) の章を参照
110
113
- ` AuthorizationParameters `
111
- - ` openid-client ` の [ 型定義 ] ( https://github.com/panva/node-openid-client/blob/main/types/index.d.ts )
114
+ - ` openid-client ` の [ ` AuthorizationParameters ` の型定義 ] ( https://github.com/panva/node-openid-client/blob/main/types/index.d.ts )
112
115
- および ` OpenID Connect Core 1.0 ` の [ Authentication Request] ( https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest ) の章を参照
113
116
117
+ ## エンドポイント
118
+
119
+ - ElysiaJSプラグインとしてのメタデータ
120
+ - 名称: ` elysia-openid-client-endpoints `
121
+ - [ シード] ( https://elysiajs.com/essential/plugin#plugin-deduplication ) : ` settings.pluginSeed ` 、未指定なら ` issuerUrl `
122
+ - 参照: [ openid-client API Documentation - Client] ( https://github.com/panva/node-openid-client/blob/main/docs/README.md#client )
123
+
124
+ ### 内訳
125
+
126
+ - Login (GET: ` /auth/login ` )
127
+ - ` openid-client ` の ` client.authorizationUrl ` を呼び出す
128
+ - OPの認証エンドポイントにリダイレクトする
129
+ - Callback (GET: ` /auth/callback ` )
130
+ - ` openid-client ` の ` client.callbackParams ` と ` client.callback ` を呼び出す
131
+ - OPからリダイレクトされた後、ログイン完了ページにリダイレクトする
132
+ - Logout (GET: ` /auth/logout ` )
133
+ - ` openid-client ` の ` client.endSessionUrl ` を呼び出す
134
+ - OPのログアウトエンドポイントにリダイレクトする
135
+ - UserInfo (ALL: ` /auth/userinfo ` )
136
+ - ` openid-client ` の ` client.userinfo ` を呼び出す
137
+ - レスポンス(ユーザー情報)をそのまま返す
138
+ - Introspect (ALL: ` /auth/introspect ` )
139
+ - ` openid-client ` の ` client.introspect ` を呼び出す
140
+ - レスポンスをそのまま返す
141
+ - Refresh (ALL: ` /auth/refresh ` )
142
+ - ` openid-client ` の ` client.refresh ` を呼び出す
143
+ - ID Tokenに含まれるクレームを返す
144
+ - Resource (GET: ` /auth/resource?url=<resource-url> ` )
145
+ - ` openid-client ` の ` client.requestResource ` を呼び出す
146
+ - リソースプロバイダーからのレスポンスを返す
147
+ - Revoke (ALL: ` /auth/revoke ` )
148
+ - ` openid-client ` の ` client.revoke ` を呼び出す
149
+ - ` 204 ` を返す
150
+ - Status (ALL: ` /auth/status ` )
151
+ - 内部データベースからセッションステータスを取得する
152
+ - OPにはアクセスしない
153
+ - Claims (ALL: ` /auth/claims ` )
154
+ - ID Tokenに含まれるクレームを取得する
155
+ - OPにはアクセスしない
156
+
157
+ ## フック
158
+
159
+ ` onBeforeHandle ` フックでCookieを元にセッションが有効かどうかを判断し、 [ ` resolve ` フック] ( https://elysiajs.com/life-cycle/before-handle.html#resolve ) から ` sessionStatus ` と ` sessionClaims ` を返す。
160
+
161
+ - セッションが有効な場合
162
+ - ` sessionStatus ` : セッションステータス
163
+ - 参照: [ OIDCClientSessionStatus] ( https://macropygia.github.io/elysia-openid-client/interfaces/types.OIDCClientSessionStatus.html )
164
+ - ` sessionClaims ` : ID Token Claims
165
+ - 参照: [ ` IdTokenClaims ` type definition] ( https://github.com/panva/node-openid-client/blob/main/types/index.d.ts ) of ` openid-client ` .
166
+ - 参照: ` OpenID Connect Core 1.0 ` の [ Claims] ( https://openid.net/specs/openid-connect-core-1_0.html#Claims ) 及び [ IDToken] ( https://openid.net/specs/openid-connect-core-1_0.html#IDToken ) の章
167
+ - セッションが無効な場合
168
+ - ` loginRedirectUrl ` にリダイレクト
169
+ - ` disableRedirect ` が ` true ` の場合は ` sessionStatus ` , ` sessionClaims ` 共に ` null ` になる
170
+ - ElysiaJSプラグインとしてのメタデータ
171
+ - 名称: ` elysia-openid-client-auth-hook `
172
+ - [ シード] ( https://elysiajs.com/essential/plugin#plugin-deduplication ) : ` settings.pluginSeed ` 、未指定なら ` issuerUrl `
173
+ - 参照: [ AuthHookOptions] ( https://macropygia.github.io/elysia-openid-client/interfaces/types.AuthHookOptions.html )
174
+
114
175
## データアダプター
115
176
116
177
セッション情報の保存方法を定義したもの。
117
178
118
179
``` typescript
119
- const client = await OidcClient .create ({
180
+ const rp = await OidcClient .create ({
120
181
// ...
121
- dataAdapter: < data - adapter > ,
182
+ dataAdapter: OIDCClientDataAdapter ,
122
183
// ...
123
184
})
124
185
```
125
186
126
187
- 本パッケージにはSQLite/LokiJS/Lowdb/Redisを使用したデータアダプターが含まれる
127
188
- カスタムデータアダプターを作成可能
128
- - 参照: [ OIDCClientDataAdapter] ( https://macropygia.github.io/elysia-openid-client/interfaces/types.OIDCClientDataAdapter.html )
129
189
- 既定ではSQLiteのインメモリーモードが使用される
130
190
- 複数のOPを使用する場合は一つのデータアダプターを共有する
191
+ - 参照: [ OIDCClientDataAdapter] ( https://macropygia.github.io/elysia-openid-client/interfaces/types.OIDCClientDataAdapter.html )
131
192
132
193
### SQLite
133
194
@@ -215,7 +276,7 @@ export class MyDataAdapter implements OIDCClientDataAdapter {
215
276
216
277
// app.ts
217
278
import { MyDataAdapter } from ' path/to/MyDataAdapter' ;
218
- const client = await OidcClient .create ({
279
+ const rp = await OidcClient .create ({
219
280
// ...
220
281
dataAdapter: new MyDataAdapter (),
221
282
// ...
@@ -227,9 +288,9 @@ const client = await OidcClient.create({
227
288
ロガーを定義する。
228
289
229
290
``` typescript
230
- const client = await OidcClient .create ({
291
+ const rp = await OidcClient .create ({
231
292
// ...
232
- logger: < logger > ,
293
+ logger: OIDCClientLogger | null ,
233
294
// ...
234
295
})
235
296
```
@@ -238,6 +299,7 @@ const client = await OidcClient.create({
238
299
- 変換すれば任意のロガーを使用可能
239
300
- 省略すると ` consoleLogger("info") ` を使用する
240
301
- ` null ` に設定するとログを出力しない
302
+ - 参照: [ OIDCClientLogger] ( https://macropygia.github.io/elysia-openid-client/interfaces/types.OIDCClientLogger.html )
241
303
242
304
### ログレベルポリシー
243
305
@@ -257,16 +319,17 @@ const client = await OidcClient.create({
257
319
258
320
### pinoの使用
259
321
322
+ 直接[ pino] ( https://getpino.io/ ) を割り当てられる
323
+
260
324
``` bash
261
325
bun add pino
262
326
```
263
327
264
328
``` typescript
265
329
import pino from " pino" ;
266
- const logger = pino ();
267
- const client = await OidcClient .create ({
330
+ const rp = await OidcClient .create ({
268
331
// ...
269
- logger ,
332
+ logger: pino () ,
270
333
// ...
271
334
})
272
335
```
@@ -277,8 +340,8 @@ const client = await OidcClient.create({
277
340
278
341
``` typescript
279
342
import { consoleLogger } from " elysia-openid-client/loggers/consoleLogger" ;
280
- const minimumLogLevel = " debug" ;
281
- const client = await OidcClient .create ({
343
+ const minimumLogLevel = " debug" ; // pinoと同様
344
+ const rp = await OidcClient .create ({
282
345
// ...
283
346
logger: consoleLogger (minimumLogLevel ),
284
347
// ...
@@ -287,68 +350,8 @@ const client = await OidcClient.create({
287
350
288
351
### カスタムロガー
289
352
290
- ` consoleLogger ` の実装を参照のこと。
291
-
292
- ## エンドポイント
293
-
294
- - Login (GET: ` /auth/login ` )
295
- - ` openid-client ` の ` client.authorizationUrl ` を呼び出す
296
- - OPの認証エンドポイントにリダイレクトする
297
- - Callback (GET: ` /auth/callback ` )
298
- - ` openid-client ` の ` client.callbackParams ` と ` client.callback ` を呼び出す
299
- - OPからリダイレクトされた後、ログイン完了ページにリダイレクトする
300
- - Logout (GET: ` /auth/logout ` )
301
- - ` openid-client ` の ` client.endSessionUrl ` を呼び出す
302
- - OPのログアウトエンドポイントにリダイレクトする
303
- - UserInfo (ALL: ` /auth/userinfo ` )
304
- - ` openid-client ` の ` client.userinfo ` を呼び出す
305
- - レスポンス(ユーザー情報)をそのまま返す
306
- - Introspect (ALL: ` /auth/introspect ` )
307
- - ` openid-client ` の ` client.introspect ` を呼び出す
308
- - レスポンスをそのまま返す
309
- - Refresh (ALL: ` /auth/refresh ` )
310
- - ` openid-client ` の ` client.refresh ` を呼び出す
311
- - ID Tokenに含まれるクレームを返す
312
- - Resouce (GET: ` /auth/resource?url=<resource-url> ` )
313
- - ` openid-client ` の ` client.requestResource ` を呼び出す
314
- - リソースプロバイダーからのレスポンスを返す
315
- - Revoke (ALL: ` /auth/revoke ` )
316
- - ` openid-client ` の ` client.revoke ` を呼び出す
317
- - ` 204 ` を返す
318
- - Status (ALL: ` /auth/status ` )
319
- - 内部データベースからセッションステータスを取得する
320
- - OPにはアクセスしない
321
- - Claims (ALL: ` /auth/claims ` )
322
- - ID Tokenに含まれるクレームを取得する
323
- - OPにはアクセスしない
324
-
325
- ## フック
326
-
327
- ` onBeforeHook ` フックでCookieを元にセッションが有効かどうかを判断し、 [ ` resolve ` フック] ( https://elysiajs.com/life-cycle/before-handle.html#resolve ) から ` sessionStatus ` と ` sessionClaims ` を返す。
328
-
329
- - セッションが有効な場合
330
- - ` sessionStatus ` : セッションステータス
331
- - ` sessionClaims ` : ID Token Claims
332
- - セッションが無効な場合
333
- - ` loginRedirectUrl ` にリダイレクト
334
- - ` disableRedirect ` が ` false ` の場合は ` sessionStatus ` , ` sessionClaims ` 共に ` null `
335
- - 設定
336
- - [ AuthHookOptions] ( https://macropygia.github.io/elysia-openid-client/interfaces/types.AuthHookOptions.html ) .
337
-
338
- ``` typescript
339
- const rp = await OidcClient .create (clientOptions );
340
-
341
- const hookOptions: AuthHookOptions = {
342
- scope: " scoped" ,
343
- loginRedirectUrl: " /auth/login" ,
344
- disableRedirect: false ,
345
- autoRefresh: true ,
346
- }
347
-
348
- const hook = rp .getAuthHook (hookOptions );
349
- ```
353
+ [ OIDCClientLogger] ( https://macropygia.github.io/elysia-openid-client/interfaces/types.OIDCClientLogger.html ) の型定義と ` consoleLogger ` の実装を参照のこと。
350
354
351
355
## Contributing
352
356
353
- 本リポジトリに提供するコードを ` GitHub Copilot ` で生成する場合、必ず ` Suggestions matching public code ` オプションを ` Block ` に設定すること。
354
- 同様のオプションが存在する類似のサービスを使用する場合も同様。
357
+ 本リポジトリに提供するコードを ` GitHub Copilot ` で生成する場合、必ず ` Suggestions matching public code ` オプションを ` Block ` に設定すること。同様のオプションが存在する類似のサービスを使用する場合も同様。
0 commit comments