irismail 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/README.md +158 -0
- package/dist/chunk-FTNSOYOW.mjs +223 -0
- package/dist/chunk-QZ7TP4HQ.mjs +7 -0
- package/dist/chunk-XGASTZZ6.mjs +180 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +446 -0
- package/dist/index.mjs +31 -0
- package/dist/react/index.d.mts +60 -0
- package/dist/react/index.d.ts +60 -0
- package/dist/react/index.js +261 -0
- package/dist/react/index.mjs +15 -0
- package/dist/server/index.d.mts +110 -0
- package/dist/server/index.d.ts +110 -0
- package/dist/server/index.js +218 -0
- package/dist/server/index.mjs +15 -0
- package/package.json +60 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
31
|
+
|
|
32
|
+
// src/server/index.ts
|
|
33
|
+
var server_exports = {};
|
|
34
|
+
__export(server_exports, {
|
|
35
|
+
EmailService: () => EmailService,
|
|
36
|
+
IrisMailService: () => IrisMailService,
|
|
37
|
+
OTPService: () => OTPService,
|
|
38
|
+
decrypt: () => decrypt,
|
|
39
|
+
encrypt: () => encrypt
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(server_exports);
|
|
42
|
+
|
|
43
|
+
// src/server/email.ts
|
|
44
|
+
var import_nodemailer = __toESM(require("nodemailer"));
|
|
45
|
+
var EmailService = class {
|
|
46
|
+
constructor(config) {
|
|
47
|
+
__publicField(this, "transporter");
|
|
48
|
+
__publicField(this, "config");
|
|
49
|
+
this.config = config;
|
|
50
|
+
this.transporter = import_nodemailer.default.createTransport(config.transport);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Verify SMTP connection configuration
|
|
54
|
+
*/
|
|
55
|
+
async verifyConnection() {
|
|
56
|
+
try {
|
|
57
|
+
await this.transporter.verify();
|
|
58
|
+
return true;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error("SMTP connection verification failed:", error);
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Send an email
|
|
66
|
+
* @param options - Email options (to, subject, html, text, from)
|
|
67
|
+
* @returns - Result of the send operation
|
|
68
|
+
*/
|
|
69
|
+
async sendEmail(options) {
|
|
70
|
+
const from = options.from || this.config.defaults?.from;
|
|
71
|
+
if (!from) {
|
|
72
|
+
throw new Error("From address is required either in options or config defaults");
|
|
73
|
+
}
|
|
74
|
+
const mailOptions = {
|
|
75
|
+
from: {
|
|
76
|
+
name: from.name,
|
|
77
|
+
address: from.address
|
|
78
|
+
},
|
|
79
|
+
to: options.to,
|
|
80
|
+
subject: options.subject,
|
|
81
|
+
html: options.html,
|
|
82
|
+
text: options.text || options.html.replace(/<[^>]*>/g, "")
|
|
83
|
+
// Simple HTML to text fallback
|
|
84
|
+
};
|
|
85
|
+
try {
|
|
86
|
+
const info = await this.transporter.sendMail(mailOptions);
|
|
87
|
+
return {
|
|
88
|
+
success: true,
|
|
89
|
+
messageId: info.messageId
|
|
90
|
+
};
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.error("Error sending email:", error);
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/server/crypto.ts
|
|
99
|
+
var import_crypto = __toESM(require("crypto"));
|
|
100
|
+
var ALGORITHM = "aes-256-cbc";
|
|
101
|
+
function encrypt(text, secretKey) {
|
|
102
|
+
try {
|
|
103
|
+
const key = Buffer.from(secretKey.padEnd(32).slice(0, 32));
|
|
104
|
+
const iv = import_crypto.default.randomBytes(16);
|
|
105
|
+
const cipher = import_crypto.default.createCipheriv(ALGORITHM, key, iv);
|
|
106
|
+
let encrypted = cipher.update(text, "utf8", "base64");
|
|
107
|
+
encrypted += cipher.final("base64");
|
|
108
|
+
return iv.toString("hex") + ":" + encrypted;
|
|
109
|
+
} catch (error) {
|
|
110
|
+
console.error("Encryption error:", error);
|
|
111
|
+
throw new Error("Failed to encrypt data");
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function decrypt(encryptedText, secretKey) {
|
|
115
|
+
try {
|
|
116
|
+
const key = Buffer.from(secretKey.padEnd(32).slice(0, 32));
|
|
117
|
+
const parts = encryptedText.split(":");
|
|
118
|
+
if (parts.length !== 2) {
|
|
119
|
+
throw new Error("Invalid encrypted text format");
|
|
120
|
+
}
|
|
121
|
+
const iv = Buffer.from(parts[0], "hex");
|
|
122
|
+
const encrypted = parts[1];
|
|
123
|
+
const decipher = import_crypto.default.createDecipheriv(ALGORITHM, key, iv);
|
|
124
|
+
let decrypted = decipher.update(encrypted, "base64", "utf8");
|
|
125
|
+
decrypted += decipher.final("utf8");
|
|
126
|
+
return decrypted;
|
|
127
|
+
} catch (error) {
|
|
128
|
+
console.error("Decryption error:", error);
|
|
129
|
+
throw new Error("Failed to decrypt data");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/server/otp.ts
|
|
134
|
+
var OTPService = class {
|
|
135
|
+
constructor(secretKey) {
|
|
136
|
+
__publicField(this, "secretKey");
|
|
137
|
+
if (!secretKey) {
|
|
138
|
+
throw new Error("OTPService: secretKey is required");
|
|
139
|
+
}
|
|
140
|
+
this.secretKey = secretKey;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Generate a numeric OTP of specified length
|
|
144
|
+
* @param length - Length of the OTP (default: 6)
|
|
145
|
+
* @returns - The generated OTP string
|
|
146
|
+
*/
|
|
147
|
+
generateOTP(length = 6) {
|
|
148
|
+
const min = Math.pow(10, length - 1);
|
|
149
|
+
const max = Math.pow(10, length) - 1;
|
|
150
|
+
return Math.floor(min + Math.random() * (max - min + 1)).toString();
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Encrypt OTP data for secure storage/transmission
|
|
154
|
+
* @param otp - The OTP string
|
|
155
|
+
* @param timestamp - The timestamp when OTP was generated (default: now)
|
|
156
|
+
* @returns - Encrypted OTP data string
|
|
157
|
+
*/
|
|
158
|
+
encryptOTP(otp, timestamp = Date.now()) {
|
|
159
|
+
const data = JSON.stringify({ otp, timestamp });
|
|
160
|
+
return encrypt(data, this.secretKey);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Decrypt OTP data
|
|
164
|
+
* @param encryptedData - The encrypted OTP string
|
|
165
|
+
* @returns - Decrypted OTP data object or null if failed
|
|
166
|
+
*/
|
|
167
|
+
decryptOTP(encryptedData) {
|
|
168
|
+
try {
|
|
169
|
+
const decryptedData = decrypt(encryptedData, this.secretKey);
|
|
170
|
+
return JSON.parse(decryptedData);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
console.error("Error decrypting OTP data:", error);
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Verify an OTP against encrypted data
|
|
178
|
+
* @param inputOtp - The OTP provided by the user
|
|
179
|
+
* @param encryptedData - The encrypted OTP data
|
|
180
|
+
* @param expiryMinutes - Expiry time in minutes (default: 5)
|
|
181
|
+
* @returns - Object containing valid status and message
|
|
182
|
+
*/
|
|
183
|
+
verifyOTP(inputOtp, encryptedData, expiryMinutes = 5) {
|
|
184
|
+
const data = this.decryptOTP(encryptedData);
|
|
185
|
+
if (!data) {
|
|
186
|
+
return { valid: false, message: "Invalid OTP data" };
|
|
187
|
+
}
|
|
188
|
+
const { otp, timestamp } = data;
|
|
189
|
+
const currentTime = Date.now();
|
|
190
|
+
const otpAge = currentTime - timestamp;
|
|
191
|
+
const expiryTime = expiryMinutes * 60 * 1e3;
|
|
192
|
+
if (otpAge > expiryTime) {
|
|
193
|
+
return { valid: false, message: "OTP has expired" };
|
|
194
|
+
}
|
|
195
|
+
if (otp !== inputOtp) {
|
|
196
|
+
return { valid: false, message: "Incorrect OTP" };
|
|
197
|
+
}
|
|
198
|
+
return { valid: true, message: "OTP verified successfully" };
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
// src/server/index.ts
|
|
203
|
+
var IrisMailService = class {
|
|
204
|
+
constructor(config) {
|
|
205
|
+
__publicField(this, "email");
|
|
206
|
+
__publicField(this, "otp");
|
|
207
|
+
this.email = new EmailService(config.email);
|
|
208
|
+
this.otp = new OTPService(config.secretKey);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
212
|
+
0 && (module.exports = {
|
|
213
|
+
EmailService,
|
|
214
|
+
IrisMailService,
|
|
215
|
+
OTPService,
|
|
216
|
+
decrypt,
|
|
217
|
+
encrypt
|
|
218
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "irismail",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A modular email and OTP service with Shadcn UI support",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./react": {
|
|
14
|
+
"types": "./dist/react/index.d.ts",
|
|
15
|
+
"import": "./dist/react/index.mjs",
|
|
16
|
+
"require": "./dist/react/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./server": {
|
|
19
|
+
"types": "./dist/server/index.d.ts",
|
|
20
|
+
"import": "./dist/server/index.mjs",
|
|
21
|
+
"require": "./dist/server/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup src/index.ts src/server/index.ts src/react/index.ts --format cjs,esm --dts --clean",
|
|
29
|
+
"dev": "tsup src/index.ts src/server/index.ts src/react/index.ts --format cjs,esm --dts --watch",
|
|
30
|
+
"lint": "eslint src/**",
|
|
31
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"email",
|
|
35
|
+
"otp",
|
|
36
|
+
"shadcn",
|
|
37
|
+
"react",
|
|
38
|
+
"authentication"
|
|
39
|
+
],
|
|
40
|
+
"author": "",
|
|
41
|
+
"license": "ISC",
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"clsx": "^2.1.1",
|
|
44
|
+
"input-otp": "^1.4.2",
|
|
45
|
+
"nodemailer": "^7.0.10",
|
|
46
|
+
"tailwind-merge": "^2.3.0",
|
|
47
|
+
"zod": "^3.23.8"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "^20.12.12",
|
|
51
|
+
"@types/nodemailer": "^6.4.15",
|
|
52
|
+
"@types/react": "^18.3.3",
|
|
53
|
+
"react": "^18.3.1",
|
|
54
|
+
"tsup": "^8.0.2",
|
|
55
|
+
"typescript": "^5.4.5"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"react": ">=18"
|
|
59
|
+
}
|
|
60
|
+
}
|