@dax-crafta/auth 1.2.9 → 1.3.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dax-crafta/auth",
3
- "version": "1.2.9",
3
+ "version": "1.3.1",
4
4
  "description": "Authentication plugin for crafta framework",
5
5
  "main": "src/index.js",
6
6
  "private": false,
@@ -0,0 +1,55 @@
1
+ const AuditLog = require('../models/audit-log');
2
+ const winston = require('winston');
3
+
4
+ class AuditService {
5
+ constructor() {
6
+ this.logger = winston.createLogger({
7
+ level: 'info',
8
+ format: winston.format.json(),
9
+ transports: [
10
+ new winston.transports.File({ filename: 'audit.log' })
11
+ ]
12
+ });
13
+ }
14
+
15
+ async logActivity(data) {
16
+ const log = new AuditLog({
17
+ userId: data.userId,
18
+ action: data.action,
19
+ ipAddress: data.ipAddress,
20
+ userAgent: data.userAgent,
21
+ details: data.details,
22
+ status: data.status
23
+ });
24
+
25
+ await log.save();
26
+ this.logger.info('Activity logged', { logId: log._id, ...data });
27
+ return log;
28
+ }
29
+
30
+ async getUserActivity(userId, filters = {}) {
31
+ const query = { userId, ...filters };
32
+ return AuditLog.find(query).sort({ timestamp: -1 });
33
+ }
34
+
35
+ async getActivityByDateRange(startDate, endDate) {
36
+ return AuditLog.find({
37
+ timestamp: {
38
+ $gte: startDate,
39
+ $lte: endDate
40
+ }
41
+ }).sort({ timestamp: -1 });
42
+ }
43
+
44
+ async getFailedLoginAttempts(userId, timeWindow) {
45
+ const since = new Date(Date.now() - timeWindow);
46
+ return AuditLog.countDocuments({
47
+ userId,
48
+ action: 'login',
49
+ status: 'failure',
50
+ timestamp: { $gte: since }
51
+ });
52
+ }
53
+ }
54
+
55
+ module.exports = AuditService;
@@ -0,0 +1,60 @@
1
+ const zxcvbn = require('zxcvbn');
2
+
3
+ class PasswordPolicy {
4
+ constructor(config = {}) {
5
+ this.config = {
6
+ minLength: config.minLength || 8,
7
+ requireUppercase: config.requireUppercase !== false,
8
+ requireLowercase: config.requireLowercase !== false,
9
+ requireNumbers: config.requireNumbers !== false,
10
+ requireSpecialChars: config.requireSpecialChars !== false,
11
+ passwordHistory: config.passwordHistory || 3,
12
+ expiryDays: config.expiryDays || 90,
13
+ minStrength: config.minStrength || 3
14
+ };
15
+ }
16
+
17
+ validate(password, userInfo = {}) {
18
+ const errors = [];
19
+
20
+ if (password.length < this.config.minLength) {
21
+ errors.push(`Password must be at least ${this.config.minLength} characters long`);
22
+ }
23
+
24
+ if (this.config.requireUppercase && !/[A-Z]/.test(password)) {
25
+ errors.push('Password must contain at least one uppercase letter');
26
+ }
27
+
28
+ if (this.config.requireLowercase && !/[a-z]/.test(password)) {
29
+ errors.push('Password must contain at least one lowercase letter');
30
+ }
31
+
32
+ if (this.config.requireNumbers && !/\d/.test(password)) {
33
+ errors.push('Password must contain at least one number');
34
+ }
35
+
36
+ if (this.config.requireSpecialChars && !/[!@#$%^&*(),.?":{}|<>]/.test(password)) {
37
+ errors.push('Password must contain at least one special character');
38
+ }
39
+
40
+ const strength = zxcvbn(password, [userInfo.email, userInfo.name]);
41
+ if (strength.score < this.config.minStrength) {
42
+ errors.push('Password is too weak. Please choose a stronger password');
43
+ }
44
+
45
+ return {
46
+ isValid: errors.length === 0,
47
+ errors,
48
+ strength: strength.score
49
+ };
50
+ }
51
+
52
+ isExpired(lastPasswordChange) {
53
+ if (!this.config.expiryDays) return false;
54
+
55
+ const expiryDate = new Date(lastPasswordChange.getTime() + this.config.expiryDays * 24 * 60 * 60 * 1000);
56
+ return expiryDate < new Date();
57
+ }
58
+ }
59
+
60
+ module.exports = PasswordPolicy;