oauth-electron
Advanced tools
Comparing version 1.1.12 to 2.0.0
const Oauth = require('./oauth2'), | ||
url = require('url') | ||
const getTokens = (oauth, address, resolve, reject) => { | ||
let parsed = url.parse(address, true) | ||
const getTokens = async (oauth, address) => { | ||
const parsed = url.parse(address, true) | ||
if(!parsed.query.code) | ||
return reject(`URL response is not correct, parameters are ${JSON.stringify(parsed.query)}`) | ||
oauth.getTokens(parsed.query.code) | ||
.then(resolve) | ||
.catch(reject) | ||
throw `URL response is not correct, parameters are ${JSON.stringify(parsed.query)}` | ||
return await oauth.getTokens(parsed.query.code) | ||
} | ||
const bindWindowsEvents = (window, oauth) => | ||
(resolve, reject) => { | ||
const bindWindowsEvents = (window, oauth, session, filter) => | ||
new Promise((resolve, reject) => { | ||
window.webContents.on('close', () => { | ||
reject('closed window') | ||
}); | ||
window.webContents.on('did-get-redirect-request', (_, __, address) => { | ||
if (!address.includes(oauth.redirectUrl)) | ||
return | ||
getTokens(oauth, address, resolve, reject) | ||
}) | ||
} | ||
const login = (info, window) => { | ||
let oauth = new Oauth(info) | ||
let promise = new Promise(bindWindowsEvents(window, oauth)) | ||
session.defaultSession.webRequest.onCompleted(filter, async (details) => { | ||
try { | ||
resolve(await getTokens(oauth, details.url)) | ||
} catch (error) { | ||
reject(error) | ||
} | ||
}); | ||
}) | ||
const login = async(info, window, session) => { | ||
const oauth = new Oauth(info) | ||
const filter = { urls: [`${info.redirectUrl}*`] }; | ||
const events = bindWindowsEvents(window, oauth, session, filter) | ||
window.loadURL(oauth.getAuthUrl()) | ||
return promise | ||
return await events | ||
} | ||
module.exports = login |
@@ -26,6 +26,6 @@ const Oauth2 = require('oauth').OAuth2 | ||
redirect_uri: this.info.redirectUrl | ||
}, (error, accessToken, refreshToken) => { | ||
}, (error, _, __, results) => { | ||
if(error) | ||
return reject(error) | ||
resolve({accessToken,refreshToken}) | ||
resolve(results) | ||
}) | ||
@@ -32,0 +32,0 @@ }) |
{ | ||
"name": "oauth-electron", | ||
"version": "1.1.12", | ||
"version": "2.0.0", | ||
"description": "easy oauth generic handling for electron", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"test:int": "jest test/integration", | ||
"test:cov": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", | ||
"test:cov": "jest --coverage && codecov", | ||
"push": "yarn test && git push", | ||
@@ -41,3 +41,3 @@ "deploy:major": "yarn version --major", | ||
"devDependencies": { | ||
"coveralls": "3.0.2", | ||
"codecov": "3.2.0", | ||
"electron": "4.0.5", | ||
@@ -44,0 +44,0 @@ "faker": "4.1.0", |
#  | ||
[](https://travis-ci.org/kanekotic/oauth-electron) | ||
[](https://coveralls.io/github/kanekotic/oauth-electron?branch=master) | ||
[](https://codecov.io/gh/kanekotic/oauth-electron) | ||
[](https://github.com/kanekotic/oauth-electron) | ||
@@ -50,2 +50,3 @@ [](https://github.com/kanekotic/oauth-electron/blob/master/LICENSE) | ||
const login = require('oauth-electron') | ||
const { BrowserWindow, session } = require('electron') | ||
@@ -64,3 +65,3 @@ let info = { | ||
login.oauth2(info, window) | ||
login.oauth2(info, window, session) | ||
``` | ||
@@ -73,3 +74,4 @@ | ||
accessToken: ***, | ||
refreshToken: *** | ||
refreshToken?: ***, | ||
<others: ***> | ||
} | ||
@@ -84,2 +86,7 @@ ``` | ||
## Migration V1.x to V2.x | ||
- Please refer to the response of the service, as there will probably be extra fields in the response | ||
- session is required to be passed as events of chromium have changed | ||
## Migration V0.x to V1.x | ||
@@ -86,0 +93,0 @@ |
@@ -8,17 +8,16 @@ const {app, BrowserWindow, session} = require('electron'), | ||
secret: process.env.FB_SECRET, | ||
scope: "user_posts,user_friends,publish_actions", | ||
scope: "email", | ||
baseSite: "", | ||
authorizePath: "https://www.facebook.com/dialog/oauth", | ||
accessTokenPath: "https://graph.facebook.com/oauth/access_token", | ||
redirectUrl: "http://localhost/" | ||
redirectUrl: "https://www.facebook.com/connect/login_success.html" | ||
}, | ||
window = new BrowserWindow({"node-integration": false}) | ||
window.webContents.session.cookies.remove("https://www.facebook.com/", 'c_user', () => { | ||
login.oauth2(info, window) | ||
.then(_ => { | ||
window.webContents.executeJavaScript(`document.body.innerHTML += '<div id="result">Success</div>'`) | ||
}) | ||
.catch(_ =>{ | ||
window.webContents.executeJavaScript(`document.body.innerHTML += '<div id="result">Error</div>'`) | ||
}) | ||
window.webContents.session.cookies.remove("https://www.facebook.com/", 'c_user', async () => { | ||
try { | ||
await login.oauth2(info, window, session) | ||
window.webContents.executeJavaScript(`document.body.innerHTML += '<div id="result">Success</div>'`) | ||
} catch (_) { | ||
window.webContents.executeJavaScript(`document.body.innerHTML += '<div id="result">Error</div>'`) | ||
} | ||
}) | ||
@@ -25,0 +24,0 @@ } |
@@ -5,3 +5,3 @@ const Application = require('spectron').Application | ||
describe.skip('oauth electron', () => { | ||
describe('oauth electron', () => { | ||
let app | ||
@@ -8,0 +8,0 @@ beforeAll(async () => { |
@@ -13,4 +13,15 @@ jest.mock('../../../lib/oauth2', () => jest.fn()) | ||
class TestEmitter extends EventEmitter {} | ||
class WebEmitter extends EventEmitter { | ||
onCompleted(_, listener){ | ||
return super.on('Completed',listener) | ||
} | ||
} | ||
describe('login should', () => { | ||
const session = { | ||
defaultSession:{ | ||
webRequest: new WebEmitter() | ||
} | ||
} | ||
beforeEach(() => { | ||
@@ -33,3 +44,3 @@ url.parse.mockClear() | ||
login(info, window) | ||
login(info, window, session) | ||
@@ -47,3 +58,5 @@ expect(Oauth).toBeCalledWith(info) | ||
try { | ||
let result = login(undefined, window) | ||
let result = login({ | ||
redirectUrl: faker.random.uuid() | ||
}, window, session) | ||
window.webContents.emit('close') | ||
@@ -81,4 +94,4 @@ await result | ||
redirectUrl: authUrl | ||
}, window) | ||
window.webContents.emit('did-get-redirect-request', undefined, undefined, authUrl) | ||
}, window, session) | ||
session.defaultSession.webRequest.emit('Completed', { url: authUrl }) | ||
result = await result | ||
@@ -108,4 +121,4 @@ expect(url.parse).toBeCalledWith(authUrl, true) | ||
redirectUrl: authUrl | ||
}, window) | ||
window.webContents.emit('did-get-redirect-request', undefined, undefined, authUrl) | ||
}, window, session) | ||
session.defaultSession.webRequest.emit('Completed', { url: authUrl }) | ||
result = await result | ||
@@ -142,4 +155,4 @@ expect(true).toBeFalsy() | ||
redirectUrl: authUrl | ||
}, window) | ||
window.webContents.emit('did-get-redirect-request', undefined, undefined, authUrl) | ||
}, window, session) | ||
session.defaultSession.webRequest.emit('Completed', { url: authUrl }) | ||
result = await result | ||
@@ -146,0 +159,0 @@ expect(true).toBeFalsy() |
@@ -78,6 +78,8 @@ jest.mock('oauth', () =>({ | ||
}, | ||
accessToken = faker.random.uuid(), | ||
refreshToken = faker.random.uuid(), | ||
expected = { | ||
accessToken: faker.random.uuid(), | ||
other: faker.random.uuid() | ||
}, | ||
mockOauth = { | ||
getOAuthAccessToken: jest.fn((_,__,cb) => cb(undefined, accessToken,refreshToken)) | ||
getOAuthAccessToken: jest.fn((_,__,cb) => cb(undefined, undefined,undefined,expected)) | ||
} | ||
@@ -88,3 +90,3 @@ | ||
let oauth = new Oauth(info) | ||
let result = oauth.getTokens(code) | ||
let result = await oauth.getTokens(code) | ||
@@ -98,3 +100,3 @@ expect(mockOauth.getOAuthAccessToken).toBeCalledWith( | ||
) | ||
expect(result).resolves.toEqual({accessToken, refreshToken}) | ||
expect(result).toEqual(expected) | ||
}) | ||
@@ -111,7 +113,10 @@ | ||
let oauth = new Oauth({}) | ||
let result = oauth.getTokens("pepe") | ||
expect(result).rejects.toEqual(error) | ||
try { | ||
await oauth.getTokens("pepe") | ||
fail() | ||
} catch (e) { | ||
expect(e).toEqual(error) | ||
} | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
203922
566
94