summary refs log tree commit diff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/src/util/Email.ts20
-rw-r--r--util/src/util/Token.ts (renamed from util/src/util/checkToken.ts)20
-rw-r--r--util/src/util/index.ts3
3 files changed, 42 insertions, 1 deletions
diff --git a/util/src/util/Email.ts b/util/src/util/Email.ts
new file mode 100644
index 00000000..c304f584
--- /dev/null
+++ b/util/src/util/Email.ts
@@ -0,0 +1,20 @@
+export const EMAIL_REGEX =
+	/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
+
+export function adjustEmail(email: string): string | undefined {
+	if (!email) return email;
+	// body parser already checked if it is a valid email
+	const parts = <RegExpMatchArray>email.match(EMAIL_REGEX);
+	// @ts-ignore
+	if (!parts || parts.length < 5) return undefined;
+	const domain = parts[5];
+	const user = parts[1];
+
+	// TODO: check accounts with uncommon email domains
+	if (domain === "gmail.com" || domain === "googlemail.com") {
+		// replace .dots and +alternatives -> Gmail Dot Trick https://support.google.com/mail/answer/7436150 and https://generator.email/blog/gmail-generator
+		return user.replace(/[.]|(\+.*)/g, "") + "@gmail.com";
+	}
+
+	return email;
+}
diff --git a/util/src/util/checkToken.ts b/util/src/util/Token.ts
index 8415e8c0..111d59a2 100644
--- a/util/src/util/checkToken.ts
+++ b/util/src/util/Token.ts
@@ -1,4 +1,5 @@
 import jwt, { VerifyOptions } from "jsonwebtoken";
+import { Config } from "./Config";
 import { User } from "../entities";
 
 export const JWTOptions: VerifyOptions = { algorithms: ["HS256"] };
@@ -21,3 +22,22 @@ export function checkToken(token: string, jwtSecret: string): Promise<any> {
 		});
 	});
 }
+
+export async function generateToken(id: string) {
+	const iat = Math.floor(Date.now() / 1000);
+	const algorithm = "HS256";
+
+	return new Promise((res, rej) => {
+		jwt.sign(
+			{ id: id, iat },
+			Config.get().security.jwtSecret,
+			{
+				algorithm,
+			},
+			(err, token) => {
+				if (err) return rej(err);
+				return res(token);
+			}
+		);
+	});
+}
diff --git a/util/src/util/index.ts b/util/src/util/index.ts
index 3160380f..d73bf4ca 100644
--- a/util/src/util/index.ts
+++ b/util/src/util/index.ts
@@ -1,11 +1,12 @@
 export * from "./ApiError";
 export * from "./BitField";
-export * from "./checkToken";
+export * from "./Token";
 export * from "./cdn";
 export * from "./Config";
 export * from "./Constants";
 export * from "./Database";
 export * from "./Event";
+export * from "./Email";
 export * from "./Intents";
 export * from "./MessageFlags";
 export * from "./Permissions";