@minimoth/sdk-node 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 +21 -0
- package/README.md +216 -0
- package/dist/index.cjs +556 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +144 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.js +514 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
|
|
3
|
+
type MiniMothErrorCode = 'TOKEN_EXPIRED' | 'TOKEN_REVOKED' | 'INVALID_TOKEN' | 'INVALID_OTP' | 'OTP_EXPIRED' | 'RATE_LIMITED' | 'INSUFFICIENT_BALANCE' | 'NETWORK_ERROR' | 'INVALID_PHONE' | 'UNKNOWN_ERROR';
|
|
4
|
+
declare class MiniMothError extends Error {
|
|
5
|
+
readonly code: MiniMothErrorCode;
|
|
6
|
+
readonly statusCode: number;
|
|
7
|
+
constructor(code: MiniMothErrorCode, message: string, statusCode: number, cause?: unknown);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface MiniMothSessionStore {
|
|
11
|
+
getRefreshToken(sessionId: string): Promise<string | null>;
|
|
12
|
+
setTokens(sessionId: string, tokens: {
|
|
13
|
+
accessToken: string;
|
|
14
|
+
refreshToken: string;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
deleteSession(sessionId: string): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface MiniMothLogger {
|
|
20
|
+
debug(msg: string, meta?: Record<string, unknown>): void;
|
|
21
|
+
info(msg: string, meta?: Record<string, unknown>): void;
|
|
22
|
+
warn(msg: string, meta?: Record<string, unknown>): void;
|
|
23
|
+
error(msg: string, meta?: Record<string, unknown>): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type VerifyResult = {
|
|
27
|
+
valid: true;
|
|
28
|
+
accessToken: string;
|
|
29
|
+
refreshToken: string;
|
|
30
|
+
sessionId: string;
|
|
31
|
+
} | {
|
|
32
|
+
valid: false;
|
|
33
|
+
code: MiniMothErrorCode;
|
|
34
|
+
};
|
|
35
|
+
declare class OtpClient {
|
|
36
|
+
private readonly apiKey;
|
|
37
|
+
private readonly store;
|
|
38
|
+
private readonly logger;
|
|
39
|
+
constructor(apiKey: string, store: MiniMothSessionStore, logger: MiniMothLogger);
|
|
40
|
+
send(params: {
|
|
41
|
+
phone: string;
|
|
42
|
+
}): Promise<void>;
|
|
43
|
+
verify(params: {
|
|
44
|
+
phone: string;
|
|
45
|
+
otp: string;
|
|
46
|
+
}): Promise<VerifyResult>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare class JwksCache {
|
|
50
|
+
private readonly logger;
|
|
51
|
+
private keyMap;
|
|
52
|
+
private fetchedAt;
|
|
53
|
+
private fetching;
|
|
54
|
+
constructor(logger: MiniMothLogger);
|
|
55
|
+
getKey(kid: string): Promise<crypto.KeyObject>;
|
|
56
|
+
private fetch;
|
|
57
|
+
private doFetch;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type ValidateMode = 'instant' | 'recheck_1m' | 'recheck_3m' | 'strict';
|
|
61
|
+
type MiniMothConfig = {
|
|
62
|
+
apiKey: string;
|
|
63
|
+
logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
64
|
+
logger?: MiniMothLogger;
|
|
65
|
+
session?: {
|
|
66
|
+
store?: MiniMothSessionStore;
|
|
67
|
+
validateMode?: ValidateMode;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
type Session = {
|
|
71
|
+
phone: string;
|
|
72
|
+
projectId: string;
|
|
73
|
+
sessionId: string;
|
|
74
|
+
expiresAt: Date;
|
|
75
|
+
newTokens?: {
|
|
76
|
+
accessToken: string;
|
|
77
|
+
refreshToken: string;
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
type AccessTokenPayload = {
|
|
81
|
+
sub: string;
|
|
82
|
+
project_id: string;
|
|
83
|
+
session_id: string;
|
|
84
|
+
jti: string;
|
|
85
|
+
iat: number;
|
|
86
|
+
exp: number;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
declare class RevocationCache {
|
|
90
|
+
private readonly mode;
|
|
91
|
+
private readonly apiKey;
|
|
92
|
+
private readonly logger;
|
|
93
|
+
private readonly cache;
|
|
94
|
+
private lastCleanedAt;
|
|
95
|
+
constructor(mode: ValidateMode, apiKey: string, logger: MiniMothLogger);
|
|
96
|
+
check(jti: string, accessToken: string): Promise<boolean>;
|
|
97
|
+
private maybeCleanup;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
declare class RefreshDeduplicator {
|
|
101
|
+
private readonly logger;
|
|
102
|
+
private readonly inflight;
|
|
103
|
+
constructor(logger: MiniMothLogger);
|
|
104
|
+
getOrRun<T>(sessionId: string, fn: () => Promise<T>): Promise<T>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
type Tokens = {
|
|
108
|
+
accessToken: string;
|
|
109
|
+
refreshToken: string;
|
|
110
|
+
};
|
|
111
|
+
type SafeValidateResult = {
|
|
112
|
+
valid: true;
|
|
113
|
+
session: Session;
|
|
114
|
+
} | {
|
|
115
|
+
valid: false;
|
|
116
|
+
code: MiniMothErrorCode;
|
|
117
|
+
};
|
|
118
|
+
declare class SessionClient {
|
|
119
|
+
private readonly apiKey;
|
|
120
|
+
private readonly jwksCache;
|
|
121
|
+
private readonly revocationCache;
|
|
122
|
+
private readonly deduplicator;
|
|
123
|
+
private readonly store;
|
|
124
|
+
private readonly logger;
|
|
125
|
+
constructor(apiKey: string, jwksCache: JwksCache, revocationCache: RevocationCache, deduplicator: RefreshDeduplicator, store: MiniMothSessionStore, logger: MiniMothLogger);
|
|
126
|
+
validate(accessToken: string): Promise<Session>;
|
|
127
|
+
safeValidate(accessToken: string): Promise<SafeValidateResult>;
|
|
128
|
+
refresh(refreshToken: string): Promise<Tokens>;
|
|
129
|
+
logout(params: {
|
|
130
|
+
accessToken: string;
|
|
131
|
+
refreshToken: string;
|
|
132
|
+
}): Promise<void>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
136
|
+
declare class MiniMoth {
|
|
137
|
+
readonly otp: OtpClient;
|
|
138
|
+
readonly session: SessionClient;
|
|
139
|
+
private readonly consoleLogger;
|
|
140
|
+
constructor(config: MiniMothConfig);
|
|
141
|
+
setLogLevel(level: LogLevel): void;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { type AccessTokenPayload, MiniMoth, type MiniMothConfig, MiniMothError, type MiniMothErrorCode, type MiniMothLogger, type MiniMothSessionStore, type Session, type ValidateMode };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
|
|
3
|
+
type MiniMothErrorCode = 'TOKEN_EXPIRED' | 'TOKEN_REVOKED' | 'INVALID_TOKEN' | 'INVALID_OTP' | 'OTP_EXPIRED' | 'RATE_LIMITED' | 'INSUFFICIENT_BALANCE' | 'NETWORK_ERROR' | 'INVALID_PHONE' | 'UNKNOWN_ERROR';
|
|
4
|
+
declare class MiniMothError extends Error {
|
|
5
|
+
readonly code: MiniMothErrorCode;
|
|
6
|
+
readonly statusCode: number;
|
|
7
|
+
constructor(code: MiniMothErrorCode, message: string, statusCode: number, cause?: unknown);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface MiniMothSessionStore {
|
|
11
|
+
getRefreshToken(sessionId: string): Promise<string | null>;
|
|
12
|
+
setTokens(sessionId: string, tokens: {
|
|
13
|
+
accessToken: string;
|
|
14
|
+
refreshToken: string;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
deleteSession(sessionId: string): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface MiniMothLogger {
|
|
20
|
+
debug(msg: string, meta?: Record<string, unknown>): void;
|
|
21
|
+
info(msg: string, meta?: Record<string, unknown>): void;
|
|
22
|
+
warn(msg: string, meta?: Record<string, unknown>): void;
|
|
23
|
+
error(msg: string, meta?: Record<string, unknown>): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type VerifyResult = {
|
|
27
|
+
valid: true;
|
|
28
|
+
accessToken: string;
|
|
29
|
+
refreshToken: string;
|
|
30
|
+
sessionId: string;
|
|
31
|
+
} | {
|
|
32
|
+
valid: false;
|
|
33
|
+
code: MiniMothErrorCode;
|
|
34
|
+
};
|
|
35
|
+
declare class OtpClient {
|
|
36
|
+
private readonly apiKey;
|
|
37
|
+
private readonly store;
|
|
38
|
+
private readonly logger;
|
|
39
|
+
constructor(apiKey: string, store: MiniMothSessionStore, logger: MiniMothLogger);
|
|
40
|
+
send(params: {
|
|
41
|
+
phone: string;
|
|
42
|
+
}): Promise<void>;
|
|
43
|
+
verify(params: {
|
|
44
|
+
phone: string;
|
|
45
|
+
otp: string;
|
|
46
|
+
}): Promise<VerifyResult>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare class JwksCache {
|
|
50
|
+
private readonly logger;
|
|
51
|
+
private keyMap;
|
|
52
|
+
private fetchedAt;
|
|
53
|
+
private fetching;
|
|
54
|
+
constructor(logger: MiniMothLogger);
|
|
55
|
+
getKey(kid: string): Promise<crypto.KeyObject>;
|
|
56
|
+
private fetch;
|
|
57
|
+
private doFetch;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type ValidateMode = 'instant' | 'recheck_1m' | 'recheck_3m' | 'strict';
|
|
61
|
+
type MiniMothConfig = {
|
|
62
|
+
apiKey: string;
|
|
63
|
+
logLevel?: 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
64
|
+
logger?: MiniMothLogger;
|
|
65
|
+
session?: {
|
|
66
|
+
store?: MiniMothSessionStore;
|
|
67
|
+
validateMode?: ValidateMode;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
type Session = {
|
|
71
|
+
phone: string;
|
|
72
|
+
projectId: string;
|
|
73
|
+
sessionId: string;
|
|
74
|
+
expiresAt: Date;
|
|
75
|
+
newTokens?: {
|
|
76
|
+
accessToken: string;
|
|
77
|
+
refreshToken: string;
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
type AccessTokenPayload = {
|
|
81
|
+
sub: string;
|
|
82
|
+
project_id: string;
|
|
83
|
+
session_id: string;
|
|
84
|
+
jti: string;
|
|
85
|
+
iat: number;
|
|
86
|
+
exp: number;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
declare class RevocationCache {
|
|
90
|
+
private readonly mode;
|
|
91
|
+
private readonly apiKey;
|
|
92
|
+
private readonly logger;
|
|
93
|
+
private readonly cache;
|
|
94
|
+
private lastCleanedAt;
|
|
95
|
+
constructor(mode: ValidateMode, apiKey: string, logger: MiniMothLogger);
|
|
96
|
+
check(jti: string, accessToken: string): Promise<boolean>;
|
|
97
|
+
private maybeCleanup;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
declare class RefreshDeduplicator {
|
|
101
|
+
private readonly logger;
|
|
102
|
+
private readonly inflight;
|
|
103
|
+
constructor(logger: MiniMothLogger);
|
|
104
|
+
getOrRun<T>(sessionId: string, fn: () => Promise<T>): Promise<T>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
type Tokens = {
|
|
108
|
+
accessToken: string;
|
|
109
|
+
refreshToken: string;
|
|
110
|
+
};
|
|
111
|
+
type SafeValidateResult = {
|
|
112
|
+
valid: true;
|
|
113
|
+
session: Session;
|
|
114
|
+
} | {
|
|
115
|
+
valid: false;
|
|
116
|
+
code: MiniMothErrorCode;
|
|
117
|
+
};
|
|
118
|
+
declare class SessionClient {
|
|
119
|
+
private readonly apiKey;
|
|
120
|
+
private readonly jwksCache;
|
|
121
|
+
private readonly revocationCache;
|
|
122
|
+
private readonly deduplicator;
|
|
123
|
+
private readonly store;
|
|
124
|
+
private readonly logger;
|
|
125
|
+
constructor(apiKey: string, jwksCache: JwksCache, revocationCache: RevocationCache, deduplicator: RefreshDeduplicator, store: MiniMothSessionStore, logger: MiniMothLogger);
|
|
126
|
+
validate(accessToken: string): Promise<Session>;
|
|
127
|
+
safeValidate(accessToken: string): Promise<SafeValidateResult>;
|
|
128
|
+
refresh(refreshToken: string): Promise<Tokens>;
|
|
129
|
+
logout(params: {
|
|
130
|
+
accessToken: string;
|
|
131
|
+
refreshToken: string;
|
|
132
|
+
}): Promise<void>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent';
|
|
136
|
+
declare class MiniMoth {
|
|
137
|
+
readonly otp: OtpClient;
|
|
138
|
+
readonly session: SessionClient;
|
|
139
|
+
private readonly consoleLogger;
|
|
140
|
+
constructor(config: MiniMothConfig);
|
|
141
|
+
setLogLevel(level: LogLevel): void;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { type AccessTokenPayload, MiniMoth, type MiniMothConfig, MiniMothError, type MiniMothErrorCode, type MiniMothLogger, type MiniMothSessionStore, type Session, type ValidateMode };
|