Skip to content

Commit

Permalink
enhance(backend): ログイン試行時にuserid/ip/headerをメールで送るように
Browse files Browse the repository at this point in the history
  • Loading branch information
lqvp committed Jan 2, 2025
1 parent 328a67f commit c3440da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/backend/src/server/api/SigninApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { FastifyReplyError } from '@/misc/fastify-reply-error.js';
import { NotificationService } from '@/core/NotificationService.js';
import { RateLimiterService } from './RateLimiterService.js';
import { SigninService } from './SigninService.js';
import { EmailService } from '@/core/EmailService.js';
import type { AuthenticationResponseJSON } from '@simplewebauthn/types';
import type { FastifyReply, FastifyRequest } from 'fastify';

Expand Down Expand Up @@ -54,6 +55,7 @@ export class SigninApiService {
private idService: IdService,
private rateLimiterService: RateLimiterService,
private signinService: SigninService,
private emailService: EmailService,
private userAuthService: UserAuthService,
private webAuthnService: WebAuthnService,
private captchaService: CaptchaService,
Expand Down Expand Up @@ -174,6 +176,19 @@ export class SigninApiService {
userIp: request.ip,
});

const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
if (profile.email && profile.emailVerified) {
this.emailService.sendEmail(profile.email, 'Login failed / ログインに失敗しました',
`userid: ${user.name ?? `@${user.username}`} <br>` +
`ip: ${request.ip} <br>` +
'header: ' + JSON.stringify(request.headers) + '<br>' +
'There is a new login. If you do not recognize this login, update the security status of your account, including changing your password. / 新しいログインがありました。このログインに心当たりがない場合は、パスワードを変更するなど、アカウントのセキュリティ状態を更新してください。',
`userid: ${user.name ?? `@${user.username}`} \n` +
`ip: ${request.ip} \n` +
'header: ' + JSON.stringify(request.headers) + '\n' +
'There is a new login. If you do not recognize this login, update the security status of your account, including changing your password. / 新しいログインがありました。このログインに心当たりがない場合は、パスワードを変更するなど、アカウントのセキュリティ状態を更新してください。');
}

return error(status ?? 500, failure ?? { id: '4e30e80c-e338-45a0-8c8f-44455efa3b76' });
};

Expand Down
6 changes: 6 additions & 0 deletions packages/backend/src/server/api/SigninService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ export class SigninService {
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
if (profile.email && profile.emailVerified) {
this.emailService.sendEmail(profile.email, 'New login / ログインがありました',
`userid: ${user.name ?? `@${user.username}`} <br>` +
`ip: ${request.ip} <br>` +
'header: ' + JSON.stringify(request.headers) + '<br>' +
'There is a new login. If you do not recognize this login, update the security status of your account, including changing your password. / 新しいログインがありました。このログインに心当たりがない場合は、パスワードを変更するなど、アカウントのセキュリティ状態を更新してください。',
`userid: ${user.name ?? `@${user.username}`} \n` +
`ip: ${request.ip} \n` +
'header: ' + JSON.stringify(request.headers) + '\n' +
'There is a new login. If you do not recognize this login, update the security status of your account, including changing your password. / 新しいログインがありました。このログインに心当たりがない場合は、パスワードを変更するなど、アカウントのセキュリティ状態を更新してください。');
}
});
Expand Down

0 comments on commit c3440da

Please sign in to comment.