Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
vencax committed May 6, 2017
0 parents commit 2cf8ff8
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
@@ -0,0 +1,16 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
.*
*.sqlite
pids
logs
results

node_modules
npm-debug.log
86 changes: 86 additions & 0 deletions index.js
@@ -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)
})
24 changes: 24 additions & 0 deletions package.json
@@ -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"
}
}

0 comments on commit 2cf8ff8

Please sign in to comment.