-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial * fixes after testing * refactor * use cfl navigate * new cfl package
- Loading branch information
Showing
17 changed files
with
544 additions
and
376 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,80 +1,29 @@ | ||
import { type Schema, type StringSchema, string as YupString } from "yup" | ||
import CryptoJS from "crypto-js" | ||
|
||
type Options<S extends Schema, Extras = {}> = Partial<{ schema: S } & Extras> | ||
|
||
export function classIdSchema(options?: Options<StringSchema>) { | ||
const { schema = YupString() } = options || {} | ||
|
||
return schema.matches(/^[A-Z0-9]{5}$/, "invalid class code") | ||
} | ||
|
||
export function teacherPasswordSchema(options?: Options<StringSchema>) { | ||
const { schema = YupString() } = options || {} | ||
|
||
return schema | ||
.min(10, "password must be at least 10 characters long") | ||
.matches(/[A-Z]/, "password must contain at least one uppercase letter") | ||
.matches(/[a-z]/, "password must contain at least one lowercase letter") | ||
.matches(/[0-9]/, "password must contain at least one digit") | ||
.matches( | ||
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/, | ||
"password must contain at least one special character", | ||
) | ||
} | ||
|
||
export function studentPasswordSchema(options?: Options<StringSchema>) { | ||
const { schema = YupString() } = options || {} | ||
|
||
return schema.min(6, "password must be at least 6 characters long") | ||
} | ||
|
||
export function indyPasswordSchema(options?: Options<StringSchema>) { | ||
const { schema = YupString() } = options || {} | ||
|
||
return schema | ||
.min(8, "password must be at least 8 characters long") | ||
.matches(/[A-Z]/, "password must contain at least one uppercase letter") | ||
.matches(/[a-z]/, "password must contain at least one lowercase letter") | ||
.matches(/[0-9]/, "password must contain at least one digit") | ||
} | ||
|
||
export function pwnedPasswordSchema( | ||
options?: Options<StringSchema, { onError: (error: unknown) => void }>, | ||
) { | ||
const { schema = YupString().required(), onError } = options || {} | ||
|
||
return schema.test({ | ||
message: "password is too common", | ||
test: async password => { | ||
try { | ||
// Do not raise validation error if no password. | ||
if (!password) return true | ||
|
||
// Hash the password. | ||
const hashedPassword = CryptoJS.SHA1(password).toString().toUpperCase() | ||
const hashPrefix = hashedPassword.substring(0, 5) | ||
const hashSuffix = hashedPassword.substring(5) | ||
|
||
// Call Pwned Passwords API. | ||
// https://haveibeenpwned.com/API/v3#SearchingPwnedPasswordsByRange | ||
const response = await fetch( | ||
`https://api.pwnedpasswords.com/range/${hashPrefix}`, | ||
) | ||
// TODO: Standardize how to log non-okay responses. | ||
if (!response.ok) throw Error() | ||
|
||
// Parse response. | ||
const data = await response.text() | ||
return !data.includes(hashSuffix) | ||
} catch (error) { | ||
console.error(error) | ||
|
||
if (onError) onError(error) | ||
|
||
// Do not raise validation error if a different error occurred. | ||
return true | ||
} | ||
}, | ||
}) | ||
} | ||
import * as yup from "yup" | ||
|
||
export const userIdSchema = yup.number() | ||
|
||
export const classIdSchema = yup | ||
.string() | ||
.matches(/^[A-Z0-9]{5}$/, "invalid class code") | ||
|
||
export const teacherPasswordSchema = yup | ||
.string() | ||
.min(10, "password must be at least 10 characters long") | ||
.matches(/[A-Z]/, "password must contain at least one uppercase letter") | ||
.matches(/[a-z]/, "password must contain at least one lowercase letter") | ||
.matches(/[0-9]/, "password must contain at least one digit") | ||
.matches( | ||
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/, | ||
"password must contain at least one special character", | ||
) | ||
|
||
export const studentPasswordSchema = yup | ||
.string() | ||
.min(6, "password must be at least 6 characters long") | ||
|
||
export const indyPasswordSchema = yup | ||
.string() | ||
.min(8, "password must be at least 8 characters long") | ||
.matches(/[A-Z]/, "password must contain at least one uppercase letter") | ||
.matches(/[a-z]/, "password must contain at least one lowercase letter") | ||
.matches(/[0-9]/, "password must contain at least one digit") |
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
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
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
Oops, something went wrong.