Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const axios = require('axios');
  3. const rp = require('request-promise');
  4. const simpleOAuth = require('simple-oauth2');
  5. const qs = require('querystring');
  6. const {id, secret} = require('../src/config.json');
  7.  
  8. const {Builder, By, Key, until} = require('selenium-webdriver');
  9. require('chromedriver');
  10.  
  11. class OAuth2 {
  12.  
  13.     constructor() {
  14.         this.app = express();
  15.         this.port = 3000;
  16.         this.tokenHost = 'https://oauth.lichess.org';
  17.         this.tokenPath = '/oauth';
  18.         this.authorizePath = '/oauth/authorize';
  19.         this.redirectUri = "http://localhost:3000/callback";
  20.     }
  21.  
  22.     /**
  23.      * List of scopes {"https://lichess.org/api#section/Authentication"}
  24.      */
  25.     get scopes () {
  26.         if (this._scopes) return this._scopes;
  27.         return this._scopes = [
  28.             'game:read',
  29.             'preference:read', //- Read your preferences
  30.             'preference:write', //- Write your preferences
  31.             'email:read', //- Read your email address
  32.             'challenge:read', //- Read incoming challenges
  33.             'challenge:write.', //- Create, accept, decline challenges
  34.             'tournament:write' //- Create tournaments
  35.         ];
  36.     }
  37.  
  38.     get state () {
  39.         if (this._state) return this._state;
  40.         return this._state = Math.random().toString(36).substring(2);
  41.     }
  42.  
  43.     get authorizationUri () {
  44.         if (this._authorizationUri) return this._authorizationUri;
  45.         return this._authorizationUri = this.tokenHost + this.authorizePath + '?' + qs.stringify({
  46.             response_type: "code",
  47.             client_id: id,
  48.             redirect_uri: this.redirectUri,
  49.             scope: this.scopes.join('%20'),
  50.             state: this.state
  51.         });
  52.     }
  53.  
  54.     get oauth2 () {
  55.         if (this._oauth2) return this._oauth2;
  56.         return this._oauth2 = simpleOAuth.create({
  57.             client: {
  58.                 id,
  59.                 secret,
  60.             },
  61.             auth: {
  62.                 tokenHost: this.tokenHost,
  63.                 tokenPath: this.tokenPath,
  64.                 authorizePath: this.authorizePath,
  65.             }
  66.         });
  67.     }
  68.  
  69.     set() {
  70.         this.app.get('/', (req, res) => res.send('Hello<br><a href="/auth">Log in with lichess</a>'));
  71.         this.app.get('/auth', (req, res) => {
  72.             console.log(this.authorizationUri);
  73.             res.redirect(this.authorizationUri);
  74.         });
  75.         this.app.get('/callback', async (req, res) => {
  76.             try {
  77.                 const result = await this.oauth2.authorizationCode.getToken({
  78.                     "code": req.query.code,
  79.                     "redirect_uri": this.redirectUri
  80.                 });
  81.                 const token = this.oauth2.accessToken.create(result);
  82.                 console.log(token);                
  83.                 const userInfo = await OAuth2.getUserInfo(token.token);
  84.                 res.send(`<h1>Success!</h1>Your lichess user info: <pre>${JSON.stringify(userInfo.data)}</pre>`);
  85.             } catch (e) {
  86.                 if (e) console.error(e);
  87.             }
  88.         });
  89.     }
  90.  
  91.     listen() {
  92.         this.app.listen(this.port, () => console.log(`App listening on port ${this.port}!`));
  93.     }
  94.  
  95.     async launch() {
  96.         try {
  97.             let driver = new Builder().forBrowser('chrome').build();
  98.             await driver.get(`http://localhost:${this.port}/`);
  99.         } catch (e) {
  100.             if (e) console.error(e);
  101.         }
  102.     }
  103.  
  104.     static async getUserInfo(token) {
  105.         return await rp.get({
  106.             "uri": 'https://lichess.org/api/account',
  107.             "json": true,
  108.             "headers": {
  109.                 Accept: 'application/json',
  110.                 Authorization: 'Bearer ' + token.access_token
  111.             }
  112.         })
  113.         return await axios.get('/api/account', {
  114.             "baseURL": 'https://lichess.org/',
  115.             "headers": {
  116.                 'Authorization': 'Bearer ' + token.access_token
  117.             }
  118.         });
  119.     }
  120.  
  121. }
  122.  
  123. let r = new OAuth2();
  124. r.set();
  125. r.listen();
  126. r.launch();
  127.  
  128. module.exports = OAuth2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement