Skip to content

Commit

Permalink
auto sign-in
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Dec 21, 2015
1 parent 56386e3 commit 5c0a0eb
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .idea/dictionaries/develar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/typescript-compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
"use strict"

let outDir = "dist/Onshape-darwin-x64";
require("rimraf")(outDir, function (error) {
if (error != null) {
throw new Error(error)
}

console.log(outDir)
})

let packageJson = JSON.parse(require("fs").readFileSync("./package.json"))

let packager = require("electron-packager")
var version = "0.0.2"
var version = packageJson.version
packager({
dir: "out",
out: "dist",
name: "Onshape",
platform: "darwin",
arch: "x64",
version: "0.36.0",
version: packageJson.devDependencies["electron-prebuilt"].substring(1),
icon: "build/icon.icns",
asar: true,
"app-version": version,
"build-version": version,
"app-bundle-id": "org.develar.onshape",
"app-category-type": "public.app-category.graphics-design",
}, function done (error, appPath) { if (error != null) throw new Error(error) })
sign: "Vladimir Krivosheev"
}, function (error, appPath) {
if (error != null) throw new Error(error)
})
2 changes: 1 addition & 1 deletion build/packager.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"win": {
"title": "Onshape",
"version": "0.0.1",
"version": "0.2.0",
"icon": "build/icon.ico"
}
}
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"name": "onshape-desktop-shell",
"version": "0.0.2",
"version": "0.2.0",
"license": "MIT",
"description": "Onshape desktop app (web application shell). Unofficial.",
"private": true,
"scripts": {
"deps": "npm install && electron-rebuild .",
"start": "npm run prepare && electron ./out",
"clean": "rimraf dist",
"clean:osx": "rimraf dist/Onshape-darwin-x64",
"clean:win": "rimraf dist/win",
"compile": "rimraf out/*.js && tsc -p src",
"prepare": "npm run compile && cp package.json out/package.json && cd out && npm prune --production && npm install --production && cd ..",
"build:osx": "npm run clean:osx && npm run prepare && node build/build.js && codesign --deep --force --verbose --sign 'Vladimir Krivosheev' dist/Onshape-darwin-x64/Onshape.app",
"build:win": "npm run clean:win && npm run prepare && electron-packager out \"Onshape\" --out=dist/win --platform=win32 --arch=all --version=0.36.0 --icon=build/icon.ico --asar=true",
"prepare": "npm run compile && cp package.json out/package.json && cd out && npm prune --production && npm install --production && electron-rebuild . && cd ..",
"build:osx": "npm run prepare && node build/build.js",
"build:win": "npm run clean:win && npm run prepare && electron-packager out \"Onshape\" --out=dist/win --platform=win32 --arch=all --version=0.36.1 --icon=build/icon.ico --asar=true",
"build": "npm run build:osx && npm run build:win",
"pack:osx": "npm run build:osx && electron-builder 'dist/Onshape-darwin-x64/Onshape.app' --platform=osx --out='dist/Onshape-darwin-x64' --config=build/packager.json",
"pack:win32": "electron-builder 'dist/win/Onshape-win32-x64' --platform=win --out='dist/win' --config=build/packager.json",
Expand All @@ -30,11 +31,14 @@
"devDependencies": {
"electron-builder": "^2.4.0",
"electron-packager": "^5.2.0",
"electron-prebuilt": "^0.36.0",
"electron-prebuilt": "^0.36.1",
"electron-rebuild": "^1.0.2",
"rimraf": "^2.4.4",
"typescript": "^1.8.0-dev.20151217"
"typescript": "^1.8.0-dev.20151219"
},
"dependencies": {
"configstore": "^1.4.0"
"configstore": "^1.4.0",
"electron-debug": "^0.5.1",
"keytar": "^3.0.0"
}
}
127 changes: 127 additions & 0 deletions src/autoSignIn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
(function (): void {
"use strict"

const SERVICE_NAME = "org.develar.onshape"
const LOCAL_STORAGE_LOGIN_KEY = SERVICE_NAME.replace('.', '_') + ".login"

let passwordToSave: Credentials = null
let maybeUrlChangedTimerId: any = null
let foundFormElementTimerId: number = -1
let oldUrl: string = null

require("electron").ipcRenderer.on("maybeUrlChanged", () => { maybeUrlChanged(true) })

document.addEventListener("DOMContentLoaded", () => {
checkLocationAndSignInIfNeed()
})

class Credentials {
constructor(public login: string, public password: string) {
}
}

function getInputElement(name: string) {
return <HTMLInputElement>document.querySelector('input[name="' + name + '"]');
}

function isNotEmpty(string: string) {
// yep, get used to strict Java&Kotlin and cannot see code like (foo)
return string != null && string.length != 0
}

function setValue(input: HTMLInputElement, value: string) {
input.value = value
// we must trigger "change" event otherwise form is not submitted on click emulate (it seems, because angular (used in Onshape) doesn't detect changes immediately)
input.dispatchEvent(new Event("change", {"bubbles": true}))
}

function fillAndSubmit(formElement: HTMLFormElement) {
let login: string = localStorage.getItem(LOCAL_STORAGE_LOGIN_KEY)
if (isNotEmpty(login)) {
setValue(getInputElement("email"), login)

let password = require("keytar").getPassword(SERVICE_NAME, login)
if (isNotEmpty(password)) {
setValue(getInputElement("password"), password);
(<HTMLButtonElement>document.querySelector('div.os-form-btn-container > button[type="submit"')).click()
return
}
}

var superOnSubmit: any = formElement.onsubmit
formElement.onsubmit = () => {
passwordToSave = null
if (superOnSubmit != null) {
superOnSubmit()
}

let login = getInputElement("email").value
let password = getInputElement("password").value
if (isNotEmpty(login) && isNotEmpty(password)) {
passwordToSave = new Credentials(login, password)
}
}
}

function fillOrWait() {
let formElement = <HTMLFormElement>document.querySelector("form[name='osForm']")
if (formElement != null) {
console.log("form element found")
fillAndSubmit(formElement)
}
else {
console.log("form element not found, schedule")
setTimeout(() => {
checkLocationAndSignInIfNeed()
})
}
}

function checkLocationAndSignInIfNeed() {
let location = window.location
if (location.host == "cad.onshape.com" && location.pathname == "/signin") {
fillOrWait()
}
}

function maybeUrlChanged(checkLater: boolean) {
if (maybeUrlChangedTimerId != null) {
clearTimeout(maybeUrlChangedTimerId)
maybeUrlChangedTimerId = null
}

let newLocation = window.location
let newUrl = newLocation.href
if (oldUrl != newUrl) {
try {
console.log("url changed:", oldUrl, newUrl)
urlChanged(oldUrl, newLocation)
}
finally {
oldUrl = newUrl
}
}
else if (checkLater) {
// let's reschedule
maybeUrlChangedTimerId = setTimeout(() => { maybeUrlChanged(false) }, 100)
}
}

function urlChanged(oldUrl: string, newLocation: Location) {
if (foundFormElementTimerId != -1) {
clearTimeout(foundFormElementTimerId)
}

if (passwordToSave != null) {
if (newLocation.host == "cad.onshape.com")
if (oldUrl.endsWith("/signin") && newLocation.pathname != "/signup/forgotpassword") {
localStorage.setItem(LOCAL_STORAGE_LOGIN_KEY, passwordToSave.login)
require("keytar").replacePassword(SERVICE_NAME, passwordToSave.login, passwordToSave.password)
}
passwordToSave = null
}
else if (document.readyState != "loading") {
checkLocationAndSignInIfNeed()
}
}
}())
37 changes: 29 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import electron = require("electron")
import BrowserWindow = GitHubElectron.BrowserWindow
import BrowserWindowOptions = GitHubElectron.BrowserWindowOptions

let stateManager = new StateManager()
require('electron-debug')()

electron.crashReporter.start()
let stateManager = new StateManager()

let windows: Array<BrowserWindow> = []
let app = electron.app
Expand All @@ -24,6 +24,30 @@ app.on("window-all-closed", () => {
}
})

function registerWindowEventHandlers(window: BrowserWindow, initialUrl: string) {
window.on("closed", () => {
var index = windows.indexOf(window)
console.assert(index >= 0)
windows.splice(index, 1)
})

let webContents = window.webContents
// cannot find way to listen url change in pure JS
let frameFinishLoadedId: NodeJS.Timer = null
webContents.on("did-frame-finish-load", (event: any, isMainFrame: boolean) => {
if (frameFinishLoadedId != null) {
clearTimeout(frameFinishLoadedId)
frameFinishLoadedId = null
}
frameFinishLoadedId = setTimeout(() => {
webContents.send("maybeUrlChanged")
}, 300)
})
webContents.on("will-navigate", (e: any, u: string) => {
console.log("will-navigate", webContents.getURL(), e)
})
}

function openWindows() {
let descriptors = stateManager.getWindows()
if (descriptors == null || descriptors.length === 0) {
Expand All @@ -35,9 +59,10 @@ function openWindows() {
let options: BrowserWindowOptions = {
// to avoid visible maximizing
show: false,
preload: __dirname + "/autoSignIn.js",
webPreferences: {
// fix jquery issue (https://github.com/atom/electron/issues/254), and in any case node integration is not required
nodeIntegration: false
nodeIntegration: false,
}
}

Expand All @@ -56,11 +81,7 @@ function openWindows() {
}
window.loadURL(descriptor.url)
window.show()
window.on("closed", () => {
var index = windows.indexOf(window)
console.assert(index >= 0)
windows.splice(index, 1)
})
registerWindowEventHandlers(window, descriptor.url)
windows.push(window)
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/tsd.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
},
"node/node.d.ts": {
"commit": "11322524f8db9cdb921427cad84cd22fe2d4f965"
},
"keytar/keytar.d.ts": {
"commit": "40c60850ad6c8175a62d5ab48c4e016ea5b3dffe"
}
}
}
2 changes: 1 addition & 1 deletion src/typings/configstore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ declare module 'configstore' {
del(key: string): void;
}

export = ConfigStore;
export = ConfigStore
}
Loading

0 comments on commit 5c0a0eb

Please sign in to comment.