Compare commits

..

No commits in common. "f25616328d16771e7aa2295e6a356af52e468db1" and "87a802e2106cfae892b93c936b21bc667101ebad" have entirely different histories.

2 changed files with 62 additions and 69 deletions

View File

@ -279,7 +279,6 @@ func nodeCreate(w http.ResponseWriter, r *http.Request) {// {{{
}) })
}// }}} }// }}}
func nodeUpdate(w http.ResponseWriter, r *http.Request) {// {{{ func nodeUpdate(w http.ResponseWriter, r *http.Request) {// {{{
log.Println("/node/update")
var err error var err error
var session Session var session Session

View File

@ -54,11 +54,7 @@ export class NodeUI extends Component {
let page = '' let page = ''
switch(this.page.value) { switch(this.page.value) {
case 'node': case 'node':
if(node.ID == 0) { if(node.ID > 0) {
page = html`
${children.length > 0 ? html`<div class="child-nodes">${children}</div>` : html``}
`
} else {
let padlock = '' let padlock = ''
if(node.CryptoKeyID > 0) if(node.CryptoKeyID > 0)
padlock = html`<img src="/images/${window._VERSION}/padlock-black.svg" style="height: 24px;" />` padlock = html`<img src="/images/${window._VERSION}/padlock-black.svg" style="height: 24px;" />`
@ -213,8 +209,7 @@ export class NodeUI extends Component {
}//}}} }//}}}
saveNode() {//{{{ saveNode() {//{{{
let content = this.nodeContent.current.contentDiv.current.value let content = this.nodeContent.current.contentDiv.current.value
this.node.value.setContent(content) this.node.value.save(content, ()=>this.props.app.nodeModified.value = false)
this.node.value.save(()=>this.props.app.nodeModified.value = false)
}//}}} }//}}}
renameNode() {//{{{ renameNode() {//{{{
let name = prompt("New name") let name = prompt("New name")
@ -304,7 +299,7 @@ class NodeContent extends Component {
textarea.parentNode.dataset.replicatedValue = textarea.value textarea.parentNode.dataset.replicatedValue = textarea.value
}//}}} }//}}}
unlock() {//{{{ unlock() {//{{{
let pass = prompt(`Password for "${this.props.model.description}"`) let pass = prompt("Password")
if(!pass) if(!pass)
return return
@ -402,21 +397,21 @@ export class Node {
}) })
.catch(this.app.responseError) .catch(this.app.responseError)
}//}}} }//}}}
async save(callback) {//{{{ async save(content, callback) {//{{{
try { let update_content = content
await this.#encrypt() /*
* XXX - fix encrypting when saving
if(this.CryptoKeyID != 0)
update_content = await this.#encrypt(content)
*/
let req = { this.app.request('/node/update', {
NodeID: this.ID, NodeID: this.ID,
Content: this._content, Content: update_content,
CryptoKeyID: this.CryptoKeyID, CryptoKeyID: this.CryptoKeyID,
} })
this.app.request('/node/update', req) .then(callback)
.then(callback) .catch(this.app.responseError)
.catch(this.app.responseError)
} catch (err) {
this.app.responseError(err)
}
}//}}} }//}}}
rename(name, callback) {//{{{ rename(name, callback) {//{{{
this.app.request('/node/rename', { this.app.request('/node/rename', {
@ -459,26 +454,30 @@ export class Node {
}) })
}//}}} }//}}}
content() {//{{{ content() {//{{{
if(this.CryptoKeyID != 0 && !this._decrypted) if(this.CryptoKeyID != 0 && !this._decrypted) {
this.#decrypt() this.#decrypt()
}
return this._content return this._content
}//}}} }//}}}
setContent(new_content) {//{{{
this._content = new_content async encrypt(obj_key) {//{{{
if(this.CryptoKeyID == 0) if(obj_key.ID != this.CryptoKeyID)
// Logic behind plaintext not being decrypted is that throw('Invalid key')
// only encrypted values can be in a decrypted state.
this._decrypted = false let crypto = new Crypto(obj_key.key)
else this._decrypted = false
this._decrypted = true
}//}}} let counter = await obj_key.counter()
async setCryptoKey(new_key) {//{{{
return this.#encrypt(true, new_key) this.content = sjcl.codec.base64.fromBits(
crypto.encrypt(
sjcl.codec.utf8String.toBits(this.content),
counter,
false,
)
)
}//}}} }//}}}
#decrypt() {//{{{ #decrypt() {//{{{
if(this.CryptoKeyID == 0 || this._decrypted)
return
let obj_key = this.app.nodeUI.current.getKey(this.CryptoKeyID) let obj_key = this.app.nodeUI.current.getKey(this.CryptoKeyID)
if(obj_key === null || obj_key.ID != this.CryptoKeyID) if(obj_key === null || obj_key.ID != this.CryptoKeyID)
throw('Invalid key') throw('Invalid key')
@ -486,7 +485,7 @@ export class Node {
// Ask user to unlock key first // Ask user to unlock key first
var pass = null var pass = null
while(pass || obj_key.status() == 'locked') { while(pass || obj_key.status() == 'locked') {
pass = prompt(`Password for "${obj_key.description}"`) pass = prompt("Password")
if(!pass) if(!pass)
throw new Error(`Key "${obj_key.description}" is locked`) throw new Error(`Key "${obj_key.description}" is locked`)
@ -507,25 +506,10 @@ export class Node {
crypto.decrypt(this._content) crypto.decrypt(this._content)
) )
}//}}} }//}}}
async #encrypt(change_key = false, new_key = null) {//{{{ /*
// Nothing to do if not changing key and already encrypted. async #encrypt(content) {//{{{
if(!change_key && this.CryptoKeyID != 0 && !this._decrypted) let obj_key = this.app.nodeUI.current.getKey(this.CryptoKeyID)
return this._content if(obj_key === null || obj_key.ID != this.CryptoKeyID)
let content = this.content()
// Changing key to no encryption or already at no encryption -
// set to not decrypted (only encrypted values can be
// decrypted) and return plain value.
if((change_key && new_key === null) || (!change_key && this.CryptoKeyID == 0)) {
this._decrypted = false
this.CryptoKeyID = 0
return content
}
let key_id = change_key ? new_key.ID : this.CryptoKeyID
let obj_key = this.app.nodeUI.current.getKey(key_id)
if(obj_key === null || obj_key.ID != key_id)
throw('Invalid key') throw('Invalid key')
if(obj_key.status() == 'locked') if(obj_key.status() == 'locked')
@ -534,11 +518,9 @@ export class Node {
let crypto = new Crypto(obj_key.key) let crypto = new Crypto(obj_key.key)
let content_bits = sjcl.codec.utf8String.toBits(content) let content_bits = sjcl.codec.utf8String.toBits(content)
let counter = await this.app.nodeUI.current.keyCounter() let counter = await this.app.nodeUI.current.keyCounter()
this.CryptoKeyID = obj_key.ID return crypto.encrypt(content_bits, counter, true)
this._content = crypto.encrypt(content_bits, counter, true)
this._decrypted = false
return this._content
}//}}} }//}}}
*/
} }
class Menu extends Component { class Menu extends Component {
@ -699,26 +681,38 @@ class NodeProperties extends Component {
</div> </div>
` `
}//}}} }//}}}
async save() {//{{{ save() {//{{{
let nodeui = this.props.nodeui let nodeui = this.props.nodeui
let node = nodeui.node.value let node = nodeui.node.value
// Find the actual key object used for encryption // Find the actual key object used for encryption
let new_key = nodeui.getKey(this.selected_key_id) let encrypt_key = nodeui.getKey(this.selected_key_id)
let current_key = nodeui.getKey(node.CryptoKeyID) let decrypt_key = nodeui.getKey(node.CryptoKeyID)
if(current_key && current_key.status() == 'locked') { if(decrypt_key && decrypt_key.status() == 'locked') {
alert("Decryption key is locked and can not be used.") alert("Decryption key is locked and can not be used.")
return return
} }
if(new_key && new_key.status() == 'locked') { if(encrypt_key && encrypt_key.status() == 'locked') {
alert("Key is locked and can not be used.") alert("Key is locked and can not be used.")
return return
} }
await node.setCryptoKey(new_key) // Currently not encrypted - encrypt with new key.
node.save(()=>this.props.nodeui.showPage('node')) let crypto = new Crypto(selected_key.key)
if(node.CryptoKeyID == 0) {
let encrypted = crypto.encrypt(
sjcl.codec.utf8String.toBits(node.Content()),
1n,
)
console.log(encrypted)
}
/*
crypto.encrypt(
)
*/
}//}}} }//}}}
} }