-
-
Notifications
You must be signed in to change notification settings - Fork 68
Add Clerk authentication with Angular login component and Electron IPC token storage #635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
25
commits into
master
Choose a base branch
from
copilot/create-angular-login-component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
13a1d2d
Initial plan
Copilot 6e0e17e
Add Clerk authentication integration with Angular login component and…
Copilot 8a03f24
Merge branch 'master' into copilot/create-angular-login-component
highperformancecoder 3ebc404
Added a "login to clerk" menu item, and added a clerk publishable key.
highperformancecoder 9d1d261
Fix login window not rendering by using createPopupWindowWithRouting
Copilot 62a9ead
Fix login window showing app shell (header/tabs) by excluding login r…
Copilot f9b417d
Move login route to headless/login for consistency with other popup w…
Copilot 6f7259d
Add openLoginWindow() promise that resolves when auth token is stored
Copilot 60f50ea
Rearrange promise from events to WindowManager
highperformancecoder 8da2877
Close login dialog window once submitted.
highperformancecoder 59e8663
Got the upgradeUsingClerk process working.
highperformancecoder 6539e6e
Dead code removal
highperformancecoder 8663d10
More dead code removal.
highperformancecoder c5fae66
Merge branch 'master' into copilot/create-angular-login-component
highperformancecoder 5936fe1
Address review comments from CodeRabbit.
highperformancecoder 32fbb2d
Initial plan
Copilot 8852185
Preserve Clerk session between login window opens
Copilot 7d51337
Fix setSession to actually reinstate the Clerk session
Copilot b1d222b
Merge pull request #642 from highperformancecoder/copilot/preserve-cl…
highperformancecoder b71b657
Handle aborting Clerk login.
highperformancecoder a954a75
Fix up use of promises in WindowManager.openLoginWindow
highperformancecoder 30560ee
Merge branch 'copilot/create-angular-login-component' of github.com:h…
highperformancecoder 8b825db
Address code review comment
highperformancecoder 72d9e24
Dead code removal
highperformancecoder 35e0140
Addres code review nits.
highperformancecoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule ecolab
updated
10 files
| +1 −1 | classdesc | |
| +1 −2 | include/Makefile | |
| +7 −3 | models/SAR.py | |
| +887 −0 | models/SAR.rvl | |
| +1,136 −0 | models/SARResults.rvl | |
| +13 −41 | models/ecolab_model.cc | |
| +4 −2 | models/ecolab_model.h | |
| +108 −0 | models/fragmentation_pump.py | |
| +26 −0 | models/loadSARResults.py | |
| +4 −4 | models/panmictic_ecolab.py |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -26,6 +26,67 @@ import ProgressBar from 'electron-progressbar'; | |
| import {exec,spawn} from 'child_process'; | ||
| import decompress from 'decompress'; | ||
| import {promisify} from 'util'; | ||
| import {net, safeStorage } from 'electron'; | ||
|
|
||
| function semVer(version: string) { | ||
| const pattern=/(\d+)\.(\d+)\.(\d+)/; | ||
| let [,major,minor,patch]=pattern.exec(version); | ||
| return {major: +major,minor: +minor, patch: +patch}; | ||
| } | ||
| function semVerLess(x: string, y: string): boolean { | ||
| let xver=semVer(x), yver=semVer(y); | ||
| return xver.major<yver.major || | ||
| xver.major===yver.major && ( | ||
| xver.minor<yver.minor || | ||
| xver.minor===yver.minor && xver.patch<yver.patch | ||
| ); | ||
| } | ||
|
|
||
| const backendAPI='https://minskybe-x7dj1.sevalla.app/api'; | ||
| // perform a call on the backend API, returning the JSON encoded result | ||
| // options is passed to the constructor of a ClienRequest object https://www.electronjs.org/docs/latest/api/client-request#requestendchunk-encoding-callback | ||
| async function callBackendAPI(options: string|Object, token: string) { | ||
| return new Promise<string>((resolve, reject)=> { | ||
| let request=net.request(options); | ||
| request.setHeader('Authorization',`Bearer ${token}`); | ||
| request.on('response', (response)=>{ | ||
| let chunks=[]; | ||
| response.on('data', (chunk)=>{chunks.push(chunk);}); | ||
| response.on('end', ()=>resolve(Buffer.concat(chunks).toString())); | ||
| response.on('error',()=>reject(response.statusMessage)); | ||
| }); | ||
| request.on('error',(err)=>reject(err.toString())); | ||
| request.end(); | ||
| }); | ||
| } | ||
|
|
||
| // to handle redirects | ||
| async function getFinalUrl(initialUrl, token) { | ||
| try { | ||
| const response = await fetch(initialUrl, { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Authorization': `Bearer ${token}` | ||
| }, | ||
| redirect: 'manual' // This tells fetch NOT to follow the link automatically | ||
| }); | ||
highperformancecoder marked this conversation as resolved.
Dismissed
Show dismissed
Hide dismissed
|
||
|
|
||
| // In 'manual' mode, a redirect returns an 'opaqueredirect' type or status 302 | ||
| if (response.status >= 300 && response.status < 400) { | ||
| const redirectUrl = response.headers.get('location'); | ||
| if (redirectUrl) return redirectUrl; | ||
| } | ||
|
|
||
| if (response.ok) return initialUrl; | ||
|
|
||
| throw new Error(`Server responded with ${response.status}`); | ||
| } catch (error) { | ||
| // If redirect: 'manual' is used, fetch might throw a 'TypeError' | ||
| // when it hits the redirect—this is actually what we want to catch. | ||
| console.error("Fetch encountered the redirect/error:", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| export class CommandsManager { | ||
| static activeGodleyWindowItems = new Map<string, CanvasItem>(); | ||
|
|
@@ -1137,6 +1198,7 @@ export class CommandsManager { | |
|
|
||
| // handler for downloading Ravel and installing it | ||
| static downloadRavel(event,item,webContents) { | ||
|
|
||
| switch (process.platform) { | ||
| case 'win32': | ||
| const savePath=dirname(process.execPath)+'/libravel.dll'; | ||
|
|
@@ -1158,7 +1220,7 @@ export class CommandsManager { | |
| // handler for when download completed | ||
| item.once('done', (event,state)=>{ | ||
| progress.close(); | ||
|
|
||
| if (state==='completed') { | ||
| dialog.showMessageBoxSync(WindowManager.getMainWindow(),{ | ||
| message: 'Ravel plugin updated successfully - restart Ravel to use', | ||
|
|
@@ -1308,6 +1370,44 @@ export class CommandsManager { | |
| modal: false, | ||
| }); | ||
| } | ||
|
|
||
| // return information about the current system | ||
| static async buildState(previous: boolean) { | ||
| // need to pass what platform we are | ||
| let state; | ||
| switch (process.platform) { | ||
| case 'win32': | ||
| state={system: 'windows', distro: '', version: '', arch:'', previous: ''}; | ||
| break; | ||
| case 'darwin': | ||
| state={system: 'macos', distro: '', version: '', arch: `${process.arch}`, previous: ''}; | ||
| break; | ||
| case 'linux': { | ||
| state={system: 'linux', distro: '', version: '',arch:'', previous: ''}; | ||
| // figure out distro and version from /etc/os-release | ||
| let aexec=promisify(exec); | ||
| let osRelease='/etc/os-release'; | ||
| if (existsSync(process.resourcesPath+'/os-release')) | ||
| osRelease=process.resourcesPath+'/os-release'; | ||
| let distroInfo=await aexec(`grep ^ID= ${osRelease}`); | ||
| // value may or may not be quoted | ||
| let extractor=/.*=['"]?([^'"\n]*)['"]?/; | ||
| state.distro=extractor.exec(distroInfo.stdout)[1]; | ||
| distroInfo=await aexec(`grep ^VERSION_ID= ${osRelease}`); | ||
| state.version=extractor.exec(distroInfo.stdout)[1]; | ||
| break; | ||
| } | ||
| default: | ||
| dialog.showMessageBoxSync(WindowManager.getMainWindow(),{ | ||
| message: `In app update is not available for your operating system yet, please check back later`, | ||
| type: 'error', | ||
| }); | ||
| return null; | ||
| } | ||
| if (await minsky.ravelAvailable() && previous) | ||
| state.previous=/[^:]*/.exec(await minsky.ravelVersion())[0]; | ||
| return state; | ||
| } | ||
|
|
||
| static async upgrade(installCase: InstallCase=InstallCase.theLot) { | ||
| const window=this.createDownloadWindow(); | ||
|
|
@@ -1344,7 +1444,7 @@ export class CommandsManager { | |
| } | ||
| if (ravelFile) { | ||
| // currently on latest, so reinstall ravel | ||
| window.webContents.session.on('will-download',this.downloadRavel); | ||
| window.webContents.session.on('will-download',this.downloadRavel); | ||
| window.webContents.downloadURL(ravelFile); | ||
| return; | ||
| } | ||
|
|
@@ -1357,44 +1457,87 @@ export class CommandsManager { | |
| } | ||
| }); | ||
|
|
||
| let clientId='-PiL7snNmZL_BlLJTPm62SHBcFTMG5d46m2336r118mfrp6sz4ty0g-thbKAs76c'; | ||
| // need to pass what platform we are | ||
| let state; | ||
| switch (process.platform) { | ||
| case 'win32': | ||
| state={system: 'windows', distro: '', version: '', arch:'', previous: ''}; | ||
| break; | ||
| case 'darwin': | ||
| state={system: 'macos', distro: '', version: '', arch: `${process.arch}`, previous: ''}; | ||
| break; | ||
| case 'linux': | ||
| state={system: 'linux', distro: '', version: '',arch:'', previous: ''}; | ||
| // figure out distro and version from /etc/os-release | ||
| let aexec=promisify(exec); | ||
| let osRelease='/etc/os-release'; | ||
| if (existsSync(process.resourcesPath+'/os-release')) | ||
| osRelease=process.resourcesPath+'/os-release'; | ||
| let distroInfo=await aexec(`grep ^ID= ${osRelease}`); | ||
| // value may or may not be quoted | ||
| let extractor=/.*=['"]?([^'"\n]*)['"]?/; | ||
| state.distro=extractor.exec(distroInfo.stdout)[1]; | ||
| distroInfo=await aexec(`grep ^VERSION_ID= ${osRelease}`); | ||
| state.version=extractor.exec(distroInfo.stdout)[1]; | ||
| break; | ||
| default: | ||
| dialog.showMessageBoxSync(WindowManager.getMainWindow(),{ | ||
| message: `In app update is not available for your operating system yet, please check back later`, | ||
| type: 'error', | ||
| }); | ||
| let state=await CommandsManager.buildState(installCase==InstallCase.previousRavel); | ||
| if (!state) { | ||
| window.close(); | ||
| return; | ||
| break; | ||
| } | ||
| if (await minsky.ravelAvailable() && installCase===InstallCase.previousRavel) | ||
| state.previous=/[^:]*/.exec(await minsky.ravelVersion())[0]; | ||
| let clientId='-PiL7snNmZL_BlLJTPm62SHBcFTMG5d46m2336r118mfrp6sz4ty0g-thbKAs76c'; | ||
| let encodedState=encodeURI(JSON.stringify(state)); | ||
| // load patreon's login page | ||
| window.loadURL(`https://www.patreon.com/oauth2/authorize?response_type=code&client_id=${clientId}&redirect_uri=https://ravelation.net/ravel-downloader.cgi&scope=identity%20identity%5Bemail%5D&state=${encodedState}`); | ||
| } | ||
|
|
||
|
|
||
| // gets release URL for current system from Ravelation.net backend | ||
| static async getRelease(product: string, previous: boolean, token: string) { | ||
| let state=await CommandsManager.buildState(previous); | ||
| if (!state) return ''; | ||
| let query=`product=${product}&os=${state.system}&arch=${state.arch}&distro=${state.distro}&distro_version=${state.version}`; | ||
| if (previous) { | ||
| let releases=JSON.parse(await callBackendAPI(`${backendAPI}/releases?${query}`, token)); | ||
| let prevRelease; | ||
| for (let release of releases) | ||
| if (semVerLess(release.version, state.previous)) | ||
| prevRelease=release; | ||
| if (prevRelease) return prevRelease.download_url; | ||
| // if not, then treat the request as latest | ||
| } | ||
| let release=JSON.parse(await callBackendAPI(`${backendAPI}/releases/latest?${query}`, token)); | ||
| return release?.release?.download_url; | ||
| } | ||
|
|
||
| static stashClerkToken(token: string) { | ||
| if (token) { | ||
| if (safeStorage.isEncryptionAvailable()) { | ||
| const encrypted = safeStorage.encryptString(token); | ||
| StoreManager.store.set('authToken', encrypted.toString('latin1')); | ||
| } else | ||
| // fallback: store plaintext | ||
| StoreManager.store.set('authToken', token); | ||
| } else { | ||
| StoreManager.store.delete('authToken'); | ||
| } | ||
highperformancecoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| static async upgradeUsingClerk(installCase: InstallCase=InstallCase.theLot) { | ||
| while (!StoreManager.store.get('authToken')) | ||
| if (!await WindowManager.openLoginWindow()) return; | ||
|
|
||
| let token=StoreManager.store.get('authToken'); | ||
|
Comment on lines
+1503
to
+1507
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let the user cancel login.
↩️ Suggested fix- while (!StoreManager.store.get('authToken'))
- await WindowManager.openLoginWindow();
- let token=StoreManager.store.get('authToken');
+ let token=StoreManager.store.get('authToken');
+ while (!token) {
+ token=await WindowManager.openLoginWindow();
+ if (!token) return;
+ }🤖 Prompt for AI Agents |
||
| // decrypt token if encrypted | ||
| if (safeStorage.isEncryptionAvailable()) | ||
| token=safeStorage.decryptString(Buffer.from(token, 'latin1')); | ||
highperformancecoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const window=WindowManager.getMainWindow(); | ||
| let minskyAsset; | ||
| try { | ||
| if (installCase===InstallCase.theLot) | ||
| minskyAsset=await CommandsManager.getRelease('minsky', false, token); | ||
| let ravelAsset=await CommandsManager.getRelease('ravel', installCase===InstallCase.previousRavel, token); | ||
|
|
||
| if (minskyAsset) { | ||
| if (ravelAsset) { // stash ravel upgrade to be installed on next startup | ||
| StoreManager.store.set('ravelPlugin',await getFinalUrl(ravelAsset,token)); | ||
| } | ||
| window.webContents.session.on('will-download',this.downloadMinsky); | ||
| window.webContents.downloadURL(await getFinalUrl(minskyAsset,token)); | ||
| return; | ||
| } else if (ravelAsset) { | ||
| window.webContents.session.on('will-download',this.downloadRavel); | ||
| window.webContents.downloadURL(await getFinalUrl(ravelAsset,token)); | ||
| return; | ||
| } | ||
| dialog.showMessageBoxSync(WindowManager.getMainWindow(),{ | ||
| message: "Everything's up to date, nothing to do.\n"+ | ||
| "If you're trying to download the Ravel plugin, please ensure you are logged into an account subscribed to Ravel Fan or Explorer tiers.", | ||
| type: 'info', | ||
| }); | ||
| } | ||
| catch (error) { | ||
| dialog.showErrorBox('Error', error.toString()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,4 +1,5 @@ | ||
| export const AppConfig = { | ||
| production: true, | ||
| environment: 'PROD', | ||
| clerkPublishableKey: 'pk_test_cG9zaXRpdmUtcGhvZW5peC04NS5jbGVyay5hY2NvdW50cy5kZXYk', | ||
| }; |
This file contains hidden or 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,4 +1,5 @@ | ||
| export const AppConfig = { | ||
| production: false, | ||
| environment: 'LOCAL', | ||
| clerkPublishableKey: 'pk_test_cG9zaXRpdmUtcGhvZW5peC04NS5jbGVyay5hY2NvdW50cy5kZXYk', | ||
| }; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.