Skip to content

Commit

Permalink
feat: working redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastia committed Jun 8, 2023
1 parent c30016b commit a7c4790
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ async def root():
return {"message": "Hello World"}

@app.get("/verify_page", response_class=HTMLResponse)
async def verify_page(request: Request, email_address: str = "missing", auth_provider_uuid: str = "", redirect_url="test redirect"):
async def verify_page(request: Request, email_address: str = "missing",route_prefix: str = "", auth_provider_uuid: str = "", redirect_url="test redirect"):
return templates.TemplateResponse("verify.html",
{"request": request,
"email_address": email_address,
"redirect_url": redirect_url,
"auth_provider_uuid": auth_provider_uuid,
"route_prefix": "",
"route_prefix": route_prefix,
"validation_failed": False})


Expand Down
5 changes: 4 additions & 1 deletion api/templates/verify.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
</head>
<body>
<header class="bcgov-header">
<img src="static/bcgov_logo.svg" style="height: 4em; margin-top: 0.5em" />
<img
src="{{ route_prefix }}/static/bcgov_logo.svg"
style="height: 4em; margin-top: 0.5em"
/>
</header>
<div class="w3-half w3-display-middle">
<div class="w3-card-4 w3-container">
Expand Down
46 changes: 25 additions & 21 deletions middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const express = require("express");
const request = require("request");

const defaultConfig = {
everifyHost: "localhost:8000",
everifyHost: "http://localhost:8000",
routePrefix: "/everify",
};

const host = process.env.HOST || "http://localhost:3004";

function getMiddleware(configuration) {
const config = configuration || defaultConfig;

Expand All @@ -17,6 +19,10 @@ function getMiddleware(configuration) {
console.log("Verify!");
});

internalRouter.get("/verify_page", (req, res, next) => {
return "a";
});

// Handle static assets for verify page
internalRouter.get("/static/:filename", (req, res) => {
request({
Expand All @@ -29,14 +35,11 @@ function getMiddleware(configuration) {
router.use(config.routePrefix, internalRouter);

router.use(async (req, res, next) => {
if (!req.claims) next();
if (!req.claims) return next();

const isVerifiedUrl = new URL(`${config.everifyHost}/is_verified`);
isVerifiedUrl.searchParams.append(
"email_address",
req.claims.email_address
);
isVerifiedUrl.searchParams.append("auth_provider_uuid", req.claims.uuid);
isVerifiedUrl.searchParams.append("email_address", req.claims.email);
isVerifiedUrl.searchParams.append("auth_provider_uuid", req.claims.sub);

const isVerifiedResponse = await fetch(isVerifiedUrl);

Expand All @@ -51,25 +54,26 @@ function getMiddleware(configuration) {
const sendEmailUrl = new URL(`${config.everifyHost}/create_otp`);
const sendEmailResponse = await fetch(sendEmailUrl, {
method: "POST",
body: {
email_address: req.claims.email_address,
auth_provider_uuid: req.claims.uuid,
},
body: JSON.stringify({
email_address: req.claims.email,
auth_provider_uuid: req.claims.sub,
}),
headers: { "Content-Type": "application/json" },
});
if (!sendEmailResponse.ok)
throw new Error("error calling send email endpoint");
throw new Error(
"error calling send email endpoint " + sendEmailResponse.error
);

// Show the user the OTP page
const verifyPageUrl = new URL(`${config.everifyHost}/is_verified`);
verifyPageUrl.searchParams.append("route_prefix", config.route_prefix);
verifyPageUrl.searchParams.append(
"email_address",
req.claims.email_address
);
verifyPageUrl.searchParams.append("auth_provider_uuid", req.claims.uuid);
const verifyPageUrl = new URL(`${config.everifyHost}/verify_page`);
verifyPageUrl.searchParams.append("route_prefix", config.routePrefix);
verifyPageUrl.searchParams.append("email_address", req.claims.email);
verifyPageUrl.searchParams.append("auth_provider_uuid", req.claims.sub);

//const verifyPageResponse = await fetch(verifyPageUrl);
request({
url: verifyPageUrl,
uri: verifyPageUrl,
}).pipe(res);
});

Expand All @@ -79,4 +83,4 @@ function getMiddleware(configuration) {
module.exports = { getMiddleware, defaultConfig };

/// consumer would do
// app.use(package.getMiddleware( {..optional config..} ))
// app.use('/everify', getMiddleware())

0 comments on commit a7c4790

Please sign in to comment.