Skip to content
Open
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
26 changes: 26 additions & 0 deletions migration/1781004217553-AddWalletNameToIpLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @typedef {import('typeorm').MigrationInterface} MigrationInterface
* @typedef {import('typeorm').QueryRunner} QueryRunner
*/

/**
* @class
* @implements {MigrationInterface}
*/
module.exports = class AddWalletNameToIpLog1781004217553 {
name = 'AddWalletNameToIpLog1781004217553'

/**
* @param {QueryRunner} queryRunner
*/
async up(queryRunner) {
await queryRunner.query(`ALTER TABLE "ip_log" ADD "walletName" character varying(256)`);
}

/**
* @param {QueryRunner} queryRunner
*/
async down(queryRunner) {
await queryRunner.query(`ALTER TABLE "ip_log" DROP COLUMN "walletName"`);
}
}
2 changes: 1 addition & 1 deletion src/shared/auth/ip-country.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class IpCountryGuard implements CanActivate {
const address = req.body?.address ?? req.user?.address;
if (!address) throw new BadRequestException('Address is required');

const ipLog = await this.ipLogService.create(ip, req.url, address, req.body?.walletType);
const ipLog = await this.ipLogService.create(ip, req.url, address, req.body?.walletType, req.body?.wallet);
if (!ipLog.result) throw new ForbiddenException('The country of IP address is not allowed');

const region = +req.body?.region;
Expand Down
3 changes: 3 additions & 0 deletions src/shared/models/ip-log/ip-log.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export class IpLog extends IEntity {
@Column({ length: 256, nullable: true })
walletType?: WalletType;

@Column({ length: 256, nullable: true })
walletName?: string;

@Index()
@ManyToOne(() => User, { nullable: true })
user?: User;
Expand Down
15 changes: 12 additions & 3 deletions src/shared/models/ip-log/ip-log.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,25 @@ export class IpLogService {
private readonly idCache = new AsyncCache<IpLog>(CacheItemResetPeriod.EVERY_6_MONTHS);
private readonly recentLogCache = new AsyncCache<IpLog>(CacheItemResetPeriod.EVERY_10_SECONDS);

async create(ip: string, url: string, address: string, walletType?: WalletType, userData?: UserData): Promise<IpLog> {
const cacheKey = `${ip}:${address}:${url}:${walletType ?? ''}:${userData?.id ?? ''}`;
async create(
ip: string,
url: string,
address: string,
walletType?: WalletType,
walletName?: string,
userData?: UserData,
): Promise<IpLog> {
const cacheKey = `${ip}:${address}:${url}:${walletType ?? ''}:${walletName ?? ''}:${userData?.id ?? ''}`;

return this.recentLogCache.get(cacheKey, () => this.doCreate(ip, url, address, walletType, userData));
return this.recentLogCache.get(cacheKey, () => this.doCreate(ip, url, address, walletType, walletName, userData));
}

private async doCreate(
ip: string,
url: string,
address: string,
walletType?: WalletType,
walletName?: string,
userData?: UserData,
): Promise<IpLog> {
const { country, result, user } = await this.checkIpCountry(ip, address);
Expand All @@ -48,6 +56,7 @@ export class IpLogService {
user,
userData: userData ?? user?.userData,
walletType,
walletName,
});

return this.ipLogRepo.save(ipLog);
Expand Down
11 changes: 10 additions & 1 deletion src/subdomains/generic/user/models/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface MailKeyData {
userDataId: number;
loginUrl: string;
redirectUri?: string;
walletName?: string;
}

@Injectable()
Expand Down Expand Up @@ -297,6 +298,7 @@ export class AuthService {
userDataId: userData.id,
loginUrl: url,
redirectUri: dto.redirectUri,
walletName: loginWallet?.name,
});

// send notification
Expand Down Expand Up @@ -337,7 +339,14 @@ export class AuthService {
const account = await this.userDataService.getUserData(entry.userDataId, { users: true, wallet: true });
if (account.status === UserDataStatus.MERGED) throw new UnauthorizedException('User data is merged');

const ipLog = await this.ipLogService.create(ip, entry.loginUrl, entry.mail, undefined, account);
const ipLog = await this.ipLogService.create(
ip,
entry.loginUrl,
entry.mail,
undefined,
entry.walletName,
account,
);
if (!ipLog.result) throw new Error('The country of IP address is not allowed');

const token = this.generateAccountToken(account, ip);
Expand Down