-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a window for authentication
- Loading branch information
Showing
16 changed files
with
224 additions
and
29 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"presets": [ | ||
"es2015", | ||
"stage-0", | ||
"react" | ||
] | ||
} |
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
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,39 @@ | ||
import {BrowserWindow, ipcMain, session} from 'electron'; | ||
|
||
export default class LoginModal { | ||
constructor(parent) { | ||
const publicPath = process.env.NODE_ENV === 'development' ? `file://${__dirname}/../renderer/login` : `file://${__dirname}/login`; | ||
|
||
this.ipc = ipcMain; | ||
|
||
this.window = new BrowserWindow({ | ||
parent: parent, | ||
modal: true, | ||
show: false, | ||
width: 600, | ||
height: 400, | ||
resizable: false | ||
}); | ||
this.window.loadURL(`${publicPath}/index.html`); | ||
this.window.once('ready-to-show', () => this.window.show()); | ||
this.window.on('closed', () => this.window = null); | ||
|
||
this.ipc.on('RequestSetCookie', ::this._onRequestSetCookie) | ||
} | ||
|
||
_onRequestSetCookie(e, cookieValue) { | ||
const cookie = { | ||
url: 'http://.nicovideo.jp', | ||
name: 'user_session', | ||
value: cookieValue | ||
} | ||
|
||
session.defaultSession.cookies.set(cookie, ((err) => { | ||
if (err) { | ||
alert(err); | ||
} else { | ||
this.window.close(); | ||
} | ||
})); | ||
} | ||
} |
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,22 @@ | ||
import {app, BrowserWindow, ipcMain} from 'electron'; | ||
import path from 'path'; | ||
import windowStateKeeper from 'electron-window-state'; | ||
import LoginModal from './LoginModal'; | ||
|
||
export default class MainWindow { | ||
constructor() { | ||
const publicPath = process.env.NODE_ENV === 'development' ? `file://${__dirname}/../renderer` : `file://${__dirname}`; | ||
const windowState = windowStateKeeper(); | ||
|
||
this.window = new BrowserWindow({ ...windowState }); | ||
|
||
this.window.loadURL(`${publicPath}/index.html`); | ||
this.window.on('closed', () => this.window = null); | ||
windowState.manage(this.window); | ||
this.loginModal = null; | ||
} | ||
|
||
createLoginModal() { | ||
this.loginModal = new LoginModal(this.window); | ||
} | ||
} |
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,60 @@ | ||
import sqlite3 from 'sqlite3'; | ||
import path from 'path'; | ||
import keytar from 'keytar'; | ||
import crypto from 'crypto'; | ||
|
||
export default class SessionExtractor { | ||
constructor() { | ||
const dbPath = path.resolve(path.join(process.env.HOME, 'Library/Application Support/Google/Chrome/Default/Cookies')); | ||
this.db = new sqlite3.Database(dbPath); | ||
} | ||
|
||
extract() { | ||
return new Promise((resolve, reject) => { | ||
this.db.serialize(() => { | ||
Promise.resolve() | ||
.then(() => { | ||
return this.getEncryptedUserSession(); | ||
}) | ||
.then((value) => { | ||
this.db.close(); | ||
resolve(this.decryptUserSession(value)); | ||
}) | ||
.catch((err) => { | ||
this.db.close(); | ||
reject(err); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
getEncryptedUserSession() { | ||
return new Promise((resolve, reject) => { | ||
this.db.get("SELECT * FROM cookies WHERE host_key = '.nicovideo.jp' AND name = 'user_session'", (err, row) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
|
||
resolve(row.encrypted_value); | ||
}); | ||
}); | ||
} | ||
|
||
decryptUserSession(encryptedValue) { | ||
return new Promise((resolve, reject) => { | ||
const password = keytar.getPassword('Chrome Safe Storage', 'Chrome'); | ||
|
||
crypto.pbkdf2(password, 'saltysalt', 1003, 16, 'sha1', (err, key) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
|
||
const iv = new Array(17).join(' '); | ||
const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv); | ||
resolve(decipher.update(encryptedValue.slice(3)) + decipher.final()); | ||
}); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,23 +1,29 @@ | ||
import {app, BrowserWindow} from 'electron'; | ||
import {app} from 'electron'; | ||
import MainWindow from './browser/MainWindow'; | ||
|
||
let mainWindow = null; | ||
|
||
app.on('ready', () => { | ||
if (mainWindow) return; | ||
class Main { | ||
constructor() { | ||
this.mainWindow = null; | ||
} | ||
|
||
mainWindow = new BrowserWindow({width: 800, height: 600}); | ||
mainWindow.loadURL(`file://${__dirname}/../src/index.html`); | ||
mainWindow.on('closed', () => { | ||
mainWindow = null; | ||
}); | ||
onReady() { | ||
this.mainWindow = new MainWindow(); | ||
this.mainWindow.createLoginModal(); | ||
} | ||
|
||
if (process.env.NODE_ENV === 'development') { | ||
mainWindow.openDevTools(); | ||
onWindowAllClosed() { | ||
if (process.platform !== 'darwin') { | ||
app.quit(); | ||
} | ||
} | ||
} | ||
|
||
const main = new Main(); | ||
|
||
app.on('ready', () => { | ||
main.onReady(); | ||
}); | ||
|
||
app.on('window-all-closed', () => { | ||
if (process.platform !== 'darwin') { | ||
app.quit(); | ||
} | ||
main.onWindowAllClosed(); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,30 @@ | ||
import React from 'react'; | ||
import {ipcRenderer} from 'electron'; | ||
import SessionExtractor from '../../../libraries/SessionExtractor'; | ||
|
||
export default class App extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.ipc = ipcRenderer; | ||
} | ||
|
||
handleLogin() { | ||
const sessionExtractor = new SessionExtractor(); | ||
sessionExtractor.extract() | ||
.then((sessionId) => { | ||
console.log(sessionId); | ||
this.ipc.send('RequestSetCookie', sessionId); | ||
}) | ||
.catch((err) => { | ||
}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<p onClick={::this.handleLogin}>ログイン</p> | ||
</div> | ||
); | ||
} | ||
} |
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,19 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Nicomentron - ログイン</title> | ||
<meta charset="utf-8"> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script> | ||
{ | ||
const script = document.createElement('script'); | ||
const port = process.env.PORT || 3000; | ||
script.src = process.env.NODE_ENV === 'development' ? | ||
'http://localhost:' + port + '/login/index.js' : 'login/index.js'; | ||
document.write(script.outerHTML); | ||
} | ||
</script> | ||
</body> | ||
</html> |
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,5 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import App from './components/App'; | ||
|
||
ReactDOM.render(<App />, document.getElementById('root')); |
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 |
---|---|---|
|
@@ -18,5 +18,8 @@ export default { | |
} | ||
] | ||
}, | ||
externals: ['sqlite3'] | ||
externals: [ | ||
'sqlite3', | ||
'keytar' | ||
] | ||
}; |
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
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
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