mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-08 01:39:22 +08:00
feat(server): verify domain record (#8608)
This commit is contained in:
@@ -40,6 +40,11 @@ export interface AuthRuntimeConfigurations {
|
|||||||
*/
|
*/
|
||||||
allowSignup: boolean;
|
allowSignup: boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether require email domain record verification before access restricted resources
|
||||||
|
*/
|
||||||
|
requireEmailDomainVerification: boolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether require email verification before access restricted resources
|
* Whether require email verification before access restricted resources
|
||||||
*/
|
*/
|
||||||
@@ -76,6 +81,10 @@ defineRuntimeConfig('auth', {
|
|||||||
desc: 'Whether allow new registrations',
|
desc: 'Whether allow new registrations',
|
||||||
default: true,
|
default: true,
|
||||||
},
|
},
|
||||||
|
requireEmailDomainVerification: {
|
||||||
|
desc: 'Whether require email domain record verification before accessing restricted resources',
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
requireEmailVerification: {
|
requireEmailVerification: {
|
||||||
desc: 'Whether require email verification before accessing restricted resources',
|
desc: 'Whether require email verification before accessing restricted resources',
|
||||||
default: true,
|
default: true,
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { resolveMx, resolveTxt, setServers } from 'node:dns/promises';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
@@ -55,7 +57,16 @@ export class AuthController {
|
|||||||
private readonly user: UserService,
|
private readonly user: UserService,
|
||||||
private readonly token: TokenService,
|
private readonly token: TokenService,
|
||||||
private readonly config: Config
|
private readonly config: Config
|
||||||
) {}
|
) {
|
||||||
|
if (config.node.dev) {
|
||||||
|
// set DNS servers in dev mode
|
||||||
|
// NOTE: some network debugging software uses DNS hijacking
|
||||||
|
// to better debug traffic, but their DNS servers may not
|
||||||
|
// handle the non dns query(like txt, mx) correctly, so we
|
||||||
|
// set a public DNS server here to avoid this issue.
|
||||||
|
setServers(['1.1.1.1', '8.8.8.8']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Public()
|
@Public()
|
||||||
@Post('/preflight')
|
@Post('/preflight')
|
||||||
@@ -147,6 +158,33 @@ export class AuthController {
|
|||||||
if (!allowSignup) {
|
if (!allowSignup) {
|
||||||
throw new SignUpForbidden();
|
throw new SignUpForbidden();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const requireEmailDomainVerification = await this.config.runtime.fetch(
|
||||||
|
'auth/requireEmailDomainVerification'
|
||||||
|
);
|
||||||
|
if (requireEmailDomainVerification) {
|
||||||
|
// verify domain has MX, SPF, DMARC records
|
||||||
|
const [name, domain, ...rest] = email.split('@');
|
||||||
|
if (rest.length || !domain) {
|
||||||
|
throw new InvalidEmail();
|
||||||
|
}
|
||||||
|
const [mx, spf, dmarc] = await Promise.allSettled([
|
||||||
|
resolveMx(domain).then(t => t.map(mx => mx.exchange).filter(Boolean)),
|
||||||
|
resolveTxt(domain).then(t =>
|
||||||
|
t.map(([k]) => k).filter(txt => txt.includes('v=spf1'))
|
||||||
|
),
|
||||||
|
resolveTxt('_dmarc.' + domain).then(t =>
|
||||||
|
t.map(([k]) => k).filter(txt => txt.includes('v=DMARC1'))
|
||||||
|
),
|
||||||
|
]).then(t => t.filter(t => t.status === 'fulfilled').map(t => t.value));
|
||||||
|
if (!mx?.length || !spf?.length || !dmarc?.length) {
|
||||||
|
throw new InvalidEmail();
|
||||||
|
}
|
||||||
|
// filter out alias emails
|
||||||
|
if (name.includes('+') || name.includes('.')) {
|
||||||
|
throw new InvalidEmail();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = await this.token.createToken(TokenType.SignIn, email);
|
const token = await this.token.createToken(TokenType.SignIn, email);
|
||||||
|
|||||||
Reference in New Issue
Block a user