@navios/jwt 0.1.0

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.
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2025 Oleksandr Hanzha
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # Navios JWT
2
+
3
+ ## Overview
4
+
5
+ Navios JWT is a TypeScript library that provides a robust implementation for JSON Web Token (JWT) for a Navios framework operations in your applications. It wraps the functionality of the `jsonwebtoken` library with a service-oriented approach that integrates with the Navios dependency injection system.
6
+
7
+ It was forked from a [NestJS](https://github.com/nestjs/jwt) library and is designed to be used with the Navios framework, providing a seamless experience for signing and verifying JWTs.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @navios/jwt
13
+ # or
14
+ yarn add @navios/jwt
15
+ ```
16
+
17
+ ## Features
18
+
19
+ - Token signing with customizable options
20
+ - Token verification with comprehensive validation
21
+ - Support for various JWT algorithms (HS256, RS256, ES256, etc.)
22
+ - Integration with Navios dependency injection system
23
+ - Type-safe API with Zod schema validation
24
+
25
+ ## Usage
26
+
27
+ ### Basic Setup
28
+
29
+ ```typescript
30
+ import { inject } from '@navios/core';
31
+ import { provideJwtService } from '@navios/jwt';
32
+
33
+
34
+ const MyJwtService = provideJwtService({
35
+ secret: 'your-secret-key',
36
+ signOptions: {
37
+ expiresIn: '1h'
38
+ },
39
+ })
40
+ // Or with factory
41
+ const JwtService = provideJwtService(async () => {
42
+ const config = await inject(ConfigService);
43
+ return config.jwt
44
+ })
45
+ ```
46
+
47
+ ### Signing Tokens
48
+
49
+ ```typescript
50
+ // Create a token
51
+ import { Injectable, syncInject } from '@navios/core'
52
+ import { JwtService } from '../service/jwt.service.mjs'
53
+ //or to load without options
54
+ import { JwtService } from '@navios/jwt'
55
+
56
+ @Injectable()
57
+ class AuthService {
58
+ jwtService = syncInject(JwtService)
59
+
60
+ async generateToken(userId: number, role: string) {
61
+ const token = await this.jwtService.signAsync({ userId, role });
62
+ return token;
63
+ }
64
+ }
65
+
66
+ ```
67
+
68
+ ### Verifying Tokens
69
+
70
+ ```typescript
71
+ try {
72
+ // Verify and decode a token
73
+ const payload = await jwtService.verify(token);
74
+ console.log(payload); // { userId: 123, role: 'admin', iat: 1234567890, exp: 1234571490 }
75
+ } catch (error) {
76
+ // Handle verification errors
77
+ if (error instanceof TokenExpiredError) {
78
+ console.error('Token expired');
79
+ } else if (error instanceof JsonWebTokenError) {
80
+ console.error('Invalid token');
81
+ }
82
+ }
83
+ ```
84
+
85
+ ## API Reference
86
+
87
+ ### JwtServiceOptions
88
+
89
+ Configuration options for the JWT service:
90
+
91
+ ```typescript
92
+ interface JwtServiceOptions {
93
+ signOptions?: SignOptions;
94
+ secret?: string;
95
+ publicKey?: string | Buffer;
96
+ privateKey?: Secret;
97
+ verifyOptions?: VerifyOptions;
98
+ secretOrKeyProvider?: (
99
+ requestType: RequestType,
100
+ token?: string,
101
+ options?: SignOptions | VerifyOptions
102
+ ) => Secret | Promise<Secret>;
103
+ }
104
+ ```
105
+
106
+ ### Error Handling
107
+
108
+ The library exports error classes from the underlying `jsonwebtoken` library:
109
+ - `TokenExpiredError`: Thrown when the token is expired
110
+ - `NotBeforeError`: Thrown when the token is not yet valid
111
+ - `JsonWebTokenError`: Base class for all JWT errors
112
+
113
+ ## Contributing
114
+
115
+ Contributions are welcome! Please feel free to submit a Pull Request.
116
+
117
+ ## License
118
+
119
+ [MIT](LICENSE)