Skip to content
Snippets Groups Projects
Select Git revision
  • 2bd4b15e1ad114c0a8b8d89176e4f87f5a71e0c4
  • master default
  • clement
  • fix_requirements
  • new_signup
  • interface_admin
  • hamza
  • dev
  • test
  • melissa
  • context_sheet
  • sorties_new
  • Seon82-patch-2
  • export_bdd
  • refactor/participation-user-link
15 results

dev_aws.py

Blame
  • utils.js 1.11 KiB
    /* eslint no-param-reassign: off, import/prefer-default-export: off */
    const jwt = require('jsonwebtoken');
    const { cert } = require('./config');
    
    const interval = (fn, initialTTL, output = { id: null, clear: () => undefined }) => {
      const timeoutID = setTimeout(async () => {
        let TTL;
        try {
          TTL = fn();
          if (TTL instanceof Promise) {
            TTL = await TTL;
          }
        } catch (error) {
          console.error(error);
          TTL = initialTTL;
        }
        const nextTTL = parseInt(TTL, 10) || initialTTL;
        interval(fn, nextTTL, output);
      }, initialTTL);
      output.id = timeoutID;
      output.clear = () => clearTimeout(timeoutID);
      return output;
    };
    
    const generateRandomString = (length) => {
      let text = '';
      const possible =
        'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-,;:!?./§ù*^$£%µ&"\'(=';
    
      for (let i = 0; i < length; i += 1) {
        text += possible.charAt(Math.floor(Math.random() * possible.length));
      }
    
      return text;
    };
    
    const createSignedJWT = () =>
      jwt.sign({ state: generateRandomString(20) }, cert, { algorithm: 'RS256' });
    
    module.exports = { interval, createSignedJWT };