Last active
December 20, 2016 13:46
-
-
Save solidgoldpig/c66af9cbe30081a1546072b25eb26ac9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// const yargs = require('yargs') | |
const express = require('express') | |
const request = require('request') | |
const uuid = require('node-uuid') | |
const querystring = require('querystring') | |
const opener = require('opener') | |
let argv = require('yargs') | |
.option('client_id', { | |
alias: 'id', | |
type: 'string', | |
required: true | |
}) | |
.option('client_secret', { | |
alias: 'secret', | |
type: 'string', | |
required: true | |
}) | |
.option('auth_url', { | |
alias: 'auth', | |
type: 'string', | |
default: 'auth.getmondo.co.uk' | |
}) | |
.option('api_domain', { | |
alias: 'api', | |
type: 'string', | |
default: 'api.monzo.com' | |
}) | |
.option('state', { | |
type: 'string' | |
}) | |
.option('port', { | |
alias: 'p', | |
type: 'number', | |
default: 0 | |
}) | |
.option('exit', { | |
alias: 'x', | |
type: 'boolean', | |
default: true | |
}) | |
.argv | |
const clientId = argv.client_id | |
const clientSecret = argv.client_secret | |
const authUrl = `https://${argv.auth_url}` | |
const apiDomain = `https://${argv.api_domain}` | |
const apiTokenUrl = '/oauth2/token' | |
const app = express() | |
const server = require('http').createServer(app) | |
const exitImmediately = argv.exit | |
let port = argv.port | |
let state = argv.state | |
function getRootAddress (url) { | |
url = url || '' | |
return `http://localhost:${port}${url}` | |
} | |
function getAuthAddress () { | |
return getRootAddress('/monzo') | |
} | |
app.use('/monzo', (req, res) => { | |
if (req.query.state !== state) { | |
res.json({ | |
error: 'State does not match', | |
state: state, | |
query: req.query | |
}) | |
} | |
request({ | |
uri: `${apiDomain}${apiTokenUrl}`, | |
method: 'POST', | |
json: true, | |
form: { | |
grant_type: 'authorization_code', | |
client_id: clientId, | |
client_secret: clientSecret, | |
redirect_uri: getAuthAddress(), | |
code: req.query.code | |
} | |
}, (err, data) => { | |
if (err) { | |
res.status(500) | |
res.json(err) | |
} | |
if (data) { | |
res.json(data.body) | |
} | |
if (exitImmediately) { | |
process.exit() | |
} | |
}) | |
}) | |
app.use('/', (req, res) => { | |
state = req.query.state || uuid.v4() | |
let params = { | |
client_id: clientId, | |
redirect_uri: getAuthAddress(), | |
response_type: 'code', | |
state: state | |
} | |
let qs = querystring.stringify(params) | |
res.redirect(`${authUrl}?${qs}`) | |
}) | |
server.listen(port, function () { | |
console.log('Monzo auth process started') | |
port = server.address().port | |
opener(getRootAddress()) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment