Skip to content
Snippets Groups Projects
Select Git revision
  • 8de95ead283ec6f6f43dc26a722cbf28c22a6f3a
  • master default
  • dockerization
  • staging
  • backup-before-cleaning-repo
  • dockerfiles-pour-maelle
6 results

auth.controller.js

Blame
  • Forked from an inaccessible project.
    user avatar
    Fabien Zucchet authored
    8de95ead
    History
    auth.controller.js 2.85 KiB
    const https = require('https');
    var querystring = require('querystring');
    const url = require('url');
    
    var secrets = require('../secrets.js');
    var client_id = secrets.client_id;
    var client_secret = secrets.client_secret;
    
    const scope = "default linkcs:read"
    
    
    function getRedirectURI(){
      return url.format({
        pathname:"https://auth.viarezo.fr/oauth/authorize",
        query: {
           "redirect_uri": process.env.PROD_REDIRECT_URI,
           "client_id": client_id,
           "response_type": "code",
           "state": "aaa", // Generate a random here
           "scope": scope
         }
      });
    }
    
    async function getToken(code){
        return new Promise ((resolve, reject) => {
            const data = querystring.stringify({
                grant_type: 'authorization_code',
                code: code,
                redirect_uri: process.env.PROD_REDIRECT_URI,
                client_id: client_id,
                client_secret: client_secret
            })
    
            const options = {
                hostname: 'auth.viarezo.fr',
                port: 443,
                path: '/oauth/token',
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'Content-Length': data.length
                }
            }
    
            let req = https.request(options, res => {
              if(res.statusCode != 200){
                reject("Token Failed");
              }
    
              var body = '';
              res.on('data', function (chunk) {
                body += chunk;
              });
              res.on('end', function () {
                resolve(JSON.parse(body));
              });
    
            })
    
            req.on('error', err => {
              reject(err);
            });
            req.write(data);
            req.end();
          });
    }
    
    async function getInfos(token){
      return new Promise ((resolve, reject) => {
      //https://auth.viarezo.fr/api/user/show/me
        const options = {
          hostname: 'auth.viarezo.fr',
          port: 443,
          path: '/api/user/show/me',
          method: 'GET',
          headers: {
              'Authorization': 'Bearer ' + token.access_token
          }
        }
    
        let req = https.request(options, res => {
          if(res.statusCode != 200){
            reject("Data fetching Failed");
          }
    
          var body = '';
          res.on('data', function (chunk) {
            body += chunk;
          });
          res.on('end', function () {
            var data = JSON.parse(body);
            resolve({
              id : data.id,
              login: data.login,
              firstName: data.firstName,
              lastName: data.lastName,
              email: data.email,
              promo: data.promo
             });
          });
    
        })
    
        req.on('error', err => {
          reject(err);
        });
        req.end();
      });
    }
    
    function authMiddleware(req, res, next) {
      if ('login' in req.session.ids){
        return next();
      }
      res.redirect('/');
    }
    
    exports.getRedirectURI = getRedirectURI;
    exports.getToken =  getToken;
    exports.getInfos =  getInfos;
    exports.authMiddleware = authMiddleware;