@twin.org/api-auth-entity-storage-service 0.0.1-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +21 -0
  3. package/dist/cjs/index.cjs +623 -0
  4. package/dist/esm/index.mjs +611 -0
  5. package/dist/types/entities/authenticationUser.d.ts +21 -0
  6. package/dist/types/index.d.ts +10 -0
  7. package/dist/types/models/IAuthHeaderProcessorConfig.d.ts +15 -0
  8. package/dist/types/models/IEntityStorageAuthenticationServiceConfig.d.ts +15 -0
  9. package/dist/types/processors/authHeaderProcessor.d.ts +50 -0
  10. package/dist/types/restEntryPoints.d.ts +2 -0
  11. package/dist/types/routes/entityStorageAuthenticationRoutes.d.ts +37 -0
  12. package/dist/types/schema.d.ts +4 -0
  13. package/dist/types/services/entityStorageAuthenticationService.d.ts +55 -0
  14. package/dist/types/utils/passwordHelper.d.ts +12 -0
  15. package/dist/types/utils/tokenHelper.d.ts +41 -0
  16. package/docs/changelog.md +5 -0
  17. package/docs/examples.md +1 -0
  18. package/docs/reference/classes/AuthHeaderProcessor.md +149 -0
  19. package/docs/reference/classes/AuthenticationUser.md +45 -0
  20. package/docs/reference/classes/EntityStorageAuthenticationService.md +169 -0
  21. package/docs/reference/classes/PasswordHelper.md +37 -0
  22. package/docs/reference/classes/TokenHelper.md +125 -0
  23. package/docs/reference/functions/authenticationLogin.md +25 -0
  24. package/docs/reference/functions/authenticationLogout.md +25 -0
  25. package/docs/reference/functions/authenticationRefreshToken.md +25 -0
  26. package/docs/reference/functions/generateRestRoutesAuthentication.md +21 -0
  27. package/docs/reference/functions/initSchema.md +9 -0
  28. package/docs/reference/index.md +27 -0
  29. package/docs/reference/interfaces/IAuthHeaderProcessorConfig.md +31 -0
  30. package/docs/reference/interfaces/IEntityStorageAuthenticationServiceConfig.md +31 -0
  31. package/docs/reference/variables/restEntryPoints.md +3 -0
  32. package/docs/reference/variables/tagsAuthentication.md +5 -0
  33. package/locales/en.json +18 -0
  34. package/package.json +73 -0
@@ -0,0 +1,50 @@
1
+ import { type IHttpRequestIdentity, type IHttpResponse, type IHttpRestRouteProcessor, type IHttpServerRequest, type IRestRoute } from "@twin.org/api-models";
2
+ import type { IAuthHeaderProcessorConfig } from "../models/IAuthHeaderProcessorConfig";
3
+ /**
4
+ * Handle a JWT token in the authorization header or cookies and validate it to populate request context identity.
5
+ */
6
+ export declare class AuthHeaderProcessor implements IHttpRestRouteProcessor {
7
+ /**
8
+ * Runtime name for the class.
9
+ */
10
+ readonly CLASS_NAME: string;
11
+ /**
12
+ * Create a new instance of AuthCookiePreProcessor.
13
+ * @param options Options for the processor.
14
+ * @param options.vaultConnectorType The vault for the private keys, defaults to "vault".
15
+ * @param options.config The configuration for the processor.
16
+ */
17
+ constructor(options?: {
18
+ vaultConnectorType?: string;
19
+ config?: IAuthHeaderProcessorConfig;
20
+ });
21
+ /**
22
+ * The service needs to be started when the application is initialized.
23
+ * @param nodeIdentity The identity of the node.
24
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
25
+ * @returns Nothing.
26
+ */
27
+ start(nodeIdentity: string, nodeLoggingConnectorType?: string): Promise<void>;
28
+ /**
29
+ * Pre process the REST request for the specified route.
30
+ * @param request The incoming request.
31
+ * @param response The outgoing response.
32
+ * @param route The route to process.
33
+ * @param requestIdentity The identity context for the request.
34
+ * @param processorState The state handed through the processors.
35
+ */
36
+ pre(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
37
+ [id: string]: unknown;
38
+ }): Promise<void>;
39
+ /**
40
+ * Post process the REST request for the specified route.
41
+ * @param request The incoming request.
42
+ * @param response The outgoing response.
43
+ * @param route The route to process.
44
+ * @param requestIdentity The identity context for the request.
45
+ * @param processorState The state handed through the processors.
46
+ */
47
+ post(request: IHttpServerRequest, response: IHttpResponse, route: IRestRoute | undefined, requestIdentity: IHttpRequestIdentity, processorState: {
48
+ [id: string]: unknown;
49
+ }): Promise<void>;
50
+ }
@@ -0,0 +1,2 @@
1
+ import type { IRestRouteEntryPoint } from "@twin.org/api-models";
2
+ export declare const restEntryPoints: IRestRouteEntryPoint[];
@@ -0,0 +1,37 @@
1
+ import type { ILoginRequest, ILoginResponse, ILogoutRequest, IRefreshTokenRequest, IRefreshTokenResponse } from "@twin.org/api-auth-entity-storage-models";
2
+ import type { IHttpRequestContext, INoContentResponse, IRestRoute, IRestRouteResponseOptions, ITag } from "@twin.org/api-models";
3
+ /**
4
+ * The tag to associate with the routes.
5
+ */
6
+ export declare const tagsAuthentication: ITag[];
7
+ /**
8
+ * The REST routes for authentication.
9
+ * @param baseRouteName Prefix to prepend to the paths.
10
+ * @param componentName The name of the component to use in the routes stored in the ComponentFactory.
11
+ * @returns The generated routes.
12
+ */
13
+ export declare function generateRestRoutesAuthentication(baseRouteName: string, componentName: string): IRestRoute[];
14
+ /**
15
+ * Login to the server.
16
+ * @param httpRequestContext The request context for the API.
17
+ * @param componentName The name of the component to use in the routes.
18
+ * @param request The request.
19
+ * @returns The response object with additional http response properties.
20
+ */
21
+ export declare function authenticationLogin(httpRequestContext: IHttpRequestContext, componentName: string, request: ILoginRequest): Promise<ILoginResponse & IRestRouteResponseOptions>;
22
+ /**
23
+ * Logout from the server.
24
+ * @param httpRequestContext The request context for the API.
25
+ * @param componentName The name of the component to use in the routes.
26
+ * @param request The request.
27
+ * @returns The response object with additional http response properties.
28
+ */
29
+ export declare function authenticationLogout(httpRequestContext: IHttpRequestContext, componentName: string, request: ILogoutRequest): Promise<INoContentResponse & IRestRouteResponseOptions>;
30
+ /**
31
+ * Refresh the login token.
32
+ * @param httpRequestContext The request context for the API.
33
+ * @param componentName The name of the component to use in the routes.
34
+ * @param request The request.
35
+ * @returns The response object with additional http response properties.
36
+ */
37
+ export declare function authenticationRefreshToken(httpRequestContext: IHttpRequestContext, componentName: string, request: IRefreshTokenRequest): Promise<IRefreshTokenResponse & IRestRouteResponseOptions>;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Initialize the schema for the authentication service.
3
+ */
4
+ export declare function initSchema(): void;
@@ -0,0 +1,55 @@
1
+ import type { IAuthenticationComponent } from "@twin.org/api-auth-entity-storage-models";
2
+ import type { IEntityStorageAuthenticationServiceConfig } from "../models/IEntityStorageAuthenticationServiceConfig";
3
+ /**
4
+ * Implementation of the authentication component using entity storage.
5
+ */
6
+ export declare class EntityStorageAuthenticationService implements IAuthenticationComponent {
7
+ /**
8
+ * Runtime name for the class.
9
+ */
10
+ readonly CLASS_NAME: string;
11
+ /**
12
+ * Create a new instance of EntityStorageAuthentication.
13
+ * @param options The dependencies for the identity connector.
14
+ * @param options.userEntityStorageType The entity storage for the users, defaults to "authentication-user".
15
+ * @param options.vaultConnectorType The vault for the private keys, defaults to "vault".
16
+ * @param options.config The configuration for the authentication.
17
+ */
18
+ constructor(options?: {
19
+ userEntityStorageType?: string;
20
+ vaultConnectorType?: string;
21
+ config?: IEntityStorageAuthenticationServiceConfig;
22
+ });
23
+ /**
24
+ * The service needs to be started when the application is initialized.
25
+ * @param nodeIdentity The identity of the node.
26
+ * @param nodeLoggingConnectorType The node logging connector type, defaults to "node-logging".
27
+ * @returns Nothing.
28
+ */
29
+ start(nodeIdentity: string, nodeLoggingConnectorType?: string): Promise<void>;
30
+ /**
31
+ * Perform a login for the user.
32
+ * @param email The email address for the user.
33
+ * @param password The password for the user.
34
+ * @returns The authentication token for the user, if it uses a mechanism with public access.
35
+ */
36
+ login(email: string, password: string): Promise<{
37
+ token?: string;
38
+ expiry: number;
39
+ }>;
40
+ /**
41
+ * Logout the current user.
42
+ * @param token The token to logout, if it uses a mechanism with public access.
43
+ * @returns Nothing.
44
+ */
45
+ logout(token?: string): Promise<void>;
46
+ /**
47
+ * Refresh the token.
48
+ * @param token The token to refresh, if it uses a mechanism with public access.
49
+ * @returns The refreshed token, if it uses a mechanism with public access.
50
+ */
51
+ refresh(token?: string): Promise<{
52
+ token: string;
53
+ expiry: number;
54
+ }>;
55
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Helper class for password operations.
3
+ */
4
+ export declare class PasswordHelper {
5
+ /**
6
+ * Hash the password for the user.
7
+ * @param passwordBytes The password bytes.
8
+ * @param saltBytes The salt bytes.
9
+ * @returns The hashed password.
10
+ */
11
+ static hashPassword(passwordBytes: Uint8Array, saltBytes: Uint8Array): Promise<string>;
12
+ }
@@ -0,0 +1,41 @@
1
+ import type { IVaultConnector } from "@twin.org/vault-models";
2
+ import { type IHttpHeaders, type IJwtHeader, type IJwtPayload } from "@twin.org/web";
3
+ /**
4
+ * Helper class for token operations.
5
+ */
6
+ export declare class TokenHelper {
7
+ /**
8
+ * Create a new token.
9
+ * @param vaultConnector The vault connector.
10
+ * @param signingKeyName The signing key name.
11
+ * @param subject The subject for the token.
12
+ * @param ttlMinutes The time to live for the token in minutes.
13
+ * @returns The new token and its expiry date.
14
+ */
15
+ static createToken(vaultConnector: IVaultConnector, signingKeyName: string, subject: string, ttlMinutes: number): Promise<{
16
+ token: string;
17
+ expiry: number;
18
+ }>;
19
+ /**
20
+ * Verify the token.
21
+ * @param vaultConnector The vault connector.
22
+ * @param signingKeyName The signing key name.
23
+ * @param token The token to verify.
24
+ * @returns The verified details.
25
+ * @throws UnauthorizedError if the token is missing, invalid or expired.
26
+ */
27
+ static verify(vaultConnector: IVaultConnector, signingKeyName: string, token: string | undefined): Promise<{
28
+ header: IJwtHeader;
29
+ payload: IJwtPayload;
30
+ }>;
31
+ /**
32
+ * Extract the auth token from the headers, either from the authorization header or the cookie header.
33
+ * @param headers The headers to extract the token from.
34
+ * @param cookieName The name of the cookie to extract the token from.
35
+ * @returns The token if found.
36
+ */
37
+ static extractTokenFromHeaders(headers?: IHttpHeaders, cookieName?: string): {
38
+ token: string | undefined;
39
+ location: "authorization" | "cookie" | undefined;
40
+ };
41
+ }
@@ -0,0 +1,5 @@
1
+ # @twin.org/api-auth-entity-storage-service - Changelog
2
+
3
+ ## v0.0.1-next.2
4
+
5
+ - Initial Release
@@ -0,0 +1 @@
1
+ # @twin.org/api-auth-entity-storage-service - Examples
@@ -0,0 +1,149 @@
1
+ # Class: AuthHeaderProcessor
2
+
3
+ Handle a JWT token in the authorization header or cookies and validate it to populate request context identity.
4
+
5
+ ## Implements
6
+
7
+ - `IHttpRestRouteProcessor`
8
+
9
+ ## Constructors
10
+
11
+ ### new AuthHeaderProcessor()
12
+
13
+ > **new AuthHeaderProcessor**(`options`?): [`AuthHeaderProcessor`](AuthHeaderProcessor.md)
14
+
15
+ Create a new instance of AuthCookiePreProcessor.
16
+
17
+ #### Parameters
18
+
19
+ • **options?**
20
+
21
+ Options for the processor.
22
+
23
+ • **options.vaultConnectorType?**: `string`
24
+
25
+ The vault for the private keys, defaults to "vault".
26
+
27
+ • **options.config?**: [`IAuthHeaderProcessorConfig`](../interfaces/IAuthHeaderProcessorConfig.md)
28
+
29
+ The configuration for the processor.
30
+
31
+ #### Returns
32
+
33
+ [`AuthHeaderProcessor`](AuthHeaderProcessor.md)
34
+
35
+ ## Properties
36
+
37
+ ### CLASS\_NAME
38
+
39
+ > `readonly` **CLASS\_NAME**: `string`
40
+
41
+ Runtime name for the class.
42
+
43
+ #### Implementation of
44
+
45
+ `IHttpRestRouteProcessor.CLASS_NAME`
46
+
47
+ ## Methods
48
+
49
+ ### start()
50
+
51
+ > **start**(`nodeIdentity`, `nodeLoggingConnectorType`?): `Promise`\<`void`\>
52
+
53
+ The service needs to be started when the application is initialized.
54
+
55
+ #### Parameters
56
+
57
+ • **nodeIdentity**: `string`
58
+
59
+ The identity of the node.
60
+
61
+ • **nodeLoggingConnectorType?**: `string`
62
+
63
+ The node logging connector type, defaults to "node-logging".
64
+
65
+ #### Returns
66
+
67
+ `Promise`\<`void`\>
68
+
69
+ Nothing.
70
+
71
+ #### Implementation of
72
+
73
+ `IHttpRestRouteProcessor.start`
74
+
75
+ ***
76
+
77
+ ### pre()
78
+
79
+ > **pre**(`request`, `response`, `route`, `requestIdentity`, `processorState`): `Promise`\<`void`\>
80
+
81
+ Pre process the REST request for the specified route.
82
+
83
+ #### Parameters
84
+
85
+ • **request**: `IHttpServerRequest`\<`any`\>
86
+
87
+ The incoming request.
88
+
89
+ • **response**: `IHttpResponse`\<`any`\>
90
+
91
+ The outgoing response.
92
+
93
+ • **route**: `undefined` \| `IRestRoute`\<`any`, `any`\>
94
+
95
+ The route to process.
96
+
97
+ • **requestIdentity**: `IHttpRequestIdentity`
98
+
99
+ The identity context for the request.
100
+
101
+ • **processorState**
102
+
103
+ The state handed through the processors.
104
+
105
+ #### Returns
106
+
107
+ `Promise`\<`void`\>
108
+
109
+ #### Implementation of
110
+
111
+ `IHttpRestRouteProcessor.pre`
112
+
113
+ ***
114
+
115
+ ### post()
116
+
117
+ > **post**(`request`, `response`, `route`, `requestIdentity`, `processorState`): `Promise`\<`void`\>
118
+
119
+ Post process the REST request for the specified route.
120
+
121
+ #### Parameters
122
+
123
+ • **request**: `IHttpServerRequest`\<`any`\>
124
+
125
+ The incoming request.
126
+
127
+ • **response**: `IHttpResponse`\<`any`\>
128
+
129
+ The outgoing response.
130
+
131
+ • **route**: `undefined` \| `IRestRoute`\<`any`, `any`\>
132
+
133
+ The route to process.
134
+
135
+ • **requestIdentity**: `IHttpRequestIdentity`
136
+
137
+ The identity context for the request.
138
+
139
+ • **processorState**
140
+
141
+ The state handed through the processors.
142
+
143
+ #### Returns
144
+
145
+ `Promise`\<`void`\>
146
+
147
+ #### Implementation of
148
+
149
+ `IHttpRestRouteProcessor.post`
@@ -0,0 +1,45 @@
1
+ # Class: AuthenticationUser
2
+
3
+ Class defining the storage for user login credentials.
4
+
5
+ ## Constructors
6
+
7
+ ### new AuthenticationUser()
8
+
9
+ > **new AuthenticationUser**(): [`AuthenticationUser`](AuthenticationUser.md)
10
+
11
+ #### Returns
12
+
13
+ [`AuthenticationUser`](AuthenticationUser.md)
14
+
15
+ ## Properties
16
+
17
+ ### email
18
+
19
+ > **email**: `string`
20
+
21
+ The user e-mail address.
22
+
23
+ ***
24
+
25
+ ### password
26
+
27
+ > **password**: `string`
28
+
29
+ The encrypted password for the user.
30
+
31
+ ***
32
+
33
+ ### salt
34
+
35
+ > **salt**: `string`
36
+
37
+ The salt for the password.
38
+
39
+ ***
40
+
41
+ ### identity
42
+
43
+ > **identity**: `string`
44
+
45
+ The user identity.
@@ -0,0 +1,169 @@
1
+ # Class: EntityStorageAuthenticationService
2
+
3
+ Implementation of the authentication component using entity storage.
4
+
5
+ ## Implements
6
+
7
+ - `IAuthenticationComponent`
8
+
9
+ ## Constructors
10
+
11
+ ### new EntityStorageAuthenticationService()
12
+
13
+ > **new EntityStorageAuthenticationService**(`options`?): [`EntityStorageAuthenticationService`](EntityStorageAuthenticationService.md)
14
+
15
+ Create a new instance of EntityStorageAuthentication.
16
+
17
+ #### Parameters
18
+
19
+ • **options?**
20
+
21
+ The dependencies for the identity connector.
22
+
23
+ • **options.userEntityStorageType?**: `string`
24
+
25
+ The entity storage for the users, defaults to "authentication-user".
26
+
27
+ • **options.vaultConnectorType?**: `string`
28
+
29
+ The vault for the private keys, defaults to "vault".
30
+
31
+ • **options.config?**: [`IEntityStorageAuthenticationServiceConfig`](../interfaces/IEntityStorageAuthenticationServiceConfig.md)
32
+
33
+ The configuration for the authentication.
34
+
35
+ #### Returns
36
+
37
+ [`EntityStorageAuthenticationService`](EntityStorageAuthenticationService.md)
38
+
39
+ ## Properties
40
+
41
+ ### CLASS\_NAME
42
+
43
+ > `readonly` **CLASS\_NAME**: `string`
44
+
45
+ Runtime name for the class.
46
+
47
+ #### Implementation of
48
+
49
+ `IAuthenticationComponent.CLASS_NAME`
50
+
51
+ ## Methods
52
+
53
+ ### start()
54
+
55
+ > **start**(`nodeIdentity`, `nodeLoggingConnectorType`?): `Promise`\<`void`\>
56
+
57
+ The service needs to be started when the application is initialized.
58
+
59
+ #### Parameters
60
+
61
+ • **nodeIdentity**: `string`
62
+
63
+ The identity of the node.
64
+
65
+ • **nodeLoggingConnectorType?**: `string`
66
+
67
+ The node logging connector type, defaults to "node-logging".
68
+
69
+ #### Returns
70
+
71
+ `Promise`\<`void`\>
72
+
73
+ Nothing.
74
+
75
+ #### Implementation of
76
+
77
+ `IAuthenticationComponent.start`
78
+
79
+ ***
80
+
81
+ ### login()
82
+
83
+ > **login**(`email`, `password`): `Promise`\<`object`\>
84
+
85
+ Perform a login for the user.
86
+
87
+ #### Parameters
88
+
89
+ • **email**: `string`
90
+
91
+ The email address for the user.
92
+
93
+ • **password**: `string`
94
+
95
+ The password for the user.
96
+
97
+ #### Returns
98
+
99
+ `Promise`\<`object`\>
100
+
101
+ The authentication token for the user, if it uses a mechanism with public access.
102
+
103
+ ##### token?
104
+
105
+ > `optional` **token**: `string`
106
+
107
+ ##### expiry
108
+
109
+ > **expiry**: `number`
110
+
111
+ #### Implementation of
112
+
113
+ `IAuthenticationComponent.login`
114
+
115
+ ***
116
+
117
+ ### logout()
118
+
119
+ > **logout**(`token`?): `Promise`\<`void`\>
120
+
121
+ Logout the current user.
122
+
123
+ #### Parameters
124
+
125
+ • **token?**: `string`
126
+
127
+ The token to logout, if it uses a mechanism with public access.
128
+
129
+ #### Returns
130
+
131
+ `Promise`\<`void`\>
132
+
133
+ Nothing.
134
+
135
+ #### Implementation of
136
+
137
+ `IAuthenticationComponent.logout`
138
+
139
+ ***
140
+
141
+ ### refresh()
142
+
143
+ > **refresh**(`token`?): `Promise`\<`object`\>
144
+
145
+ Refresh the token.
146
+
147
+ #### Parameters
148
+
149
+ • **token?**: `string`
150
+
151
+ The token to refresh, if it uses a mechanism with public access.
152
+
153
+ #### Returns
154
+
155
+ `Promise`\<`object`\>
156
+
157
+ The refreshed token, if it uses a mechanism with public access.
158
+
159
+ ##### token
160
+
161
+ > **token**: `string`
162
+
163
+ ##### expiry
164
+
165
+ > **expiry**: `number`
166
+
167
+ #### Implementation of
168
+
169
+ `IAuthenticationComponent.refresh`
@@ -0,0 +1,37 @@
1
+ # Class: PasswordHelper
2
+
3
+ Helper class for password operations.
4
+
5
+ ## Constructors
6
+
7
+ ### new PasswordHelper()
8
+
9
+ > **new PasswordHelper**(): [`PasswordHelper`](PasswordHelper.md)
10
+
11
+ #### Returns
12
+
13
+ [`PasswordHelper`](PasswordHelper.md)
14
+
15
+ ## Methods
16
+
17
+ ### hashPassword()
18
+
19
+ > `static` **hashPassword**(`passwordBytes`, `saltBytes`): `Promise`\<`string`\>
20
+
21
+ Hash the password for the user.
22
+
23
+ #### Parameters
24
+
25
+ • **passwordBytes**: `Uint8Array`
26
+
27
+ The password bytes.
28
+
29
+ • **saltBytes**: `Uint8Array`
30
+
31
+ The salt bytes.
32
+
33
+ #### Returns
34
+
35
+ `Promise`\<`string`\>
36
+
37
+ The hashed password.