irismail 0.1.0 → 0.5.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.
@@ -1,223 +0,0 @@
1
- // src/react/components/input-otp.tsx
2
- import * as React from "react";
3
- import { OTPInput, OTPInputContext } from "input-otp";
4
-
5
- // src/utils/constants.ts
6
- import { clsx } from "clsx";
7
- import { twMerge } from "tailwind-merge";
8
- function cn(...inputs) {
9
- return twMerge(clsx(inputs));
10
- }
11
- var OTP_DEFAULTS = {
12
- LENGTH: 6,
13
- EXPIRY_MINUTES: 5,
14
- RESEND_COOLDOWN_SECONDS: 120,
15
- // 2 minutes
16
- MAX_ATTEMPTS: 5,
17
- RATE_LIMIT_RESET_HOURS: 1
18
- };
19
-
20
- // src/react/components/input-otp.tsx
21
- import { jsx, jsxs } from "react/jsx-runtime";
22
- var Dot = () => /* @__PURE__ */ jsx(
23
- "svg",
24
- {
25
- width: "15",
26
- height: "15",
27
- viewBox: "0 0 15 15",
28
- fill: "none",
29
- xmlns: "http://www.w3.org/2000/svg",
30
- className: "h-4 w-4",
31
- children: /* @__PURE__ */ jsx(
32
- "path",
33
- {
34
- d: "M7.5 7.5C7.5 8.32843 6.82843 9 6 9C5.17157 9 4.5 8.32843 4.5 7.5C4.5 6.67157 5.17157 6 6 6C6.82843 6 7.5 6.67157 7.5 7.5Z",
35
- fill: "currentColor",
36
- fillRule: "evenodd",
37
- clipRule: "evenodd"
38
- }
39
- )
40
- }
41
- );
42
- var InputOTP = React.forwardRef(
43
- ({ className, containerClassName, ...props }, ref) => /* @__PURE__ */ jsx(
44
- OTPInput,
45
- {
46
- ref,
47
- containerClassName: cn("flex items-center gap-2 has-[:disabled]:opacity-50", containerClassName),
48
- className: cn("disabled:cursor-not-allowed", className),
49
- ...props
50
- }
51
- )
52
- );
53
- InputOTP.displayName = "InputOTP";
54
- var InputOTPGroup = React.forwardRef(
55
- ({ className, ...props }, ref) => /* @__PURE__ */ jsx("div", { ref, className: cn("flex items-center", className), ...props })
56
- );
57
- InputOTPGroup.displayName = "InputOTPGroup";
58
- var InputOTPSlot = React.forwardRef(({ index, className, ...props }, ref) => {
59
- const inputOTPContext = React.useContext(OTPInputContext);
60
- const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index];
61
- return /* @__PURE__ */ jsxs(
62
- "div",
63
- {
64
- ref,
65
- className: cn(
66
- "relative flex h-10 w-10 items-center justify-center border-y border-r border-input text-sm transition-all first:rounded-l-md first:border-l last:rounded-r-md",
67
- isActive && "z-10 ring-2 ring-ring ring-offset-background",
68
- className
69
- ),
70
- ...props,
71
- children: [
72
- char,
73
- hasFakeCaret && /* @__PURE__ */ jsx("div", { className: "pointer-events-none absolute inset-0 flex items-center justify-center", children: /* @__PURE__ */ jsx("div", { className: "h-4 w-px animate-caret-blink bg-foreground duration-1000" }) })
74
- ]
75
- }
76
- );
77
- });
78
- InputOTPSlot.displayName = "InputOTPSlot";
79
- var InputOTPSeparator = React.forwardRef(
80
- ({ ...props }, ref) => /* @__PURE__ */ jsx("div", { ref, role: "separator", ...props, children: /* @__PURE__ */ jsx(Dot, {}) })
81
- );
82
- InputOTPSeparator.displayName = "InputOTPSeparator";
83
-
84
- // src/react/hooks/use-otp.ts
85
- import { useState, useEffect } from "react";
86
- function useOTP({ email, onRateLimit }) {
87
- const [otpTimeLeft, setOtpTimeLeft] = useState(OTP_DEFAULTS.EXPIRY_MINUTES * 60);
88
- const [resendTimeLeft, setResendTimeLeft] = useState(0);
89
- const [attemptsLeft, setAttemptsLeft] = useState(OTP_DEFAULTS.MAX_ATTEMPTS);
90
- const [isRateLimited, setIsRateLimited] = useState(false);
91
- const [rateLimitResetTime, setRateLimitResetTime] = useState(null);
92
- const getStorageKey = (prefix) => `${prefix}_${email}`;
93
- const getStoredData = (key) => {
94
- if (typeof window === "undefined") return null;
95
- const data = sessionStorage.getItem(key);
96
- return data ? JSON.parse(data) : null;
97
- };
98
- const setStoredData = (key, data) => {
99
- if (typeof window !== "undefined") {
100
- sessionStorage.setItem(key, JSON.stringify(data));
101
- }
102
- };
103
- const removeStoredData = (key) => {
104
- if (typeof window !== "undefined") {
105
- sessionStorage.removeItem(key);
106
- }
107
- };
108
- const checkRateLimit = () => {
109
- const key = getStorageKey("rate_limit");
110
- const data = getStoredData(key);
111
- if (!data) {
112
- setAttemptsLeft(OTP_DEFAULTS.MAX_ATTEMPTS);
113
- setIsRateLimited(false);
114
- return { limited: false, attemptsLeft: OTP_DEFAULTS.MAX_ATTEMPTS };
115
- }
116
- const currentTime = Date.now();
117
- const resetTime = OTP_DEFAULTS.RATE_LIMIT_RESET_HOURS * 60 * 60 * 1e3;
118
- if (currentTime - data.firstAttemptTime > resetTime) {
119
- removeStoredData(key);
120
- setAttemptsLeft(OTP_DEFAULTS.MAX_ATTEMPTS);
121
- setIsRateLimited(false);
122
- return { limited: false, attemptsLeft: OTP_DEFAULTS.MAX_ATTEMPTS };
123
- }
124
- const attempts = OTP_DEFAULTS.MAX_ATTEMPTS - data.attempts;
125
- const limited = data.attempts >= OTP_DEFAULTS.MAX_ATTEMPTS;
126
- const limitResetTime = data.firstAttemptTime + resetTime;
127
- setAttemptsLeft(Math.max(0, attempts));
128
- setIsRateLimited(limited);
129
- setRateLimitResetTime(limited ? limitResetTime : null);
130
- if (limited && onRateLimit) {
131
- onRateLimit(limitResetTime);
132
- }
133
- return { limited, attemptsLeft: Math.max(0, attempts), resetTime: limited ? limitResetTime : void 0 };
134
- };
135
- const updateRateLimit = () => {
136
- const key = getStorageKey("rate_limit");
137
- const data = getStoredData(key);
138
- const currentTime = Date.now();
139
- let newData;
140
- if (!data) {
141
- newData = {
142
- attempts: 1,
143
- firstAttemptTime: currentTime,
144
- lastAttemptTime: currentTime
145
- };
146
- } else {
147
- const resetTime = OTP_DEFAULTS.RATE_LIMIT_RESET_HOURS * 60 * 60 * 1e3;
148
- if (currentTime - data.firstAttemptTime > resetTime) {
149
- newData = {
150
- attempts: 1,
151
- firstAttemptTime: currentTime,
152
- lastAttemptTime: currentTime
153
- };
154
- } else {
155
- newData = {
156
- ...data,
157
- attempts: data.attempts + 1,
158
- lastAttemptTime: currentTime
159
- };
160
- }
161
- }
162
- setStoredData(key, newData);
163
- checkRateLimit();
164
- return newData;
165
- };
166
- const checkResendCooldown = () => {
167
- const key = getStorageKey("last_sent");
168
- const lastSentTime = getStoredData(key);
169
- if (!lastSentTime) {
170
- setResendTimeLeft(0);
171
- return { canResend: true, timeLeft: 0 };
172
- }
173
- const currentTime = Date.now();
174
- const timeSinceLastSent = currentTime - lastSentTime;
175
- const cooldown = OTP_DEFAULTS.RESEND_COOLDOWN_SECONDS * 1e3;
176
- const canResend = timeSinceLastSent >= cooldown;
177
- const timeLeft = canResend ? 0 : Math.ceil((cooldown - timeSinceLastSent) / 1e3);
178
- setResendTimeLeft(timeLeft);
179
- return { canResend, timeLeft };
180
- };
181
- const recordSentOTP = () => {
182
- const key = getStorageKey("last_sent");
183
- setStoredData(key, Date.now());
184
- updateRateLimit();
185
- setOtpTimeLeft(OTP_DEFAULTS.EXPIRY_MINUTES * 60);
186
- setResendTimeLeft(OTP_DEFAULTS.RESEND_COOLDOWN_SECONDS);
187
- };
188
- useEffect(() => {
189
- checkRateLimit();
190
- checkResendCooldown();
191
- const timer = setInterval(() => {
192
- setOtpTimeLeft((prev) => Math.max(0, prev - 1));
193
- const { timeLeft } = checkResendCooldown();
194
- setResendTimeLeft(timeLeft);
195
- }, 1e3);
196
- return () => clearInterval(timer);
197
- }, [email]);
198
- const formatTime = (seconds) => {
199
- const mins = Math.floor(seconds / 60);
200
- const secs = seconds % 60;
201
- return `${mins}:${secs.toString().padStart(2, "0")}`;
202
- };
203
- return {
204
- otpTimeLeft,
205
- resendTimeLeft,
206
- attemptsLeft,
207
- isRateLimited,
208
- rateLimitResetTime,
209
- formatTime,
210
- recordSentOTP,
211
- checkRateLimit
212
- };
213
- }
214
-
215
- export {
216
- cn,
217
- OTP_DEFAULTS,
218
- InputOTP,
219
- InputOTPGroup,
220
- InputOTPSlot,
221
- InputOTPSeparator,
222
- useOTP
223
- };
@@ -1,180 +0,0 @@
1
- import {
2
- __publicField
3
- } from "./chunk-QZ7TP4HQ.mjs";
4
-
5
- // src/server/email.ts
6
- import nodemailer from "nodemailer";
7
- var EmailService = class {
8
- constructor(config) {
9
- __publicField(this, "transporter");
10
- __publicField(this, "config");
11
- this.config = config;
12
- this.transporter = nodemailer.createTransport(config.transport);
13
- }
14
- /**
15
- * Verify SMTP connection configuration
16
- */
17
- async verifyConnection() {
18
- try {
19
- await this.transporter.verify();
20
- return true;
21
- } catch (error) {
22
- console.error("SMTP connection verification failed:", error);
23
- return false;
24
- }
25
- }
26
- /**
27
- * Send an email
28
- * @param options - Email options (to, subject, html, text, from)
29
- * @returns - Result of the send operation
30
- */
31
- async sendEmail(options) {
32
- const from = options.from || this.config.defaults?.from;
33
- if (!from) {
34
- throw new Error("From address is required either in options or config defaults");
35
- }
36
- const mailOptions = {
37
- from: {
38
- name: from.name,
39
- address: from.address
40
- },
41
- to: options.to,
42
- subject: options.subject,
43
- html: options.html,
44
- text: options.text || options.html.replace(/<[^>]*>/g, "")
45
- // Simple HTML to text fallback
46
- };
47
- try {
48
- const info = await this.transporter.sendMail(mailOptions);
49
- return {
50
- success: true,
51
- messageId: info.messageId
52
- };
53
- } catch (error) {
54
- console.error("Error sending email:", error);
55
- throw error;
56
- }
57
- }
58
- };
59
-
60
- // src/server/crypto.ts
61
- import crypto from "crypto";
62
- var ALGORITHM = "aes-256-cbc";
63
- function encrypt(text, secretKey) {
64
- try {
65
- const key = Buffer.from(secretKey.padEnd(32).slice(0, 32));
66
- const iv = crypto.randomBytes(16);
67
- const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
68
- let encrypted = cipher.update(text, "utf8", "base64");
69
- encrypted += cipher.final("base64");
70
- return iv.toString("hex") + ":" + encrypted;
71
- } catch (error) {
72
- console.error("Encryption error:", error);
73
- throw new Error("Failed to encrypt data");
74
- }
75
- }
76
- function decrypt(encryptedText, secretKey) {
77
- try {
78
- const key = Buffer.from(secretKey.padEnd(32).slice(0, 32));
79
- const parts = encryptedText.split(":");
80
- if (parts.length !== 2) {
81
- throw new Error("Invalid encrypted text format");
82
- }
83
- const iv = Buffer.from(parts[0], "hex");
84
- const encrypted = parts[1];
85
- const decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
86
- let decrypted = decipher.update(encrypted, "base64", "utf8");
87
- decrypted += decipher.final("utf8");
88
- return decrypted;
89
- } catch (error) {
90
- console.error("Decryption error:", error);
91
- throw new Error("Failed to decrypt data");
92
- }
93
- }
94
-
95
- // src/server/otp.ts
96
- var OTPService = class {
97
- constructor(secretKey) {
98
- __publicField(this, "secretKey");
99
- if (!secretKey) {
100
- throw new Error("OTPService: secretKey is required");
101
- }
102
- this.secretKey = secretKey;
103
- }
104
- /**
105
- * Generate a numeric OTP of specified length
106
- * @param length - Length of the OTP (default: 6)
107
- * @returns - The generated OTP string
108
- */
109
- generateOTP(length = 6) {
110
- const min = Math.pow(10, length - 1);
111
- const max = Math.pow(10, length) - 1;
112
- return Math.floor(min + Math.random() * (max - min + 1)).toString();
113
- }
114
- /**
115
- * Encrypt OTP data for secure storage/transmission
116
- * @param otp - The OTP string
117
- * @param timestamp - The timestamp when OTP was generated (default: now)
118
- * @returns - Encrypted OTP data string
119
- */
120
- encryptOTP(otp, timestamp = Date.now()) {
121
- const data = JSON.stringify({ otp, timestamp });
122
- return encrypt(data, this.secretKey);
123
- }
124
- /**
125
- * Decrypt OTP data
126
- * @param encryptedData - The encrypted OTP string
127
- * @returns - Decrypted OTP data object or null if failed
128
- */
129
- decryptOTP(encryptedData) {
130
- try {
131
- const decryptedData = decrypt(encryptedData, this.secretKey);
132
- return JSON.parse(decryptedData);
133
- } catch (error) {
134
- console.error("Error decrypting OTP data:", error);
135
- return null;
136
- }
137
- }
138
- /**
139
- * Verify an OTP against encrypted data
140
- * @param inputOtp - The OTP provided by the user
141
- * @param encryptedData - The encrypted OTP data
142
- * @param expiryMinutes - Expiry time in minutes (default: 5)
143
- * @returns - Object containing valid status and message
144
- */
145
- verifyOTP(inputOtp, encryptedData, expiryMinutes = 5) {
146
- const data = this.decryptOTP(encryptedData);
147
- if (!data) {
148
- return { valid: false, message: "Invalid OTP data" };
149
- }
150
- const { otp, timestamp } = data;
151
- const currentTime = Date.now();
152
- const otpAge = currentTime - timestamp;
153
- const expiryTime = expiryMinutes * 60 * 1e3;
154
- if (otpAge > expiryTime) {
155
- return { valid: false, message: "OTP has expired" };
156
- }
157
- if (otp !== inputOtp) {
158
- return { valid: false, message: "Incorrect OTP" };
159
- }
160
- return { valid: true, message: "OTP verified successfully" };
161
- }
162
- };
163
-
164
- // src/server/index.ts
165
- var IrisMailService = class {
166
- constructor(config) {
167
- __publicField(this, "email");
168
- __publicField(this, "otp");
169
- this.email = new EmailService(config.email);
170
- this.otp = new OTPService(config.secretKey);
171
- }
172
- };
173
-
174
- export {
175
- EmailService,
176
- encrypt,
177
- decrypt,
178
- OTPService,
179
- IrisMailService
180
- };