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

auth.controller.js

Blame
  • Forked from an inaccessible project.
    auth.controller.js 2.87 KiB
    const https = require('https');
    var querystring = require('querystring');
    const url = require('url');
    
    var [dbpassword, dbuser, dbhost, dbdatabase, client_id, client_secret] = require('../secrets.js');
    const scope = "default linkcs:read"
    
    
    function getRedirectURI(){
      return url.format({
        pathname:"https://auth.viarezo.fr/oauth/authorize",
        query: {
           "redirect_uri": "http://leaderboard.viarezo.fr:3000/api/fallback",
           "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: 'http://leaderboard.viarezo.fr:3000/api/fallback',
                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',