@darco2903/auth-api 2.0.4-beta.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 +674 -0
- package/README.md +68 -0
- package/dist/client.d.ts +1477 -0
- package/dist/client.js +32 -0
- package/dist/common.d.ts +4 -0
- package/dist/common.js +4 -0
- package/dist/consts.d.ts +10 -0
- package/dist/consts.js +9 -0
- package/dist/contract/assets.d.ts +356 -0
- package/dist/contract/assets.js +45 -0
- package/dist/contract/auth.d.ts +1386 -0
- package/dist/contract/auth.js +163 -0
- package/dist/contract/index.d.ts +3581 -0
- package/dist/contract/index.js +16 -0
- package/dist/contract/key.d.ts +17 -0
- package/dist/contract/key.js +15 -0
- package/dist/contract/requests.d.ts +723 -0
- package/dist/contract/requests.js +77 -0
- package/dist/contract/user.d.ts +1112 -0
- package/dist/contract/user.js +107 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/roles.d.ts +11 -0
- package/dist/roles.js +12 -0
- package/dist/server.d.ts +11 -0
- package/dist/server.js +58 -0
- package/dist/types/assets.d.ts +5 -0
- package/dist/types/assets.js +3 -0
- package/dist/types/auth.d.ts +26 -0
- package/dist/types/auth.js +9 -0
- package/dist/types/creds.d.ts +8 -0
- package/dist/types/creds.js +44 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.js +5 -0
- package/dist/types/jwt.d.ts +73 -0
- package/dist/types/jwt.js +23 -0
- package/dist/types/user.d.ts +90 -0
- package/dist/types/user.js +26 -0
- package/dist/types.d.ts +32 -0
- package/dist/types.js +13 -0
- package/package.json +47 -0
- package/tools/generate-openapi.ts +25 -0
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Authentication API
|
|
2
|
+
|
|
3
|
+
## Description
|
|
4
|
+
|
|
5
|
+
This package provides an authentication API for Darco2903's services, including JWT token signing and verification. It includes both client and server utilities to facilitate the implementation of authentication across multiple services.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @darco2903/auth-api
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Creating a client
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createClient } from "@darco2903/auth-api/client";
|
|
19
|
+
|
|
20
|
+
const SERVER_ORIGIN = "https://auth.example.com";
|
|
21
|
+
const authApi = createAuthClient(SERVER_ORIGIN);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Signing a JWT token
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import {
|
|
28
|
+
JWTSign,
|
|
29
|
+
type AccessTokenData,
|
|
30
|
+
UserRole,
|
|
31
|
+
} from "@darco2903/auth-api/server";
|
|
32
|
+
import { Hour } from "@darco2903/secondthought";
|
|
33
|
+
|
|
34
|
+
const JWT_PRIVATE_KEY = "";
|
|
35
|
+
const tokenData: AccessTokenData = {
|
|
36
|
+
public_id: "user_public_id",
|
|
37
|
+
role: UserRole.User,
|
|
38
|
+
totp_required: false,
|
|
39
|
+
totp_verified: false,
|
|
40
|
+
};
|
|
41
|
+
const expiresIn = new Hour(1); // Token expires in 1 hour. Also accepts a number of seconds (e.g., 3600).
|
|
42
|
+
|
|
43
|
+
await JWTSign(tokenData, JWT_PRIVATE_KEY, expiresIn).match(
|
|
44
|
+
(signedToken) => {
|
|
45
|
+
console.log("JWT signing successful:", signedToken);
|
|
46
|
+
},
|
|
47
|
+
(err) => {
|
|
48
|
+
console.error(`JWT signing failed: ${err.message}`);
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Verifying a JWT token
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { JWTVerify } from "@darco2903/auth-api/server";
|
|
57
|
+
|
|
58
|
+
const accessToken = "..."; // Your JWT token here
|
|
59
|
+
|
|
60
|
+
await JWTVerify(accessToken, JWT_PUBLIC_KEY).match(
|
|
61
|
+
(decodedToken) => {
|
|
62
|
+
console.log("JWT verification successful:", decodedToken);
|
|
63
|
+
},
|
|
64
|
+
(err) => {
|
|
65
|
+
console.error(`JWT verification failed: ${err.message}`);
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
```
|