Skip to content

Commit

Permalink
add all grant type
Browse files Browse the repository at this point in the history
ref #4
  • Loading branch information
mironal committed Dec 10, 2017
1 parent 978a2b9 commit a3d9601
Show file tree
Hide file tree
Showing 27 changed files with 13,804 additions and 2,599 deletions.
9 changes: 9 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": [
["env", {
"targets": {
"node": "8.9.2"
}
}]
]
}
39 changes: 37 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
module.exports = {
"parserOptions": {
"ecmaVersion": 8,
"ecmaFeatures": {

"experimentalObjectRestSpread": true
}
},
"env": {
"browser": true,
"es6": true,
"node": true
"node": true,
"jest": true,
},
"extends": "eslint:recommended",
"extends": [
"eslint:recommended",
"plugin:ava/recommended",
"plugin:node/recommended",
"plugin:promise/recommended",
"plugin:security/recommended",
"plugin:import/errors",
"plugin:import/warnings",
],
"plugins": [
"ava",
"node",
"import",
"promise",
"security"
],
"rules": {
"node/no-unpublished-require": ["error", {
"allowModules": ["electron"]
}],
"promise/avoid-new": "off",
"promise/always-return": "off",
"no-multiple-empty-lines": ["error", { max: 1 }],
"indent": [
"error",
2,
Expand All @@ -22,6 +51,12 @@ module.exports = {
"semi": [
"error",
"never"
],
"no-var": [
"error"
],
"prefer-const": [
"error"
]
}
};
5 changes: 4 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.vscode
coverage
example/
docs/
.eslintrc*
ss/
ss/
**/*.test.js
*/__test_utils/*
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ provider.perform()
const { OAuth2Provider } = require("electron-oauth-helper")

const config = { /* oauth config. please see example/main/config.example.js. */}
const provider = new OAuth2Provider(config)
const provider = new OAuth2Provider(config).withCustomAuthPrameter({}).withCustomTokenParameter({})
provider.perform()
.then(resp => {
console.log(resp)
Expand Down Expand Up @@ -289,14 +289,12 @@ provider.perform()

#### Access Token Response

> [Successful Response](https://tools.ietf.org/html/rfc6749#section-5.1)
> [Successful Response](https://tools.ietf.org/html/rfc6749#section-4.2.2)
> [Error Response](https://tools.ietf.org/html/rfc6749#section-4.2.2.1)
### Resource Owner Password Credentials Grant

TODO

> [RFC 6749: 4.3](https://tools.ietf.org/html/rfc6749#section-4.3)
```js
Expand Down Expand Up @@ -349,8 +347,6 @@ No need.
### Client Credentials Grant

TODO

> [RFC 6749: 4.4](https://tools.ietf.org/html/rfc6749#section-4.4)
```js
Expand Down
4 changes: 3 additions & 1 deletion example/src/main/config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const GitHub = {
client_id: "your client id",
client_secret: "your client secret",
scope: "read:user",
redirect_url: "your redirect url",
redirect_uri: "your redirect url",
authorize_url: "https://github.com/login/oauth/authorize",
access_token_url: "https://github.com/login/oauth/access_token",
}
Expand Down Expand Up @@ -64,6 +64,7 @@ const Firebase = {
messagingSenderId: ""
}

/* eslint-disable security/detect-object-injection*/
const mapTypeToConfig = type => {
return {
GitHub,
Expand All @@ -75,5 +76,6 @@ const mapTypeToConfig = type => {
Firebase,
}[type]
}
/* eslint-enable */

module.exports = mapTypeToConfig
31 changes: 26 additions & 5 deletions example/src/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const {
const firebase = require("firebase")

const mapTypeToConfig = require("./config")

firebase.initializeApp(mapTypeToConfig("Firebase"))
if (mapTypeToConfig("Firebase")) {
firebase.initializeApp(mapTypeToConfig("Firebase"))
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
Expand Down Expand Up @@ -81,11 +82,31 @@ ipc.on("oauth", (event, type) => {
})()

const provider = new Provider(config)
provider.perform()

const options = Object.assign({
show: false,
width: 800,
height: 800,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
},
})

let window = new BrowserWindow(options)
window.once("ready-to-show", () => {
window.show()
})
window.once("closed", () => {
window = null
})

provider.perform(window)
.then(resp => {
console.log(resp)
window.close()
console.log("Got response (◍•ᴗ•◍):", resp)
})
.catch(error => console.error(error))
})

process.on("uncaughtException", error => console.error(error))
process.on("uncaughtException", error => console.error(error))
49 changes: 32 additions & 17 deletions example/src/render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const {
OAuth2Provider,
} = require("electron").remote.require("../../../")

const { BrowserWindow } = require("electron").remote

/** @type {firebase} */
const firebase = require("electron").remote.require("firebase")
const querystring = require("querystring")
Expand All @@ -32,38 +34,51 @@ const onClick = event => {
return OAuth2Provider
}
})()

const options = Object.assign({
show: false,
width: 800,
height: 800,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
},
})

let window = new BrowserWindow(options)
window.once("ready-to-show", () => {
window.show()
})
window.once("closed", () => {
window = null
})

const provider = new Provider(config)
provider.perform()
provider.perform(window)
.then(resp => {
console.log("Got response", resp)
window.close()

if (isLinkFirebaseAuth && type === "GoogleClientWebApp") {

console.log("join google")
// https://firebase.google.com/docs/reference/js/firebase.auth.GoogleAuthProvider#.credential
const credential = firebase.auth.GoogleAuthProvider.credential(null, resp.access_token)
firebase.auth().signInWithCredential(credential)
.then(result => {
console.log("joined google", result)
})
.catch(error => {
console.error(error)
})
return firebase.auth().signInWithCredential(credential)
}

if (isLinkFirebaseAuth && type === "GitHub") {

console.log("join github")
const query = querystring.parse(resp)
// https://firebase.google.com/docs/auth/web/github-auth#handle_the_sign-in_flow_manually
const credential = firebase.auth.GithubAuthProvider.credential(query.access_token)
firebase.auth().signInWithCredential(credential)
.then(result => {
console.log(result)
})
.catch(error => {
console.error(error)
})
return firebase.auth().signInWithCredential(credential)
}
return resp
})
.then(resp => {
console.log("Got response", resp)
})
.catch(error => console.error(error))
} else {

ipc.send("oauth", type)
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ const OAuth2Provider = require("./lib/oauth2")
module.exports = {
OAuth1Provider,
OAuth2Provider,
}
}
68 changes: 68 additions & 0 deletions lib/__test_utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const Koa = require("koa")
const Router = require("koa-router")
const bodyParser = require("koa-bodyparser")
const net = require("net")
const debug = require("debug")("eoh:test_util")

const routing = koa => routingFn => {
const router = new Router()
routingFn(router)
koa.use(router.routes())
}

const startServer = async t => {
const port = await getPort()
const koa = new Koa()
koa.use(bodyParser())
const server = koa.listen(port)
t.context = {
koa,
port,
server,
}
}

const stopServer = async t => {
if (t && t.context && t.context.server) {
await t.context.server.close()
}
}

// private

const getPort = async () => {

return new Promise((resolve, reject) => {

let port = Math.floor(Math.random() * (60000 - 5000 + 1) + 5000)
const server = net.createServer()
server.on("error", error => {
debug("got error:", error)
server.close(() => {
port += 1
if (error.code !== "EADDRINUSE" || port >= 65535) {
server.removeAllListeners()
reject(error)
}
setImmediate(() => {
server.listen(port)
})
})
})
server.on("listening", () => {
debug("connected:", port)
server.removeAllListeners()
server.close(() => {
resolve(port)
})
})
debug("try connect:", port)
server.listen(port)
})
}

module.exports = {
startServer,
stopServer,
routing
}
55 changes: 0 additions & 55 deletions lib/helper.js

This file was deleted.

Loading

0 comments on commit a3d9601

Please sign in to comment.