Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/api-keys/api-key.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// API Key type definitions

export interface ApiKey {
id: string;
name: string;
key: string;
keyPrefix: string;
scopes: string[];
requestCount: bigint;
lastUsedAt?: Date;
isActive: boolean;
rateLimit?: number;
createdAt: Date;
updatedAt: Date;
}

export interface CreateApiKeyDto {
name: string;
scopes: string[];
rateLimit?: number;
}

export interface UpdateApiKeyDto {
name?: string;
scopes?: string[];
rateLimit?: number;
isActive?: boolean;
}

export interface ApiKeyQueryDto {
page?: number;
limit?: number;
isActive?: boolean;
search?: string;
}

export interface ApiKeyResponseDto {
id: string;
name: string;
keyPrefix: string;
scopes: string[];
requestCount: string;
lastUsedAt?: Date;
isActive: boolean;
rateLimit?: number;
createdAt: Date;
updatedAt: Date;
}

export interface ApiKeyValidationResult {
isValid: boolean;
apiKey?: ApiKey;
error?: string;
remainingRequests?: number;
resetTime?: number;
}

export interface ApiKeyRateLimitInfo {
limit: number;
remaining: number;
resetTime: number;
window: number;
}

export interface ApiKeyUsageStats {
totalRequests: number;
requestsToday: number;
requestsThisMonth: number;
averageDailyRequests: number;
peakHour: number;
lastUsedAt?: Date;
}

export interface ApiKeyScope {
resource: string;
action: string;
description: string;
}

export interface ApiKeyWithUsage extends ApiKey {
usageStats: ApiKeyUsageStats;
rateLimitInfo: ApiKeyRateLimitInfo;
}

export interface ApiKeyRequestContext {
apiKey?: ApiKey;
ipAddress: string;
userAgent: string;
timestamp: Date;
endpoint: string;
method: string;
}
13 changes: 9 additions & 4 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import * as bcrypt from 'bcrypt';
import { RedisService } from '../common/services/redis.service';
import { v4 as uuidv4 } from 'uuid';
import { StructuredLoggerService } from '../common/logging/logger.service';
import { AuthUser, JwtPayload, AuthTokens } from './auth.types';
import { PrismaUser } from '../types/prisma.types';
import { isObject, isString } from '../types/guards';

@Injectable()
export class AuthService {
Expand All @@ -28,8 +31,9 @@ export class AuthService {
return {
message: 'User registered successfully. Please check your email for verification.',
};
} catch (error) {
this.logger.error('User registration failed', error.stack, {
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
this.logger.error('User registration failed', errorMessage, {
email: createUserDto.email,
});
throw error;
Expand Down Expand Up @@ -81,8 +85,9 @@ export class AuthService {

this.logger.logAuth('User login successful', { userId: user.id });
return this.generateTokens(user);
} catch (error) {
this.logger.error('User login failed', error.stack, {
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
this.logger.error('User login failed', errorMessage, {
email: credentials.email,
});
throw error;
Expand Down
115 changes: 115 additions & 0 deletions src/auth/auth.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Authentication type definitions

export interface AuthUser {
id: string;
email: string;
walletAddress?: string;
password?: string;
firstName?: string;
lastName?: string;
isVerified: boolean;
role: string;
createdAt: Date;
updatedAt: Date;
}

export interface JwtPayload {
sub: string;
email: string;
jti?: string;
iat?: number;
exp?: number;
}

export interface AuthTokens {
access_token: string;
refresh_token: string;
user: {
id: string;
email: string;
walletAddress?: string;
isVerified: boolean;
};
}

export interface LoginRequest {
email: string;
password: string;
}

export interface Web3LoginRequest {
walletAddress: string;
signature: string;
}

export interface RefreshTokenRequest {
refresh_token: string;
}

export interface RegisterRequest {
email: string;
password: string;
firstName?: string;
lastName?: string;
walletAddress?: string;
}

export interface PasswordResetRequest {
email: string;
}

export interface PasswordResetConfirmRequest {
token: string;
newPassword: string;
}

export interface MfaSetupRequest {
method: 'totp' | 'sms' | 'email';
phoneNumber?: string;
email?: string;
}

export interface MfaVerifyRequest {
method: string;
code: string;
}

export interface SessionInfo {
userId: string;
jti: string;
createdAt: string;
userAgent: string;
ip: string;
lastActivity?: string;
}

export interface LoginAttempt {
email: string;
ip: string;
timestamp: Date;
success: boolean;
userAgent?: string;
}

export interface AccountLockInfo {
email: string;
ip: string;
lockoutUntil: Date;
failedAttempts: number;
lastAttempt: Date;
}

export interface TokenBlacklistEntry {
jti: string;
userId: string;
blacklistedAt: Date;
reason?: string;
}

export interface AuthRequestContext {
user?: AuthUser;
session?: SessionInfo;
ip: string;
userAgent: string;
timestamp: Date;
}
Loading