async function processGetRequest(ctx, req, res, permissions) {
  try {
    // Webtask proxy is luckily dropping x-forwarded-for from client.
    // Otherwise client could easily spoof any IP address.
    const ip = proxyaddr.all(req).slice(-1)[0];

    // Only IPv4 supported for the moment
    if (!isIp.v4(ip)) {
      res.writeHead(400, { 'Content-Type': 'text/plain' });
      res.end("IP from X-Forwarded-For is not a valid IPv4 address: " + ip);
      return;
    }

    // Check if user is authorized
    let user;
    if (ctx.user.sub) user = ctx.user.sub;
    if (ctx.user.name) user = ctx.user.name;
    if (ctx.user.email) user = ctx.user.email;
    if (!user) throw new Error("No user identification found!");
    if ( (ctx.user.email_verified && (ctx.user.email_verified !== true )) || (permissions.authorized(user) !== true)) {
      res.writeHead(401, { 'Content-Type': 'text/plain' });
      res.end("User " + user + " is not authorized!");
      return;
    }

    // Authorize current IP
    const result = await permissions.authorize(ip);

    // Revoke any expired permissions in storage
    await permissions.revokeExpired().catch((error) => { console.log(error); });

    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end(`
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Permit Current IP</title>
</head>
<body>
User: ${user}<br>
IP: ${ip}<br>
IP Permission Changes:<br>
<ul>
${result.map(item => `<li>${item}</li>`).join("\n")}
</ul>
</body>
</html>`);
  } catch (error) {
    console.log(error);
    res.writeHead(500, { 'Content-Type': 'text/plain' });
    res.end((error instanceof Error) ? error.stack : JSON.stringify(error, null, 2));
  }
}
/**
 * GET -> User opened the page. So add permissions.
 * Goes over all defined permissions, check if permission is already applied for current IP and apply if needed.
 * 
 * Also removes expired permissions if there are any.
 */
> User opened the page. So add permissions.
Goes over all defined permissions, check if permission is already applied for current IP and apply if needed.

Also removes expired permissions if there are any.
> User opened the page . So add permissions . Goes over all defined permissions check if permission is already applied for current IP and apply if needed . Also removes expired permissions if there are any .
async function processGetRequest(ctx, req, res, permissions) {
  try {
    const ip = proxyaddr.all(req).slice(-1)[0];
    if (!isIp.v4(ip)) {
      res.writeHead(400, { 'Content-Type': 'text/plain' });
      res.end("IP from X-Forwarded-For is not a valid IPv4 address: " + ip);
      return;
    }
    let user;
    if (ctx.user.sub) user = ctx.user.sub;
    if (ctx.user.name) user = ctx.user.name;
    if (ctx.user.email) user = ctx.user.email;
    if (!user) throw new Error("No user identification found!");
    if ( (ctx.user.email_verified && (ctx.user.email_verified !== true )) || (permissions.authorized(user) !== true)) {
      res.writeHead(401, { 'Content-Type': 'text/plain' });
      res.end("User " + user + " is not authorized!");
      return;
    }
    const result = await permissions.authorize(ip);
    await permissions.revokeExpired().catch((error) => { console.log(error); });
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end(`
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Permit Current IP</title>
</head>
<body>
User: ${user}<br>
IP: ${ip}<br>
IP Permission Changes:<br>
<ul>
${result.map(item => `<li>${item}</li>`).join("\n")}
</ul>
</body>
</html>`);
  } catch (error) {
    console.log(error);
    res.writeHead(500, { 'Content-Type': 'text/plain' });
    res.end((error instanceof Error) ? error.stack : JSON.stringify(error, null, 2));
  }
}
async function processGetRequest ( ctx , req , res , permissions ) { try { const ip = proxyaddr . all ( req ) . slice ( - 1 ) [ 0 ] ; if ( ! isIp . v4 ( ip ) ) { res . writeHead ( 400 , { 'Content-Type' : 'text/plain' } ) ; res . end ( "IP from X-Forwarded-For is not a valid IPv4 address: " + ip ) ; return ; } let user ; if ( ctx . user . sub ) user = ctx . user . sub ; if ( ctx . user . name ) user = ctx . user . name ; if ( ctx . user . email ) user = ctx . user . email ; if ( ! user ) throw new Error ( "No user identification found!" ) ; if ( ( ctx . user . email_verified && ( ctx . user . email_verified !== true ) ) || ( permissions . authorized ( user ) !== true ) ) { res . writeHead ( 401 , { 'Content-Type' : 'text/plain' } ) ; res . end ( "User " + user + " is not authorized!" ) ; return ; } const result = await permissions . authorize ( ip ) ; await permissions . revokeExpired ( ) . catch ( ( error ) => { console . log ( error ) ; } ) ; res . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ; res . end ( ` ${ user } ${ ip } ${ result . map ( item => ` ${ item } ` ) . join ( "\n" ) } ` ) ; } catch ( error ) { console . log ( error ) ; res . writeHead ( 500 , { 'Content-Type' : 'text/plain' } ) ; res . end ( ( error instanceof Error ) ? error . stack : JSON . stringify ( error , null , 2 ) ) ; } }
GET -> User opened the page.
GET - > User opened the page .
// Webtask proxy is luckily dropping x-forwarded-for from client. // Otherwise client could easily spoof any IP address. // Only IPv4 supported for the moment // Check if user is authorized // Authorize current IP // Revoke any expired permissions in storage