{"size": 2083, "ext": "js", "max_stars_repo_path": "api/oauth.js", "max_stars_repo_name": "fluxo-app/flux-api", "max_stars_repo_stars_event_min_datetime": "2017-10-12T15:14:37.000Z", "max_stars_repo_stars_event_max_datetime": "2017-10-12T15:14:37.000Z", "max_issues_repo_path": "api/oauth.js", "max_issues_repo_name": "fluxo-app/flux-api", "max_issues_repo_issues_event_min_datetime": "2021-05-11T04:33:32.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-11T04:33:32.000Z", "max_forks_repo_path": "api/oauth.js", "max_forks_repo_name": "fluxo-app/fluxo-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 32.0461538462, "max_line_length": 146, "alphanum_fraction": 0.6773883821}
const express = require('express')
const router = express.Router()
const settings = require('../settings')
const NodeCache = require("node-cache")
const secretsCache = new NodeCache({stdTTL: 300, checkperiod: 30})
const jwt = require('jsonwebtoken')


router.get('/login', (req, res) => {
  const redirectUrl = req.query.redirectUrl
  const callbackUrl = `${settings.appUrl}/oauth/callback?redirectUrl=${redirectUrl}`
  settings.oauth._authorize_callback = callbackUrl

  settings.oauth.getOAuthRequestToken((error, token, tokenSecret, response) => {
    if (error) {
      return res.status(error.statusCode).send({message:error.data})
    }

    secretsCache.set(token, `${tokenSecret}`)

    res.writeHead(302, {'Location': `${settings.authorizeURL}?oauth_token=${token}&name=${settings.appName}`})
    res.end()
  })
})

router.get('/callback', (req, res) => {
  const token = req.query.oauth_token
  const redirectUrl = req.query.redirectUrl

  if (!token) {
    res.writeHead(302, {'Location': `${redirectUrl}?error=${'no oauth token'}`})
    return res.end()
    return res.status(401).send({message:'no oauth token'})
  }

  const tokenSecret = secretsCache.get(token)
  const verifier = req.query.oauth_verifier

  settings.oauth.getOAuthAccessToken(token, tokenSecret, verifier, (error, accessToken, accessTokenSecret, response) => {
    if (error) {
      return res.status(error.statusCode).send({message:error.data})
    }

    settings.oauth.getProtectedResource("https://api.trello.com/1/members/me", "GET", accessToken, accessTokenSecret, (error, data, response) => {
      if (error) {
        return res.status(error.statusCode).send({message:error.data})
      }

      const json = JSON.parse(data)
      const payload = {
        name: json.fullName,
        accessToken: accessToken,
        accessTokenSecret: accessTokenSecret,
      }

      const jsonWebToken = jwt.sign(payload, settings.appSecret, { expiresIn: '30d' });

      res.writeHead(302, {'Location': `${redirectUrl}?jwt=${jsonWebToken}`})
      res.end()
    })
  })
})

module.exports = router
