Skip to content

Commit

Permalink
Merge pull request #15 from marmelab/feat/totp-support
Browse files Browse the repository at this point in the history
Add support for TOTP MFA
  • Loading branch information
slax57 authored Feb 16, 2024
2 parents 9ad83ec + c5802f9 commit e4e73e0
Show file tree
Hide file tree
Showing 30 changed files with 5,249 additions and 3,348 deletions.
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ build-ra-auth-cognito:
@echo "Transpiling ra-auth-cognito files...";
@cd ./packages/ra-auth-cognito && yarn build

build-ra-auth-cognito-languages:
@echo "Transpiling ra-auth-cognito-language-english files...";
@cd ./packages/ra-auth-cognito-language-english && yarn build
@echo "Transpiling ra-auth-cognito-language-french files...";
@cd ./packages/ra-auth-cognito-language-french && yarn build

build-demo-react-admin:
@echo "Transpiling demo files...";
@cd ./packages/demo-react-admin && yarn build

build: build-ra-auth-cognito build-demo-react-admin ## compile ES6 files to JS
build: build-ra-auth-cognito build-ra-auth-cognito-languages build-demo-react-admin ## compile ES6 files to JS

lint: ## lint the code and check coding conventions
@echo "Running linter..."
Expand All @@ -40,5 +46,7 @@ test-unit: ## launch unit tests
start:
@cd ./packages/demo-react-admin && yarn start

publish: ## Publish the package
cd packages/ra-auth-cognito && npm publish
publish: ## Publish the packages
cd packages/ra-auth-cognito && npm publish
cd packages/ra-auth-cognito-language-english && npm publish
cd packages/ra-auth-cognito-language-french && npm publish
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ Feel free to play around with this demo, along with the Cognito config, to under
## License

This repository and the code it contains are licensed under the MIT License and sponsored by [marmelab](https://marmelab.com).


44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
"author": "Gildas Garcia <[email protected]> (https://marmelab.com/)",
"license": "MIT",
"devDependencies": {
"@babel/preset-env": "^7.15.0",
"@babel/preset-react": "^7.14.5",
"@babel/preset-typescript": "^7.15.0",
"@types/jest": "^29.1.0",
"@typescript-eslint/eslint-plugin": "^4.28.5",
"@typescript-eslint/parser": "^4.28.5",
"babel-jest": "^27.1.0",
"eslint": "^7.7.0",
"eslint-config-prettier": "^6.11.0",
"eslint-config-react-app": "^5.2.1",
"eslint-plugin-cypress": "^2.11.1",
"eslint-plugin-flowtype": "^5.2.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-react-hooks": "^4.1.0",
"jest": "^29.1.1",
"jest-environment-jsdom": "^29.1.1",
"prettier": "~2.1.1",
"@babel/preset-env": "^7.23.9",
"@babel/preset-react": "^7.23.3",
"@babel/preset-typescript": "^7.23.3",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"babel-jest": "^29.7.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-react-app": "^7.0.1",
"eslint-plugin-cypress": "^2.15.1",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"prettier": "^3.2.5",
"raf": "^3.4.1",
"ts-jest": "^29.0.3",
"typescript": "^4.8.4"
"ts-jest": "^29.1.2",
"typescript": "^5.3.3"
},
"scripts": {
"build": "yarn workspaces run build",
Expand Down
68 changes: 35 additions & 33 deletions packages/demo-react-admin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "demo-react-admin",
"version": "1.0.0",
"version": "1.1.0",
"private": true,
"scripts": {
"dev": "webpack-dev-server --progress --color --hot --mode development",
Expand All @@ -10,43 +10,45 @@
"type-check": "tsc --noEmit"
},
"dependencies": {
"@emotion/react": "^11.7.1",
"@emotion/styled": "^11.6.0",
"@mui/icons-material": "^5.0.1",
"@mui/material": "^5.0.2",
"amazon-cognito-identity-js": "^6.1.2",
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",
"@mui/icons-material": "^5.15.8",
"@mui/material": "^5.15.7",
"amazon-cognito-identity-js": "^6.3.7",
"jsonexport": "^3.2.0",
"lodash": "~4.17.5",
"prop-types": "^15.7.2",
"proxy-polyfill": "^0.3.0",
"ra-auth-cognito": "*",
"ra-data-fakerest": "^4.7.0",
"ra-data-json-server": "^4.7.0",
"ra-i18n-polyglot": "^4.7.0",
"ra-language-english": "^4.7.0",
"ra-language-french": "^4.7.0",
"lodash": "^4.17.21",
"prop-types": "^15.8.1",
"proxy-polyfill": "^0.3.2",
"qrcode.react": "^3.1.0",
"ra-auth-cognito": "workspace:^",
"ra-auth-cognito-language-english": "workspace:^",
"ra-auth-cognito-language-french": "workspace:^",
"ra-data-fakerest": "^4.16.9",
"ra-data-json-server": "^4.16.9",
"ra-i18n-polyglot": "^4.16.9",
"ra-language-english": "^4.16.9",
"ra-language-french": "^4.16.9",
"react": "^18.2.0",
"react-admin": "^4.7.0",
"react-admin": "^4.16.9",
"react-dom": "^18.2.0",
"react-hook-form": "^7.34.2",
"react-query": "^3.32.1",
"react-router": "^6.1.0",
"react-router-dom": "^6.1.0"
"react-hook-form": "^7.50.1",
"react-router": "^6.22.0",
"react-router-dom": "^6.22.0"
},
"devDependencies": {
"@hookform/devtools": "^4.0.2",
"@types/jest": "^29.2.3",
"@vitejs/plugin-react-refresh": "^1.3.3",
"html-loader": "~3.1.0",
"html-webpack-plugin": "~5.5.0",
"@hookform/devtools": "^4.3.1",
"@types/jest": "^29.5.12",
"@vitejs/plugin-react-refresh": "^1.3.6",
"html-loader": "^5.0.0",
"html-webpack-plugin": "^5.6.0",
"ignore-not-found-export-plugin": "^1.0.1",
"react-app-polyfill": "^1.0.4",
"style-loader": "~3.3.1",
"ts-loader": "^9.2.8",
"typescript": "^4.4.0",
"vite": "^2.2.3",
"webpack": "~5.70.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "~4.7.4"
"react-app-polyfill": "^3.0.0",
"style-loader": "^3.3.4",
"ts-loader": "^9.5.1",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"webpack": "^5.90.1",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
}
}
19 changes: 17 additions & 2 deletions packages/demo-react-admin/src/i18nProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { raAuthCognitoEnglishMessages } from 'ra-auth-cognito-language-english';
import polyglotI18nProvider from 'ra-i18n-polyglot';
import englishMessages from './i18n/en';
import { mergeTranslations } from 'react-admin';

const allEnglishMessages = mergeTranslations(
englishMessages,
raAuthCognitoEnglishMessages
);
const messages = {
fr: () => import('./i18n/fr').then(messages => messages.default),
fr: () =>
Promise.all([
import('./i18n/fr'),
import('ra-auth-cognito-language-french'),
]).then(([raMessages, raAuthCognitoMessages]) =>
mergeTranslations(
raMessages.default,
raAuthCognitoMessages.raAuthCognitoFrenchMessages
)
),
};

export default polyglotI18nProvider(
Expand All @@ -12,7 +27,7 @@ export default polyglotI18nProvider(
}

// Always fallback on english
return englishMessages;
return allEnglishMessages;
},
'en',
[
Expand Down
30 changes: 20 additions & 10 deletions packages/demo-react-admin/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import reactRefresh from '@vitejs/plugin-react-refresh';
import path from 'path';
import fs from 'fs';
import fs from 'node:fs';
import path from 'node:path';
import { defineConfig } from 'vite';

const packages = fs.readdirSync(path.resolve(__dirname, '../../packages'));
const aliases = packages.map(dirName => {
const packageJson = require(path.resolve(
__dirname,
'../../packages',
dirName,
'package.json'
));
const packageJson = require(
path.resolve(__dirname, '../../packages', dirName, 'package.json')
);
return {
find: new RegExp(`^${packageJson.name}$`),
replacement: path.resolve(
Expand All @@ -23,7 +21,7 @@ const aliases = packages.map(dirName => {
* https://vitejs.dev/config/
* @type { import('vite').UserConfig }
*/
export default {
export default defineConfig({
plugins: [reactRefresh()],
resolve: {
alias: [
Expand All @@ -38,5 +36,17 @@ export default {
port: 8081,
host: '0.0.0.0',
},
build: {
rollupOptions: {
onLog: (level, log, defaultHandler) => {
// Suppress log about module level directives, ie "use-client". Now a lot of react packages are using them.
// The log warns about it, and says that it ignores the directive.
if (log.code === 'MODULE_LEVEL_DIRECTIVE') {
return;
}
defaultHandler(level, log);
},
},
},
define: { 'process.env': {} },
};
});
50 changes: 50 additions & 0 deletions packages/ra-auth-cognito-language-english/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ra-auth-cognito-language-english

English translations for [ra-auth-cognito](https://www.npmjs.com/package/ra-auth-cognito).

## Installation

```sh
npm install ra-auth-cognito-language-english
# or
yarn add ra-auth-cognito-language-english
```

## Usage

```js
// in i18nProvider.js
import { raAuthCognitoEnglishMessages } from 'ra-auth-cognito-language-english';
import { mergeTranslations } from 'react-admin';
import polyglotI18nProvider from 'ra-i18n-polyglot';
import englishMessages from 'ra-language-english';

const allEnglishMessages = mergeTranslations(
englishMessages,
raAuthCognitoEnglishMessages
);

export const i18nProvider = polyglotI18nProvider(
locale => allEnglishMessages,
'en'
);

// in App.js
import { Admin, Resource, ListGuesser } from 'react-admin';
import { authRoutes } from 'ra-auth-cognito';
import { dataProvider } from './dataProvider';
import { authProvider } from './authProvider';
import { i18nProvider } from './i18nProvider';

export const MyAdmin = () => (
<Admin
dataProvider={dataProvider}
authProvider={authProvider}
i18nProvider={i18nProvider}
customRoutes={authRoutes}
>
<Resource name="posts" list={ListGuesser} />
<Resource name="authors" list={ListGuesser} />
</Admin>
);
```
58 changes: 58 additions & 0 deletions packages/ra-auth-cognito-language-english/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "ra-auth-cognito-language-english",
"version": "1.1.0",
"repository": "[email protected]:marmelab/ra-auth-cognito.git",
"author": "Guillaume Pierson <[email protected]> (https://marmelab.com/)",
"license": "MIT",
"files": [
"*.md",
"lib",
"esm",
"src"
],
"main": "lib/index",
"module": "esm/index.js",
"types": "esm/index.d.ts",
"sideEffects": false,
"peerDependencies": {
"ra-auth-cognito": "^1.1.0"
},
"scripts": {
"build": "yarn run build-cjs && yarn run build-esm",
"build-cjs": "rimraf ./lib && tsc",
"build-esm": "rimraf ./esm && tsc --outDir esm --module es2015",
"watch": "tsc --outDir esm --module es2015 --watch",
"lint": "eslint --fix ./src",
"test-unit": "jest"
},
"devDependencies": {
"ra-auth-cognito": "workspace:^",
"rimraf": "^5.0.5",
"typescript": "^5.3.3"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "jsdom",
"collectCoverageFrom": [
"**/*.js",
"**/*.ts",
"!__tests__/util.ts",
"!coverage/**",
"!**/node_modules/**"
],
"verbose": true,
"automock": false,
"clearMocks": true,
"testPathIgnorePatterns": [
"/node_modules/"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
}
}
23 changes: 23 additions & 0 deletions packages/ra-auth-cognito-language-english/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { CognitoTranslationMessages } from 'ra-auth-cognito';

export const raAuthCognitoEnglishMessages: CognitoTranslationMessages = {
ra: {
auth: {
cognito: {
mfa: {
totp: {
association_helper_text:
'Enter the verification code generated by your authenticator app to complete the association.',
association_label: 'Verification code',
association_required:
'Two-factor authentication required, please associate a TOTP authenticator app (2FA) to your account by scanning the QR code or entering the secret key.',
copy_totp_url_to_clipboard:
'Copy secret key to clipboard',
label: 'Verification code from your authenticator app',
},
},
require_new_password: 'Please enter a new password',
},
},
},
};
Loading

0 comments on commit e4e73e0

Please sign in to comment.