Created
December 5, 2015 15:40
-
-
Save simov/015ae9f059e9b95cd85b to your computer and use it in GitHub Desktop.
Client Side Implicit OAuth Flow automation with NodeJS and NWjs
This file contains 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
{ | |
"name": "client-side-implicit-oauth-flow", | |
"main": "authorize.html", | |
"window": { | |
"show": false, | |
"show_in_taskbar": false | |
} | |
} |
This file contains 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
var fs = require('fs') | |
var path = require('path') | |
var http = require('http') | |
var url = require('url') | |
var qs = require('querystring') | |
var child = require('child_process') | |
var nw = null | |
var server = http.createServer() | |
server.on('request', function (req, res) { | |
if (req.url == '/connect') { | |
var dpath = path.resolve(__dirname) | |
nw = child.spawn('nw', [dpath]) | |
res.end() | |
} | |
else if (req.url == '/callback') { | |
var fpath = path.resolve(__dirname, 'token.html') | |
var body = fs.readFileSync(fpath, 'utf8') | |
res.writeHead(200, {'content-type': 'text/html'}) | |
res.end(body) | |
} | |
else if (/^\/token/.test(req.url)) { | |
var uri = url.parse(req.url) | |
var query = qs.parse(uri.query) | |
console.log(query) | |
nw.on('close', function (code, signal) { | |
console.log('NW closed') | |
}) | |
nw.kill('SIGHUP') | |
res.end() | |
} | |
}) | |
server.listen(3000, function () { | |
console.log('HTTP server listening on port ' + 3000) | |
}) |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> | |
<title>Client Side Implicit OAuth Flow</title> | |
<script type="text/javascript" charset="utf-8"> | |
var config = { | |
callback_uri: 'http://localhost:3000/token' | |
} | |
var access_token = window.location.hash.replace('#access_token=', '') | |
var url = config.callback_uri + '?access_token=' + access_token | |
window.location.href = url | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment