Skip to content

Commit d9a1237

Browse files
edwardzh976Copilotben-zgithub-actions[bot]
authored
Provision elastic users (#3635)
## Description <!--- Please provide a summary of your changes. Make sure to include relevant motivation, context, and link related documents/conversations. --> <!--- Use [linking keywords](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) like `Resolves #<issue_number>` to automatically close issues. --> When you submit the onboarding form you now have the option to automatically create an elastic account and connect with the WATonomous Elastic Organization. Resolves WATonomous/infra-config#3404 ## Checklist - [x] I have read and understood the [WATcloud Guidelines](https://cloud.watonomous.ca/docs/community-docs/watcloud/guidelines) - [x] I have performed a self-review of my code --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Ben Zhang <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 35ac947 commit d9a1237

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

components/onboarding-form.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { ariaDescribedByIds, toPathSchema } from "@rjsf/utils";
3535
import { CheckedState } from "@radix-ui/react-checkbox";
3636
import { Code, Pre } from "nextra/components";
3737
import { Textarea } from "@/components/ui/textarea";
38-
import { debounce, deepSet, encryptUnixPassword, getDayjsRelative, getObjectPaths, getValuesFromPath, isCryptFormat } from "@/lib/utils";
38+
import { debounce, deepSet, encryptUnixPassword, getDayjsRelative, encryptBcryptPassword, getObjectPaths, getValuesFromPath, isCryptFormat, isBcryptFormat } from "@/lib/utils";
3939
import { Toaster } from "@/components/ui/sonner";
4040
import { toast } from "sonner";
4141
import { useRouter } from "next/router";
@@ -68,6 +68,7 @@ function getFormState() {
6868
}
6969

7070
const cryptPaths = getObjectPaths(userSchemaJSON as JSONSchema7, (property: Record<string, any>) => property["$transform"] === "crypt");
71+
const bcryptPaths = getObjectPaths(userSchemaJSON as JSONSchema7, (property: Record<string, any>) => property["$transform"] === "bcrypt");
7172

7273
const validator = createPrecompiledValidator(
7374
userSchemaValidate,
@@ -80,7 +81,7 @@ function string_to_mdx(str: string) {
8081

8182
function postprocessFormData(data: Record<string, unknown>) {
8283
for (const path of cryptPaths) {
83-
for (const { value, path: actualPath } of getValuesFromPath(
84+
for (const {value, path: actualPath } of getValuesFromPath(
8485
data,
8586
path
8687
)) {
@@ -91,7 +92,19 @@ function postprocessFormData(data: Record<string, unknown>) {
9192
}
9293
}
9394
}
94-
95+
for (const path of bcryptPaths) {
96+
for (const {value, path: actualPath } of getValuesFromPath(
97+
data,
98+
path
99+
)) {
100+
// If the password is already encrypted, we don't need to do anything.
101+
// Else, encrypt it.
102+
if (value && !isBcryptFormat(value)) {
103+
const encryptedValue = encryptBcryptPassword(value);
104+
deepSet(data, actualPath, encryptedValue);
105+
}
106+
}
107+
}
95108
return data;
96109
}
97110

lib/bcrypt-ts.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module 'bcrypt-ts/browser' {
2+
export function genSaltSync(rounds?: number): string;
3+
export function hashSync(data: string, salt: string): string;
4+
}

lib/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dayjsTimezone from "dayjs/plugin/timezone";
66
import dayjsRelativeTime from "dayjs/plugin/relativeTime";
77
import { JSONSchema7 } from "json-schema";
88
import { sha512crypt } from 'sha512crypt-node';
9+
import { genSaltSync, hashSync } from "bcrypt-ts/browser";
910

1011
export function cn(...inputs: ClassValue[]) {
1112
return twMerge(clsx(inputs))
@@ -269,6 +270,11 @@ export function encryptUnixPassword(password: string): string {
269270
return sha512crypt(password, genRanHex(16));
270271
}
271272

273+
export function encryptBcryptPassword(password: string): string {
274+
const salt = genSaltSync(10)
275+
return hashSync(password, salt);
276+
}
277+
272278
export function isCryptFormat(s: string) {
273279
// Regular expression for crypt format
274280
var pattern = new RegExp(
@@ -278,6 +284,13 @@ export function isCryptFormat(s: string) {
278284
return match;
279285
}
280286

287+
export function isBcryptFormat(s: string) {
288+
// Regular expression for bcrypt format
289+
const pattern: RegExp = /^\$2[ayb]\$\d{2}\$[A-Za-z0-9./]{53}$/;
290+
var match = pattern.test(s);
291+
return match;
292+
}
293+
281294
export function flatMap<T, U>(arr: T[], f: (t: T, index: number) => U[]): U[] {
282295
return arr.reduce<U[]>((acc, val, index) => acc.concat(f(val, index)), []);
283296
}

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@uppy/aws-s3": "^3.6.2",
5252
"@uppy/dashboard": "^3.7.5",
5353
"@uppy/react": "^3.2.2",
54+
"bcrypt-ts": "^5.0.3",
5455
"class-variance-authority": "^0.7.0",
5556
"clsx": "^2.0.0",
5657
"cmdk": "^0.2.0",

0 commit comments

Comments
 (0)