1
+ import { createAuthHook } from "@/methods/createAuthHook" ;
2
+ import { createEndpoints } from "@/methods/createEndpoints" ;
1
3
import { createSession } from "@/methods/createSession" ;
2
4
import { deleteSession } from "@/methods/deleteSession" ;
3
5
import { fetchSession } from "@/methods/fetchSession" ;
4
- import { getAuthHook } from "@/methods/getAuthHook" ;
5
- import { getEndpoints } from "@/methods/getEndpoints" ;
6
6
import { updateSession } from "@/methods/updateSession" ;
7
- import type {
8
- AuthHookOptions ,
9
- OIDCClientActiveSession ,
10
- OIDCClientOptions ,
11
- } from "@/types" ;
7
+ import type { OIDCClientActiveSession , OIDCClientOptions } from "@/types" ;
12
8
import { t } from "elysia" ;
13
9
import type { TokenSet } from "openid-client" ;
14
10
import { BaseOidcClient } from "./BaseOidcClient" ;
@@ -17,10 +13,19 @@ import { BaseOidcClient } from "./BaseOidcClient";
17
13
* OpenID Connect client plugin for ElysiaJS
18
14
* - Usage:
19
15
* - `const client = await BaseOidcClient.factory(options);`
20
- * - `const endpoints = client.getEndpoints ();`
21
- * - `const hook = client.getAuthHook ();`
16
+ * - `const endpoints = client.createEndpoints ();`
17
+ * - `const hook = client.createAuthHook ();`
22
18
*/
23
19
export class OidcClient extends BaseOidcClient {
20
+ protected constructor ( options : OIDCClientOptions ) {
21
+ super ( options ) ;
22
+
23
+ this . createSession = createSession . bind ( this ) ;
24
+ this . updateSession = updateSession . bind ( this ) ;
25
+ this . fetchSession = fetchSession . bind ( this ) ;
26
+ this . deleteSession = deleteSession . bind ( this ) ;
27
+ }
28
+
24
29
/**
25
30
* Create OidcClient instance
26
31
* @param options
@@ -38,66 +43,61 @@ export class OidcClient extends BaseOidcClient {
38
43
* @public
39
44
* @returns [sessionId, authorizationUrl]
40
45
*/
41
- public createSession = ( ) => createSession . call ( this ) ;
46
+ public createSession : ( ) => Promise < [ string , string ] > ;
42
47
43
48
/**
44
49
* Update session in DB
45
50
* @public
46
51
* @param sessionId Session ID
47
52
* @param tokenSet TokenSet
48
53
*/
49
- public updateSession = async (
54
+ public updateSession : (
50
55
sessionId : string ,
51
56
tokenSet : TokenSet ,
52
- ) : Promise < OIDCClientActiveSession | null > =>
53
- updateSession . call ( this , sessionId , tokenSet ) ;
57
+ ) => Promise < OIDCClientActiveSession | null > ;
54
58
55
59
/**
56
60
* Find and validate session from cookie and DB
57
61
* @public
58
62
* @param sessionId Sessison ID
59
63
* @returns Session data or false
60
64
*/
61
- public fetchSession = async (
65
+ public fetchSession : (
62
66
sessionId : string | undefined ,
63
- ) : Promise < OIDCClientActiveSession | null > =>
64
- fetchSession . call ( this , sessionId ) ;
67
+ ) => Promise < OIDCClientActiveSession | null > ;
65
68
66
69
/**
67
70
* Delete session from DB
68
71
* @protected
69
72
* @param sessionId Session ID
70
73
*/
71
- public deleteSession = async ( sessionId : string ) =>
72
- deleteSession . call ( this , sessionId ) ;
73
-
74
- /**
75
- * Get session id cookie type definition for schema
76
- * @returns Type definition
77
- */
78
- public getSessionIdCookieType = ( ) => ( {
79
- [ this . cookieSettings . sessionIdName ] : t . Optional ( t . String ( ) ) ,
80
- } ) ;
74
+ public deleteSession : ( sessionId : string ) => Promise < void > ;
81
75
82
76
/**
83
77
* Cookie definition for ElysiaJS
84
78
* @public
85
79
*/
86
- public getCookieDefinition = ( ) => t . Cookie ( this . getSessionIdCookieType ( ) ) ;
80
+ public get cookieTypeBox ( ) {
81
+ return t . Cookie ( {
82
+ [ this . cookieSettings . sessionIdName ] : t . Optional ( t . String ( ) ) ,
83
+ } ) ;
84
+ }
87
85
88
86
/**
89
87
* Get onBeforeHandle for restricted endpoints
90
88
* @public
91
- * @param options Options
92
89
* @returns ElysiaJS Plugin
93
90
*/
94
- public getAuthHook = ( options ?: Partial < AuthHookOptions > ) =>
95
- getAuthHook . call ( this , options ) ;
91
+ public get authHook ( ) {
92
+ return createAuthHook . call ( this ) ;
93
+ }
96
94
97
95
/**
98
96
* OpenID Connect client plugin for ElysiaJS
99
97
* @public
100
98
* @returns ElysiaJS Plugin
101
99
*/
102
- public getEndpoints = ( ) => getEndpoints . call ( this ) ;
100
+ public get endpoints ( ) {
101
+ return createEndpoints . call ( this ) ;
102
+ }
103
103
}
0 commit comments