forked from MindBlockLabs/mindBlock_app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.service.ts
More file actions
182 lines (151 loc) · 5.3 KB
/
Copy pathauth.service.ts
File metadata and controls
182 lines (151 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { BadRequestException, Injectable } from '@nestjs/common';
import { LoginDto } from '../dtos/login.dto';
import { SignInProvider } from './sign-in.provider';
import { ApiBody, ApiOperation } from '@nestjs/swagger';
import { RefreshTokenDto } from '../dtos/refreshTokenDto';
import { RefreshTokensProvider } from './refreshTokensProvider';
import { StellarWalletLoginDto } from '../dtos/walletLogin.dto';
import { StellarWalletLoginProvider } from './wallet-login.provider';
import { NonceResponseDto } from '../dtos/nonceResponse.dto';
import { ForgotPasswordProvider } from './forgot-password.provider';
import { ResetPasswordProvider } from './reset-password.provider';
import { ForgotPasswordDto } from '../dtos/forgot-password.dto';
import { ResetPasswordDto } from '../dtos/reset-password.dto';
// interface OAuthUser {
// email: string;
// username: string;
// picture: string;
// accessToken: string;
// }
@Injectable()
export class AuthService {
private nonces = new Map<
string,
{ walletAddress: string; expiresAt: number; used: boolean }
>();
constructor(
/**
* inject signInProvider
*/
private readonly signInProvider: SignInProvider,
/**
* inject stellarWalletLoginProvider
*/
private readonly stellarWalletLoginProvider: StellarWalletLoginProvider,
/**
* Injecting RefreshTokensProvider for token management
*/
private readonly refreshTokensProvider: RefreshTokensProvider,
/**
* inject forgotPasswordProvider
*/
private readonly forgotPasswordProvider: ForgotPasswordProvider,
/**
* inject resetPasswordProvider
*/
private readonly resetPasswordProvider: ResetPasswordProvider,
) {}
public async SignIn(signInDto: LoginDto) {
return await this.signInProvider.SignIn(signInDto);
}
public async StellarWalletLogin(dto: StellarWalletLoginDto) {
return await this.stellarWalletLoginProvider.StellarWalletLogin(dto);
}
// Generate nonce for wallet authentication
public generateNonce(walletAddress: string): NonceResponseDto {
// Validate wallet address format
if (!walletAddress || !this.isValidStellarAddress(walletAddress)) {
throw new BadRequestException('Invalid Stellar wallet address');
}
// Generate secure nonce
const nonce = this.createSecureNonce(walletAddress);
const expiresAt = Date.now() + 5 * 60 * 1000; // 5 minutes from now
// Store nonce
this.nonces.set(nonce, {
walletAddress,
expiresAt,
used: false,
});
// Clean up expired nonces periodically
this.cleanupExpiredNonces();
return { nonce, expiresAt };
}
// Check nonce status (useful for debugging)
public checkNonceStatus(nonce: string) {
const nonceData = this.nonces.get(nonce);
if (!nonceData) {
return { valid: false, reason: 'Nonce not found' };
}
if (nonceData.used) {
return { valid: false, reason: 'Nonce already used' };
}
if (Date.now() > nonceData.expiresAt) {
return { valid: false, reason: 'Nonce expired' };
}
return {
valid: true,
walletAddress: nonceData.walletAddress,
expiresAt: nonceData.expiresAt,
};
}
// Verify and mark nonce as used (called by StellarWalletLoginProvider)
public verifyAndUseNonce(nonce: string, walletAddress: string): void {
const nonceData = this.nonces.get(nonce);
if (!nonceData) {
throw new BadRequestException('Invalid nonce');
}
if (nonceData.used) {
throw new BadRequestException('Nonce already used');
}
if (Date.now() > nonceData.expiresAt) {
throw new BadRequestException('Nonce expired');
}
if (nonceData.walletAddress !== walletAddress) {
throw new BadRequestException('Nonce wallet address mismatch');
}
// Mark as used
nonceData.used = true;
this.nonces.set(nonce, nonceData);
}
private createSecureNonce(walletAddress: string): string {
const timestamp = Date.now();
const random = Math.random().toString(36).substring(2, 15);
const addressSuffix = walletAddress.slice(-6);
return `stellar_nonce_${timestamp}_${random}_${addressSuffix}`;
}
private isValidStellarAddress(address: string): boolean {
// Stellar address validation - starts with G for public key or M for muxed account
// and is 56 characters long (base32 encoded)
return /^[GM][A-Z2-7]{55}$/.test(address);
}
private cleanupExpiredNonces(): void {
const now = Date.now();
for (const [nonce, data] of this.nonces.entries()) {
if (now > data.expiresAt) {
this.nonces.delete(nonce);
}
}
}
/**
* Handles refreshing of access tokens
* @param refreshTokenDto - DTO containing the refresh token
* @returns A new access token if the refresh token is valid
*/
@ApiOperation({ summary: 'Refresh Access Token' })
@ApiBody({ type: RefreshTokenDto })
public refreshToken(refreshTokenDto: RefreshTokenDto) {
return this.refreshTokensProvider.refreshTokens(refreshTokenDto);
}
public async forgotPassword(forgotPasswordDto: ForgotPasswordDto) {
return await this.forgotPasswordProvider.forgotPassword(forgotPasswordDto);
}
public async resetPassword(
token: string,
resetPasswordDto: ResetPasswordDto,
) {
return await this.resetPasswordProvider.resetPassword(
token,
resetPasswordDto,
);
}
}