Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mironal committed Dec 3, 2017
0 parents commit 35231cc
Show file tree
Hide file tree
Showing 16 changed files with 3,105 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
2,
{ SwitchCase: 1 }
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"double"
],
"semi": [
"error",
"never"
]
}
};
64 changes: 64 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# vscode
.vscode

# example
example/src/main/config.js
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode
coverage
example/
.eslintrc*
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 mironal

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Electron OAuth Helper

## What's this

Easy to use helper library for OAuth1 and OAuth2.

This automatically manages the Electron window for OAuth.

You can get a token just by calling a method of start OAuth.

## Install

`npm run electron-oauth-helper --save`

## Usage

### OAuth1

```js

const { OAuth1Provider } = require("electron-oauth-helper")

const config = { /* oauth config. please see example/main/config.example.js. */}
const provider = new OAuth1Provider(config)
provider.perform()
.then(token => {
console.log(token)
})
.catch(error => console.error(error))
```

### OAuth2

```js

const { OAuth2Provider } = require("electron-oauth-helper")

const config = { /* oauth config. please see example/main/config.example.js. */}
const provider = new OAuth2Provider(config)
provider.perform()
.then(token => {
console.log(token)
})
.catch(error => console.error(error))
```

## Example

example electron app => `example/`
69 changes: 69 additions & 0 deletions example/src/main/config.example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const GitHub = {
client_id: "your client id",
client_secret: "your client secret",
scope: "read:user",
redirect_url: "your redirect url",
authorize_url: "https://github.com/login/oauth/authorize",
access_token_url: "https://github.com/login/oauth/access_token",
}

// Client-side Web Applications
// Impolicit Flow
const GoogleClientWebApp = {
client_id: "your client id",
client_secret: "your client secret",
redirect_uri: "your redirect uri",
authorize_url: "https://accounts.google.com/o/oauth2/v2/auth",
response_type: "token",
scope: "https://www.googleapis.com/auth/userinfo.profile",
}

// Web Server Applications
const GoogleWebServerApp = {
client_id: "your client id",
client_secret: "your client secret",
redirect_uri: "your redirect url",
authorize_url: "https://accounts.google.com/o/oauth2/v2/auth",
access_token_url: "https://www.googleapis.com/oauth2/v4/token",
access_type: "online",
response_type: "code",
scope: "https://www.googleapis.com/auth/userinfo.profile",
}

const FacebookImplicit = {
client_id: "your client id",
client_secret: "your client secret",
authorize_url: "https://www.facebook.com/v2.11/dialog/oauth",
response_type: "token",
redirect_uri: "https://www.facebook.com/connect/login_success.html",
}

const FacebookAuthCode = {
client_id: "your client id",
client_secret: "your client secret",
authorize_url: "https://www.facebook.com/v2.11/dialog/oauth",
access_token_url: "https://graph.facebook.com/v2.11/oauth/access_token",
redirect_uri: "https://www.facebook.com/connect/login_success.html",
}

const Twitter = {
oauth_consumer_key: "your consumer key",
oauth_consumer_secret: "your consumer secret",
request_token_url: "https://api.twitter.com/oauth/request_token",
authenticate_url: "https://api.twitter.com/oauth/authenticate",
access_token_url: "https://api.twitter.com/oauth/access_token",
callback_url: "your callback url"
}

const mapTypeToConfig = type => {
return {
GitHub,
GoogleWebServerApp,
GoogleClientWebApp,
FacebookImplicit,
FacebookAuthCode,
Twitter
}[type]
}

module.exports = mapTypeToConfig
101 changes: 101 additions & 0 deletions example/src/main/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-disable no-console */

const { app, BrowserWindow } = require("electron")
const path = require("path")
const url = require("url")
const ipc = require("electron").ipcMain

const {
OAuth1Provider,
OAuth2Provider,
} = require("../../../")

const mapTypeToConfig = require("./config")

// 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.
let win

function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})

// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname,"../render", "index.html"),
protocol: "file:",
slashes: true
}))

// Open the DevTools.
win.webContents.openDevTools()

// Emitted when the window is closed.
win.on("closed", () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", createWindow)

// Quit when all windows are closed.
app.on("window-all-closed", () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
app.quit()
}
})

app.on("activate", () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})

const useOAuth1 = type => {
return type === "Twitter"
}

ipc.on("oauth", (event, type) => {

const config = mapTypeToConfig(type)
if (!config) {
console.warn(`Unknown type: "${type}"`)
return
}
if (useOAuth1(type)) {
oauth1(config)
} else {
oauth2(config)
}
})

const oauth1 = config => {
const provider = new OAuth1Provider(config)
provider.perform()
.then(token => {
console.log(token)
})
.catch(error => console.error(error))
}

const oauth2 = config => {

const provider = new OAuth2Provider(config)
provider.perform()
.then(token => {
console.log(token)
})
.catch(error => console.error(error))
}

process.on("uncaughtException", error => console.error(error))
29 changes: 29 additions & 0 deletions example/src/render/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>electron oauth helper example</title>
</head>
<body>
<h1>OAuth provider</h1>
<div>
<button class="oauth-btn">GitHub</button>
</div>
<div>
<button class="oauth-btn">FacebookImplicit</button>
</div>
<div>
<button class="oauth-btn">FacebookAuthCode</button>
</div>
<div>
<button class="oauth-btn">GoogleClientWebApp</button>
</div>
<div>
<button class="oauth-btn">GoogleWebServerApp</button>
</div>
<div>
<button class="oauth-btn">Twitter</button>
</div>
</body>
<script src="./index.js"></script>
</html>
17 changes: 17 additions & 0 deletions example/src/render/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const ipc = require("electron").ipcRenderer

const onClick = event => {

const provider = event.target.innerHTML
ipc.send("oauth", provider)
}

const init = () => {
document.querySelectorAll(".oauth-btn").forEach(elem => elem.addEventListener("click", onClick))
}

if (document.readyState !== "loading") {
init()
} else {
document.addEventListener("DOMContentLoaded", init)
}
Loading

0 comments on commit 35231cc

Please sign in to comment.