Initial Commit
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import bcrypt from "bcrypt";
|
||||
|
||||
/**
|
||||
* Verifies a hash against a password
|
||||
* @param {string} pass The password
|
||||
* @param {string} hash The hash
|
||||
*/
|
||||
export const verifyHash = (pass: string, hash: string) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
void bcrypt.compare(pass, hash, (err, res) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(res);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Generates a hash from plain text
|
||||
* @param {string} pass The password
|
||||
* @returns {Promise<string>} Password hash
|
||||
*/
|
||||
export const createHash = (pass: string): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
void bcrypt.hash(pass, 10, (err, res) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
resolve(res);
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
import { SES } from "@aws-sdk/client-ses";
|
||||
import {
|
||||
AWS_ACCESS_KEY_ID,
|
||||
AWS_REGION,
|
||||
AWS_SECRET_ACCESS_KEY,
|
||||
} from "../app/constants";
|
||||
|
||||
export const ses = new SES({
|
||||
apiVersion: "2010-12-01",
|
||||
region: AWS_REGION,
|
||||
credentials: {
|
||||
accessKeyId: AWS_ACCESS_KEY_ID,
|
||||
secretAccessKey: AWS_SECRET_ACCESS_KEY,
|
||||
},
|
||||
});
|
||||
|
||||
export const getIdentities = async (identities: string[]) => {
|
||||
const res = await ses.getIdentityVerificationAttributes({
|
||||
Identities: identities.flatMap((identity) => [identity.split("@")[1]]),
|
||||
});
|
||||
|
||||
const parsedResult = Object.entries(res.VerificationAttributes ?? {});
|
||||
return parsedResult.map((obj) => {
|
||||
return { email: obj[0], status: obj[1].VerificationStatus };
|
||||
});
|
||||
};
|
||||
|
||||
export const verifyIdentity = async (email: string) => {
|
||||
const DKIM = await ses.verifyDomainDkim({
|
||||
Domain: email.includes("@") ? email.split("@")[1] : email,
|
||||
});
|
||||
|
||||
await ses.setIdentityMailFromDomain({
|
||||
Identity: email.includes("@") ? email.split("@")[1] : email,
|
||||
MailFromDomain: `plunk.${email.includes("@") ? email.split("@")[1] : email}`,
|
||||
});
|
||||
|
||||
return DKIM.DkimTokens;
|
||||
};
|
||||
|
||||
export const getIdentityVerificationAttributes = async (email: string) => {
|
||||
const attributes = await ses.getIdentityDkimAttributes({
|
||||
Identities: [email, email.split("@")[1]],
|
||||
});
|
||||
|
||||
const parsedAttributes = Object.entries(attributes.DkimAttributes ?? {});
|
||||
|
||||
return {
|
||||
email: parsedAttributes[0][0],
|
||||
tokens: parsedAttributes[0][1].DkimTokens,
|
||||
status: parsedAttributes[0][1].DkimVerificationStatus,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
import { randomBytes } from "node:crypto";
|
||||
|
||||
/**
|
||||
* A function that generates a random 24 byte API secret
|
||||
* @param type
|
||||
*/
|
||||
export function generateToken(type: "secret" | "public") {
|
||||
return `${type === "secret" ? "sk" : "pk"}_${randomBytes(24).toString("hex")}`;
|
||||
}
|
||||
Reference in New Issue
Block a user