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

Make sure error message shows in image captcha widget #1430

Merged
merged 34 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7541290
Make sure error message shows
forgetso Oct 3, 2024
b7bb2ea
Make sure onFailed callback can be set by users
forgetso Oct 3, 2024
44c951a
Extend correct captcha tests to bundle demo
forgetso Oct 7, 2024
203a14c
lint-fix
forgetso Oct 7, 2024
b9a6115
add register site key function to setup
forgetso Oct 7, 2024
47fa859
Merge branch 'staging' into fix-site-key-error-image-captcha
forgetso Oct 7, 2024
8ab8d78
fix cypress-shared build
forgetso Oct 7, 2024
c09a958
Merge branch 'main' into fix-site-key-error-image-captcha
forgetso Oct 7, 2024
1cbc15f
Remove the old user verify endpoint
forgetso Oct 7, 2024
80b24a3
Merge branch 'staging' into fix-site-key-error-image-captcha
forgetso Oct 7, 2024
7155787
Add a unit test to ensure the callbacks are picked up from the window…
forgetso Oct 8, 2024
1557465
Merge branch 'staging' into fix-site-key-error-image-captcha
forgetso Oct 8, 2024
ae74cb3
Add reset function and onReset callback
forgetso Oct 8, 2024
70874d8
lint-fix
forgetso Oct 8, 2024
b55ab25
Fire onFailed properly
forgetso Oct 8, 2024
7ea5e6d
Change error returned by authMiddleware
forgetso Oct 8, 2024
88258a6
Merge branch 'staging' into fix-site-key-error-image-captcha
forgetso Oct 8, 2024
aa9ee80
Provide properly formatted errors to the frontend
forgetso Oct 8, 2024
feab450
Nicer error handling in the API
forgetso Oct 9, 2024
65834a9
Standardise the types of API JSON errors we're passing only throw in …
forgetso Oct 9, 2024
0f9ac41
Set up env for bundle cypress tests
forgetso Oct 9, 2024
0bbe137
Fix provider integration tests
forgetso Oct 9, 2024
3680fbc
Don't allow invalid vite modes
forgetso Oct 9, 2024
0624b0c
Add PROSOPO_SERVER_URL for cypress tests
forgetso Oct 9, 2024
cc15675
Make sure other NODE_ENVs don't leak into vite config
forgetso Oct 9, 2024
d00f083
Merge branch 'staging' into fix-site-key-error-image-captcha
forgetso Oct 9, 2024
a2af811
Merge branch 'staging' into fix-site-key-error-image-captcha
forgetso Oct 10, 2024
a3ccf3f
Merge branch 'main' into fix-site-key-error-image-captcha
forgetso Oct 10, 2024
11cc587
📦🔒
forgetso Oct 10, 2024
5741da9
📦🔒
forgetso Oct 10, 2024
cfe03b6
bump dotenv package
forgetso Oct 10, 2024
d136079
📦🔒
forgetso Oct 10, 2024
3a09987
Sort out the various errors
forgetso Oct 10, 2024
deb6b20
Merge branch 'staging' into fix-site-key-error-image-captcha
forgetso Oct 10, 2024
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
3 changes: 2 additions & 1 deletion .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: cypress
on:
pull_request:
branches: [main, dev, staging, release/*]
types:
types:
- opened # when a PR is opened
- synchronize # when a PR is pushed to
- reopened # when a PR is reopened
Expand Down Expand Up @@ -106,6 +106,7 @@ jobs:

cp demos/client-example-server/env.development demos/client-example-server/.env.test
cp demos/client-example/env.development demos/client-example/.env.test
cp demos/client-bundle-example/env.development demos/client-example/.env.test
cp dev/scripts/env.test .env.test
cp dev/scripts/env.test dev/scripts/.env.test
cp dev/scripts/env.test packages/cli/.env.test
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ jobs:

cp demos/client-example-server/env.development demos/client-example-server/.env.test
cp demos/client-example/env.development demos/client-example/.env.test
cp demos/client-bundle-example/env.development demos/client-bundle-example/.env.test
cp dev/scripts/env.test .env.test
cp dev/scripts/env.test dev/scripts/.env.test
cp dev/scripts/env.test packages/cli/.env.test
Expand Down
2 changes: 1 addition & 1 deletion demos/client-bundle-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ docker compose --file ./docker/docker-compose.development.yml up -d && \
npm i && \
npm run build:all && \
npm run setup:all && \
NODE_ENV=production npm -w @prosopo/procaptcha-bundle run bundle && \
NODE_ENV=development npm -w @prosopo/procaptcha-bundle run bundle && \
npm run start:all
```

Expand Down
2 changes: 2 additions & 0 deletions demos/client-bundle-example/env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PROSOPO_SITE_KEY=5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw
PROSOPO_SERVER_URL=http://localhost:9228
2 changes: 2 additions & 0 deletions demos/client-bundle-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@prosopo/client-bundle-example",
"main": "index.js",
"type": "module",
"engines": {
"node": ">=20",
"npm": ">=9"
Expand All @@ -14,6 +15,7 @@
},
"version": "2.1.3",
"devDependencies": {
"@prosopo/dotenv": "2.1.3",
"@types/node": "22.5.5",
"@vitest/coverage-v8": "2.1.1",
"concurrently": "9.0.1",
Expand Down
2 changes: 1 addition & 1 deletion demos/client-bundle-example/src/frictionless.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Get the Element using elementId
const captchaContainer = document.getElementById('procaptcha-container')
render(captchaContainer, {
siteKey: '5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw',
siteKey: import.meta.env.PROSOPO_SITE_KEY,
theme: 'dark',
callback: onCaptchaVerified,
captchaType: 'frictionless',
Expand Down
209 changes: 162 additions & 47 deletions demos/client-bundle-example/src/index.html
Original file line number Diff line number Diff line change
@@ -1,53 +1,168 @@
<!doctype html>
<html lang="en">
<head>
<link href="//cdn.muicss.com/mui-0.10.3/css/mui.min.css" rel="stylesheet" type="text/css" />
<title>Procaptcha demo: Simple page</title>
<script id="procaptchaScript" type="module" src="./assets/procaptcha.bundle.js" async defer></script>
</head>
<body>
<div class="mui-container">
<form action="?" class="mui-form">
<legend>Login</legend>
<div class="mui-textfield mui-textfield--float-label">
<label>Email Address</label>
<input type="email" required />
</div>
<div class="mui-textfield mui-textfield--float-label">
<label>Password</label>
<input type="password" required />
</div>
<div class="mui-textfield mui-textfield--float-label">
<div id="procaptcha-container"></div>
</div>
<div class="mui-textfield mui-textfield--float-label">
<!-- Dev sitekey -->
<div
class="procaptcha"
data-theme="light"
data-sitekey="5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw"
></div>
</div>
<input type="submit" class="mui-btn mui-btn--raised" />
</form>
</div>
<script type="module">
// Pattern to avoid race condition between Procaptcha script loading and Procaptcha render function call
import { render } from './assets/procaptcha.bundle.js'
<head>
<link href="https://cdn.muicss.com/mui-0.10.3/css/mui.min.css" rel="stylesheet" type="text/css"/>
<title>Procaptcha demo: Simple page</title>
<script id="procaptchaScript" type="module" src="./assets/procaptcha.bundle.js" async defer></script>
<script type="text/javascript">
window.onCaptchaFailed = function () {
console.log('The user failed the captcha')
}
</script>
<script type="module">
window.addEventListener('load', () => {

window.setIsError = (isError) => {
const messageContainer = document.getElementById('messageContainer');
messageContainer.style.color = isError ? 'red' : 'black';
messageContainer.style.display = 'block';
};

window.setMessage = (message) => {
document.getElementById('message').innerText = message;
};

window.onLoggedIn = (token) => {
const url = new URL("/private", config.serverUrl).href;
console.log("getting private resource with token ", token, "at", url);
fetch(url, {
method: "GET",
headers: {
Origin: "http://localhost:9232", // TODO: change this to env var
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
})
.then(async (res) => {
try {
const jsonRes = await res.json();
if (res.status === 200) {
setMessage(jsonRes.message);
}
} catch (err) {
console.log(err);
}
})
.catch((err) => {
console.log(err);
});
};

window.onActionHandler = () => {

const procaptchaElements = document.getElementsByName('procaptcha-response');

if (!procaptchaElements.length) {
alert("Must complete captcha");
return
}

const procaptchaToken = procaptchaElements[0].value;

// Define a callback function to be called when the CAPTCHA is verified
function onCaptchaVerified(output) {
console.log('Captcha verified, output: ' + JSON.stringify(output))
const payload = {
email: document.getElementById('email').value,
name: document.getElementById('name').value,
password: document.getElementById('password').value,
"procaptcha-response": procaptchaToken,
};
const url = new URL('signup', import.meta.env.PROSOPO_SERVER_URL).href;
console.log("posting to", url, "with payload", payload);
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
contentType: "application/json",
}).then((response) => {
return new Promise((resolve) => response.json()
.then((json) => resolve({
status: response.status,
ok: response.ok,
json,
})))
})
.then(async ({status, json, ok}) => {
console.log("status", status, "json", json.message, "ok", ok);
console.log("json", json)
try {
if (status !== 200) {
setIsError(true);
setMessage(json.message);
} else {
setIsError(false);
setMessage(json.message);
}
} catch (err) {
console.log(err);
}
})
.catch((err) => {
console.log(err);
setIsError(true);
setMessage("Error: " + err);
});
};
console.log("here")
}
)
;
</script>

</head>
<body>

<div class="mui-container">
<form action="%PROSOPO_SERVER_URL%/signup" method="POST" class="mui-form">
<h1>Example Login Form</h1>
<div class="mui-textfield mui-textfield--float-label">
<label for="name">Name</label>
<input id="name" type="text" name="name" required/>
</div>
<div class="mui-textfield mui-textfield--float-label">
<label for="email">Email Address</label>
<input id="email" type="email" name="email" required/>
</div>
<div class="mui-textfield mui-textfield--float-label">
<label for="password">Password</label>
<input id="password" type="password" name="password" required/>
</div>
<div class="mui-textfield mui-textfield--float-label">
<div id="procaptcha-container"></div>
</div>
<div class="mui-textfield mui-textfield--float-label">
<!-- Dev sitekey -->
<div
class="procaptcha"
data-theme="light"
data-sitekey="%PROSOPO_SITE_KEY%"
data-failed-callback="onCaptchaFailed"
></div>
</div>
<div id="messageContainer" style="display: none; color: black;"><span id="message"></span></div>
<button type="button" class="mui-btn mui-btn--raised" onclick="onActionHandler()">Submit</button>
</form>
</div>
<script type="module">
// Pattern to avoid race condition between Procaptcha script loading and Procaptcha render function call
import {render} from './assets/procaptcha.bundle.js'

// Define a callback function to be called when the CAPTCHA is verified
function onCaptchaVerified(output) {
console.log('Captcha verified, output: ' + JSON.stringify(output))
}

// Get the Element using elementId
const captchaContainer = document.getElementById('procaptcha-container')
// Render the CAPTCHA explicitly on a container with id "procaptcha-container"
render(captchaContainer, {
siteKey: '5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw',
theme: 'dark',
callback: onCaptchaVerified,
})
</script>
</body>
// Get the Element using elementId
const captchaContainer = document.getElementById('procaptcha-container')
// Render the CAPTCHA explicitly on a container with id "procaptcha-container"
render(captchaContainer, {
siteKey: import.meta.env.PROSOPO_SITE_KEY,
theme: 'dark',
callback: onCaptchaVerified,
"failed-callback": function () {
console.log('The user failed the captcha')
}
})
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion demos/client-bundle-example/src/jsBundleTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div
class="procaptcha"
data-theme="light"
data-sitekey="5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw"
data-sitekey="%PROSOPO_SITE_KEY%"
></div>
</form>
</body>
Expand Down
2 changes: 1 addition & 1 deletion demos/client-bundle-example/src/urlParams.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Get the Element using elementId
const captchaContainer = document.getElementById('procaptcha-container')
render(captchaContainer, {
siteKey: '5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw',
siteKey: import.meta.env.PROSOPO_SITE_KEY,
theme: 'dark',
callback: onCaptchaVerified,
})
Expand Down
6 changes: 5 additions & 1 deletion demos/client-bundle-example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
"sourceMap": false
},
"include": ["**/*.ts"],
"references": []
"references": [
{
"path": "../../packages/dotenv"
}
]
}
32 changes: 23 additions & 9 deletions demos/client-bundle-example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,26 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export default {
watch: false,
mode: "development",
server: {
https: false,
host: true,
cors: true,
},
};

import { loadEnv } from "@prosopo/dotenv";
import { type UserConfig, defineConfig } from "vite";

export default defineConfig(({ command, mode }) => {
loadEnv();
return {
watch: false,
mode: "development",
server: {
host: true,
cors: true,
},
define: {
"import.meta.env.PROSOPO_SITE_KEY": JSON.stringify(
process.env.PROSOPO_SITE_KEY,
),
"import.meta.env.PROSOPO_SERVER_URL": JSON.stringify(
process.env.PROSOPO_SERVER_URL,
),
},
} as UserConfig;
});
15 changes: 6 additions & 9 deletions demos/client-example-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,22 @@
"include": ["src", "src/**/*.json"],
"references": [
{
"path": "../../packages/contract"
},
{
"path": "../../packages/server"
"path": "../../packages/common"
},
{
"path": "../../packages/types"
"path": "../../packages/contract"
},
{
"path": "../../packages/common"
"path": "../../packages/dotenv"
},
{
"path": "../../packages/util"
"path": "../../packages/server"
},
{
"path": "../../packages/server"
"path": "../../packages/types"
},
{
"path": "../../packages/dotenv"
"path": "../../packages/util"
}
]
}
Loading