Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2cf8ff8
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
.* | ||
*.sqlite | ||
pids | ||
logs | ||
results | ||
|
||
node_modules | ||
npm-debug.log |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
require('dotenv').config() | ||
const express = require('express') | ||
const simpleOauthModule = require('simple-oauth2') | ||
const randomstring = require('randomstring') | ||
const port = process.env.PORT || 3000 | ||
|
||
const app = express() | ||
const oauth2 = simpleOauthModule.create({ | ||
client: { | ||
id: process.env.OAUTH_CLIENT_ID, | ||
secret: process.env.OAUTH_CLIENT_SECRET, | ||
}, | ||
auth: { | ||
tokenHost: 'https://github.com', | ||
tokenPath: '/login/oauth/access_token', | ||
authorizePath: '/login/oauth/authorize', | ||
} | ||
}) | ||
|
||
// Authorization uri definition | ||
const authorizationUri = oauth2.authorizationCode.authorizeURL({ | ||
redirect_uri: process.env.REDIRECT_URL, | ||
scope: process.env.SCOPES || 'notifications', | ||
state: randomstring.generate(32), | ||
}) | ||
|
||
// Initial page redirecting to Github | ||
app.get('/auth', (req, res) => { | ||
res.redirect(authorizationUri) | ||
}) | ||
|
||
// Callback service parsing the authorization token and asking for the access token | ||
app.get('/callback', (req, res) => { | ||
const code = req.query.code | ||
const options = { | ||
code | ||
} | ||
|
||
oauth2.authorizationCode.getToken(options, (error, result) => { | ||
let mess, content | ||
|
||
if (error) { | ||
console.error('Access Token Error', error.message) | ||
mess = 'error' | ||
content = JSON.stringify(error) | ||
} else { | ||
const token = oauth2.accessToken.create(result) | ||
mess = 'success' | ||
content = { | ||
token: token.token.access_token, | ||
provider: 'github' | ||
} | ||
} | ||
|
||
const script = ` | ||
<script> | ||
(function() { | ||
function recieveMessage(e) { | ||
console.log("recieveMessage %o", e); | ||
// send message to main window with da app | ||
window.opener.postMessage( | ||
'authorization:github:${mess}:${JSON.stringify(content)}', | ||
e.origin | ||
); | ||
} | ||
window.addEventListener("message", recieveMessage, false); | ||
// Start handshare with parent | ||
console.log("Sending message: %o", "github") | ||
window.opener.postMessage("authorizing:github", "*"); | ||
})() | ||
</script>` | ||
return res.send(script) | ||
}) | ||
}) | ||
|
||
app.get('/success', (req, res) => { | ||
res.send('') | ||
}) | ||
|
||
app.get('/', (req, res) => { | ||
res.send('Hello<br><a href="/auth">Log in with Github</a>') | ||
}) | ||
|
||
app.listen(port, () => { | ||
console.log('gandalf is walkin\' on port ' + port) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "netlify-cms-github-oauth-provider", | ||
"version": "1.0.0", | ||
"description": "netlify oauth github client sending token in form as netlify service itself", | ||
"repository": "https://github.com/vencax/netlify-cms-github-oauth-provider", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node index.js" | ||
}, | ||
"keywords": [ | ||
"netlify", | ||
"cms", | ||
"auth" | ||
], | ||
"author": "Vaclav Klecanda <vencax77@gmail.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"dotenv": "^2.0.0", | ||
"express": "^4.15.2", | ||
"randomstring": "^1.1.5", | ||
"simple-oauth2": "^1.2.0" | ||
} | ||
} |