Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Review firebase rule to make sure it aligns with API's password rules #191

Merged
merged 7 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/web-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
branches: [main]
paths:
- "web-app/**"
- "functions"
- "functions/**"
push:
branches: [main]
workflow_dispatch:
Expand Down Expand Up @@ -196,6 +196,12 @@ jobs:
working-directory: functions
run: yarn test

- name: Set Firebase Password Policy
working-directory: functions/packages/firebase-password-policy
run: |
yarn build
node lib/index.js

- name: Deploy Firebase Functions
working-directory: functions
run: npx firebase deploy --only functions
Expand Down
33 changes: 33 additions & 0 deletions functions/packages/firebase-password-policy/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = {
root: true,
env: {
es6: true,
node: true,
},
extends: [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
"google",
"plugin:@typescript-eslint/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: ["tsconfig.json", "tsconfig.dev.json"],
sourceType: "module",
},
ignorePatterns: [
"/lib/**/*", // Ignore built files.
"**/*config.*", // Ignore config files.
],
plugins: [
"@typescript-eslint",
"import",
],
rules: {
"quotes": ["error", "double"],
"import/no-unresolved": 0,
"indent": ["error", 2],
},
};
9 changes: 9 additions & 0 deletions functions/packages/firebase-password-policy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Compiled JavaScript files
lib/**/*.js
lib/**/*.js.map

# TypeScript v1 declaration files
typings/

# Node.js dependency directory
node_modules
5 changes: 5 additions & 0 deletions functions/packages/firebase-password-policy/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],
};
33 changes: 33 additions & 0 deletions functions/packages/firebase-password-policy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "firebase-password-policy",
"version": "1.0.0",
"engines": {
"node": "18"
},
"scripts": {
"lint": "eslint --ext .js,.ts .",
"build": "tsc",
"build:watch": "tsc --watch",
"test": "jest"
},
"dependencies": {
"firebase": "^10.6.0",
"firebase-admin": "^11.11.1"
},
"devDependencies": {
"@types/jest": "^29.5.8",
"@typescript-eslint/eslint-plugin": "^5.12.0",
"@typescript-eslint/parser": "^5.12.0",
"eslint": "^8.9.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.25.4",
"firebase-functions-test": "^3.1.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.0"
},
"main": "lib/index.js",
"description": "Firebase function that configures password policy",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {setPasswordPolicyConfig} from "../impl/firebase-password-policy-impl";
import {getAuth} from "firebase-admin/auth";

jest.mock("firebase-admin/auth", () => ({
getAuth: jest.fn(),
}));

describe("setPasswordPolicyConfig", () => {
let mockUpdateProjectConfig: jest.Mock;

beforeEach(() => {
mockUpdateProjectConfig = jest.fn();
(getAuth as jest.Mock).mockReturnValue({
projectConfigManager: () => ({
updateProjectConfig: mockUpdateProjectConfig,
}),
});

// Mock console.log to verify its calls
global.console = {log: jest.fn()} as unknown as Console;
});

it("should log success message when updating password policy successfully",
async () => {
mockUpdateProjectConfig.mockResolvedValueOnce({});

await setPasswordPolicyConfig();

expect(console.log)
.toHaveBeenCalledWith("Password policy updated successfully");
});

it("should log error message when there is an error updating password policy",
async () => {
const mockError = new Error("Update failed");
mockUpdateProjectConfig.mockRejectedValueOnce(mockError);

await setPasswordPolicyConfig();

expect(console.log)
.toHaveBeenCalledWith("Error updating password policy: " + mockError);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {getAuth} from "firebase-admin/auth";

/**
* Sets the password policy for the Firebase project.
*/
export const setPasswordPolicyConfig = async () => {
try {
await getAuth().projectConfigManager().updateProjectConfig({
passwordPolicyConfig: {
enforcementState: "ENFORCE",
constraints: {
requireUppercase: true,
requireLowercase: true,
requireNonAlphanumeric: true,
requireNumeric: true,
minLength: 12,
},
},
});
console.log("Password policy updated successfully");
} catch (error) {
console.log("Error updating password policy: " + error);
}
};
5 changes: 5 additions & 0 deletions functions/packages/firebase-password-policy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {initializeApp} from "firebase-admin/app";
import * as impl from "./impl/firebase-password-policy-impl";

initializeApp();
impl.setPasswordPolicyConfig().then();
5 changes: 5 additions & 0 deletions functions/packages/firebase-password-policy/tsconfig.dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"include": [
".eslintrc.js"
]
}
15 changes: 15 additions & 0 deletions functions/packages/firebase-password-policy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "es2017"
},
"compileOnSave": true,
"include": [
"src"
]
}
2 changes: 1 addition & 1 deletion functions/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3674,7 +3674,7 @@ find-up@^5.0.0:
locate-path "^6.0.0"
path-exists "^4.0.0"

firebase-admin@^11.8.0:
firebase-admin@^11.11.1, firebase-admin@^11.8.0:
version "11.11.1"
resolved "https://registry.yarnpkg.com/firebase-admin/-/firebase-admin-11.11.1.tgz#6712923de70d218c9f514d73005d976d03339605"
integrity sha512-UyEbq+3u6jWzCYbUntv/HuJiTixwh36G1R9j0v71mSvGAx/YZEWEW7uSGLYxBYE6ckVRQoKMr40PYUEzrm/4dg==
Expand Down
2 changes: 1 addition & 1 deletion web-app/src/app/screens/SingUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function SignUp(): React.ReactElement {
password: Yup.string()
.required('Password is required')
.matches(
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[.;!@#$%^&*])(?=.{12,})/,
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^$*.[\]{}()?"!@#%&/\\,><':;|_~`])(?=.{12,})/,
passwordValidatioError,
),
confirmPassword: Yup.string().oneOf(
Expand Down
2 changes: 1 addition & 1 deletion web-app/src/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export type AppErrors = {
};

export const passwordValidatioError =
'Password must contain at least one uppercase letter, one lowercase letter, one digit, one special char(!@#$%^&*) and be at least 12 chars long';
'Password must contain at least one uppercase letter, one lowercase letter, one digit, one special char(^ $ * . [ ] { } ( ) ? " ! @ # % & / \\ , > < \' : ; | _ ~ `) and be at least 12 chars long';

export enum OauthProvider {
Google = 'Google',
Expand Down