Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const simpleOauth = require('simple-oauth2');
  3. const axios = require('axios');
  4. const path = require('path');
  5. const DataManager = require(path.join(__dirname, '..', '..', '..', 'util', 'datamanager'));
  6. const config = DataManager.getFile(path.join(__dirname, '..', '..', '..', 'config.json'));
  7. //const Token = DataManager.getFile('./token.json');
  8.  
  9. /* Create your lichess OAuth app on https://lichess.org/account/oauth/app/create
  10.  * Homepage URL: http://localhost:8087
  11.  * Callback URL: http://localhost:8087/callback
  12.  */
  13.  
  14. /* --- Fill in your app config here --- */
  15. const port = 3000;
  16. const clientId = config.ids.lichess;
  17. //const clientSecret = '';
  18. const redirectUri = encodeURIComponent(`http://lazybot.co.uk/callback`);
  19. // uncomment the scopes you need
  20. // list of scopes: https://lichess.org/api#section/Authentication
  21. const scopes = [
  22.     // 'game:read',
  23.     // 'preference:read',
  24.     // 'preference:write',
  25. ];
  26. /* --- End of your app config --- */
  27.  
  28. /* --- Lichess config --- */
  29. const tokenHost = 'https://oauth.lichess.org';
  30. const authorizePath = '/oauth/authorize';
  31. const tokenPath = '/oauth';
  32. /* --- End of lichess config --- */
  33.  
  34. const oauth2 = simpleOauth.create({
  35.     client: {
  36.         id: clientId,
  37.         // secret: clientSecret,
  38.     },
  39.     auth: {
  40.         tokenHost,
  41.         tokenPath,
  42.         authorizePath,
  43.     },
  44. });
  45.  
  46. const state = Math.random().toString(36).substring(2);
  47. const authorizationUri = `${tokenHost}${authorizePath}?response_type=code&client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scopes.join('%20')}&state=${state}`;
  48.  
  49. const app = express();
  50.  
  51. // Show the "log in with lichess" button
  52. app.get('/', (req, res) => res.send('Hello<br><a href="/auth">Log in with lichess</a>'));
  53.  
  54. // Initial page redirecting to Lichess
  55. app.get('/auth', (req, res) => {
  56.     console.log(authorizationUri);
  57.     res.redirect(authorizationUri);
  58. });
  59.  
  60. // Redirect URI: parse the authorization token and ask for the access token
  61. app.get('/callback', async (req, res) => {
  62.     try {
  63.         const result = await oauth2.authorizationCode.getToken({
  64.             code: req.query.code,
  65.             redirect_uri: redirectUri
  66.         });
  67.         console.log(result);
  68.         const token = oauth2.accessToken.create(result);
  69.         const userInfo = await getUserInfo(token.token);
  70.         res.send(`<h1>Success!</h1>Your lichess user info: <pre>${JSON.stringify(userInfo.data)}</pre>`);
  71.     } catch(error) {
  72.         console.error('Access Token Error', error.message);
  73.         res.status(500).json('Authentication failed');
  74.     }
  75. });
  76.  
  77. app.listen(port, () => console.log(`Express server started on port ${port}`));
  78.  
  79. function getUserInfo(token) {
  80.     return axios.get('/api/account', {
  81.         baseURL: 'https://lichess.org/',
  82.         headers: { Authorization: 'Bearer ' + token.access_token }
  83.     });
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement