-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from devondragon/issue-20-Switch-to-using-Itty…
…-Router Issue 20 switch to using itty router
- Loading branch information
Showing
15 changed files
with
724 additions
and
552 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,8 @@ | |
], | ||
"devDependencies": { | ||
"lerna": "^8.1.2" | ||
}, | ||
"dependencies": { | ||
"itty-router": "^5.0.17" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface Env { | ||
sessionstore: KVNamespace; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Env } from './env'; | ||
|
||
export function generateSessionId(): string { | ||
return crypto.randomUUID(); | ||
} | ||
|
||
export async function createSession(data: any, env: Env): Promise<string> { | ||
const sessionId = generateSessionId(); | ||
try { | ||
await env.sessionstore.put(sessionId, JSON.stringify(data)); | ||
} catch (error) { | ||
console.error("Error creating session: " + error); | ||
throw new Error('Failed to create session'); | ||
} | ||
return sessionId; | ||
} | ||
|
||
export async function updateSession(sessionId: string, data: any, env: Env): Promise<void> { | ||
try { | ||
await env.sessionstore.put(sessionId, JSON.stringify(data)); | ||
} catch (error) { | ||
console.error("Error updating session: " + error); | ||
throw new Error('Failed to update session'); | ||
} | ||
} | ||
|
||
export async function addToSession(sessionId: string, data: any, env: Env): Promise<void> { | ||
const sessionData = await getSessionData(sessionId, env); | ||
await updateSession(sessionId, { ...sessionData, ...data }, env); | ||
} | ||
|
||
export async function getSessionData(sessionId: string, env: Env): Promise<any> { | ||
const data = await env.sessionstore.get(sessionId); | ||
return data ? JSON.parse(data) : null; | ||
} | ||
|
||
export async function deleteSession(sessionId: string, env: Env): Promise<void> { | ||
try { | ||
await env.sessionstore.delete(sessionId); | ||
} catch (error) { | ||
console.error("Error deleting session: " + error); | ||
throw new Error('Failed to delete session'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Hashes a password using the SHA-256 algorithm. | ||
* | ||
* @param password - The password to be hashed. | ||
* @returns A Promise that resolves to the hashed password. | ||
*/ | ||
export async function hashPassword(password: string): Promise<string> { | ||
const salt = crypto.getRandomValues(new Uint8Array(16)).join(''); | ||
const data = new TextEncoder().encode(salt + password); | ||
const hashBuffer = await crypto.subtle.digest('SHA-256', data); | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); | ||
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); | ||
return `${salt}:${hashHex}`; | ||
} | ||
|
||
|
||
/** | ||
* Compares a provided password with a stored hash. | ||
* @param providedPassword - The password provided by the user. | ||
* @param storedHash - The stored hash of the password. | ||
* @returns A promise that resolves to a boolean indicating whether the provided password matches the stored hash. | ||
*/ | ||
export async function comparePassword(providedPassword: string, storedHash: string): Promise<boolean> { | ||
const [salt, originalHash] = storedHash.split(':'); | ||
const data = new TextEncoder().encode(salt + providedPassword); | ||
const hashBuffer = await crypto.subtle.digest('SHA-256', data); | ||
const hashArray = Array.from(new Uint8Array(hashBuffer)); | ||
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); | ||
return hashHex === originalHash; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Represents the environment configuration for the user management module. | ||
*/ | ||
export interface Env { | ||
usersDB: D1Database; | ||
sessionService: Fetcher; | ||
EMAIL_FROM: string; | ||
EMAIL_FROM_NAME: string; | ||
FORGOT_PASSWORD_URL: string; | ||
TOKEN_VALID_MINUTES: number; | ||
EMAIL_DKIM_DOMAIN: string; | ||
EMAIL_DKIM_SELECTOR: string; | ||
EMAIL_DKIM_PRIVATE_KEY: string; | ||
} | ||
|
||
export function getUsersDB(env: Env): D1Database { | ||
return env.usersDB; | ||
} | ||
|
||
export function getSessionService(env: Env): Fetcher { | ||
return env.sessionService; | ||
} | ||
|
||
export function getEmailFrom(env: Env): string { | ||
return env.EMAIL_FROM; | ||
} | ||
|
||
export function getEmailFromName(env: Env): string { | ||
return env.EMAIL_FROM_NAME; | ||
} | ||
|
||
export function getForgotPasswordUrl(env: Env): string { | ||
return env.FORGOT_PASSWORD_URL; | ||
} | ||
|
||
export function getTokenValidMinutes(env: Env): number { | ||
return env.TOKEN_VALID_MINUTES; | ||
} | ||
|
||
export function getEmailDkimDomain(env: Env): string { | ||
return env.EMAIL_DKIM_DOMAIN; | ||
} | ||
|
||
export function getEmailDkimSelector(env: Env): string { | ||
return env.EMAIL_DKIM_SELECTOR; | ||
} | ||
|
||
export function getEmailDkimPrivateKey(env: Env): string { | ||
return env.EMAIL_DKIM_PRIVATE_KEY; | ||
} |
Oops, something went wrong.