Skip to content

Instantly share code, notes, and snippets.

@zzhjerry
Last active January 18, 2023 02:38
Show Gist options
  • Save zzhjerry/2fdca96ef2e902f88262ead2c409fc88 to your computer and use it in GitHub Desktop.
Save zzhjerry/2fdca96ef2e902f88262ead2c409fc88 to your computer and use it in GitHub Desktop.
use keytar
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
</body>
</html>
// Modules to control application life and create native browser window
const {app, BrowserWindow, safeStorage, session} = require('electron')
const path = require('path')
const keytar = require('keytar')
const value = '{"app_id":1365,"user_id":2648053623837438,"user_id_str":"2648053623837438","odin_user_type":12,"name":"zzhjery","screen_name":"zzhjery","avatar_url":"https://p9-passport.byteacctimg.com/img/mosaic-legacy/3795/3047680722~120x256.image","user_verified":false,"email_collected":true,"phone_collected":true,"verified_content":"","verified_agency":"","is_blocked":0,"is_blocking":0,"bg_img_url":"https://p3-passport.byteacctimg.com/origin/60e0000116e1e21ff9a","gender":0,"media_id":0,"user_auth_info":"","industry":"","area":"","can_be_found_by_phone":1,"mobile":"187******07","birthday":"","description":"","email":"7***6@qq.com","new_user":0,"session_key":"b0c7428922149116b46dbb28a064c636","is_recommend_allowed":1,"recommend_hint_message":"同时推荐给我的粉丝","connects":[],"followings_count":0,"followers_count":0,"visit_count_recent":0,"skip_edit_profile":0,"is_manual_set_user_info":false,"device_id":0,"country_code":86,"has_password":1,"share_to_repost":0,"user_decoration":"","user_privacy_extend":0,"old_user_id":2648053623837438,"old_user_id_str":"2648053623837438","sec_user_id":"MS4wLjABAAAAw0GA9ecn8-J5ve3YM3D4WRgCvTSpsGOyXlcLjsUjP56NiMWWB1nQ2e109EFN9Mc5","sec_old_user_id":"MS4wLjABAAAAw0GA9ecn8-J5ve3YM3D4WRgCvTSpsGOyXlcLjsUjP56NiMWWB1nQ2e109EFN9Mc5","vcd_account":0,"vcd_relation":0,"can_bind_visitor_account":false,"is_visitor_account":false,"is_only_bind_ins":false,"user_device_record_status":0,"is_kids_mode":0,"is_employee":false,"passport_enterprise_user_type":0,"need_device_create":0,"need_ttwid_migration":0,"user_auth_status":0,"user_safe_mobile_2fa":"","safe_mobile_country_code":0,"lite_user_info_string":"","lite_user_info_demotion":0,"app_user_info":{"assist_register":false},"cookies":[{"name":"passport_csrf_token","value":"3117edeb87a5c9030b50b803729cd340","Path":"/","Domain":"toutiao.com","Max-Age":"5184000","SameSite":"None"},{"name":"passport_csrf_token_default","value":"3117edeb87a5c9030b50b803729cd340","Path":"/","Domain":"toutiao.com","Max-Age":"5184000"},{"name":"n_mh_microapp","value":"1NMkYQRzLs-RTk2sDM0FcYi8t2qreaqmtyYpxhGMrOY","Path":"/","Domain":"developer.toutiao.com","Max-Age":"10368000"},{"name":"sid_guard_microapp","value":"b0c7428922149116b46dbb28a064c636%7C1673580299%7C5183999%7CTue%2C+14-Mar-2023+03%3A24%3A58+GMT","Path":"/","Domain":"developer.toutiao.com","Max-Age":"31104000"},{"name":"uid_tt_microapp","value":"6aa348d9bc2c12b92c00ba827c740120","Path":"/","Domain":"developer.toutiao.com","Max-Age":"5183999"},{"name":"uid_tt_ss_microapp","value":"6aa348d9bc2c12b92c00ba827c740120","Path":"/","Domain":"developer.toutiao.com","Max-Age":"5183999","SameSite":"None"},{"name":"sid_tt_microapp","value":"b0c7428922149116b46dbb28a064c636","Path":"/","Domain":"developer.toutiao.com","Max-Age":"5183999"},{"name":"sessionid_microapp","value":"b0c7428922149116b46dbb28a064c636","Path":"/","Domain":"developer.toutiao.com","Max-Age":"5183999"},{"name":"sessionid_ss_microapp","value":"b0c7428922149116b46dbb28a064c636","Path":"/","Domain":"developer.toutiao.com","Max-Age":"5183999","SameSite":"None"},{"name":"sid_ucp_v1_microapp","value":"1.0.0-KGQxZmY0YzgyZDkxOWVlMjJiMTY2OWVhZTNmZjhjODBkOGE4YWJhMjEKFwj-peG8wIzaBBCLnoOeBhjVCjgBQOsHGgJscSIgYjBjNzQyODkyMjE0OTExNmI0NmRiYjI4YTA2NGM2MzY","Path":"/","Domain":"developer.toutiao.com","Max-Age":"5183999"},{"name":"ssid_ucp_v1_microapp","value":"1.0.0-KGQxZmY0YzgyZDkxOWVlMjJiMTY2OWVhZTNmZjhjODBkOGE4YWJhMjEKFwj-peG8wIzaBBCLnoOeBhjVCjgBQOsHGgJscSIgYjBjNzQyODkyMjE0OTExNmI0NmRiYjI4YTA2NGM2MzY","Path":"/","Domain":"developer.toutiao.com","Max-Age":"5183999","SameSite":"None"},{"name":"reg-store-region_microapp","value":"","Path":"/","Domain":"developer.toutiao.com","Max-Age":"0"},{"name":"store-region","value":"cn-zj","Path":"/","Domain":"toutiao.com","Max-Age":"31536000"},{"name":"store-region-src","value":"uid","Path":"/","Domain":"toutiao.com","Max-Age":"31536000"},{"name":"sessionid","value":"b0c7428922149116b46dbb28a064c636","Path":"/","Domain":"developer.toutiao.com","Max-Age":"5183999"},{"name":"sessionid_dy_open","value":"b0c7428922149116b46dbb28a064c636","Path":"/","Domain":"developer.toutiao.com","Max-Age":"5183999"}],"lastReviewTime":1673580296,"loginOverdueTime":1678764296889}'
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// 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.whenReady().then(async () => {
createWindow()
app.on('activate', function () {
// 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 (BrowserWindow.getAllWindows().length === 0) createWindow()
})
const buff = Buffer.from(value, 'utf-8')
const base64 = buff.toString('base64')
console.info('base64', base64.length)
// console.info('issafeavailablef', safeStorage.isEncryptionAvailable())
// const buf = safeStorage.encryptString(JSON.stringify({ 'hello': 'hello world' }))
// const bufString = buf.toString('hex')
// console.info(buf.toString('hex'))
// console.info(safeStorage.decryptString(Buffer.from(bufString, 'hex')))
const service = 'ide-auth-token'
const account = 'ide'
await keytar.setPassword(service, account, base64)
let token = await keytar.getPassword(service, account)
console.log('token', token)
if (!token) {
token = base64
await keytar.setPassword(service, account, `${token}`)
}
// const cookie = session.fromPartition('persist:auth', {
// cache: true
// }).cookies
// cookie.get({})
// .then((cookies) => {
// console.log(cookies, 'cookies now', Date.now())
// }).catch((error) => {
// console.log(error)
// })
// const data = { url: 'http://www.github.com', name: 'dummy_name', value: `${token}`, expirationDate: 9999999999999 }
// cookie.set(data).then(() => {
// // success
// cookie.get({ url: 'http://www.github.com' })
// .then((cookies) => {
// console.log(cookies, 'cookies now1', Date.now())
// }).catch((error) => {
// console.log(error)
// })
// }, (error) => {
// console.error(error)
// })
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
{
"name": "horrible-painting-thrust-x71pj",
"productName": "horrible-painting-thrust-x71pj",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "bytedance",
"scripts": {
"start": "electron ."
},
"dependencies": {
"keytar": "7.9.0"
},
"devDependencies": {
"electron": "19.0.0"
}
}
// All of the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
/* styles.css */
/* Add styles here to customize the appearance of your app */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment