NEWS
Adapter Passwort entschlüsseln
-
Hallo Zusammen,
Ich finde den ioBroker absolut super cool.
Nun möchte ich gerne mal versuchen, einen eigenen Adapter zu schreiben.
Ich bin blutiger Anfänger in der Adapterentwicklung.Der Adapter soll im Mode "schedule" laufen.
Die Adapterkonfig beinhaltet unter anderem ein Passwort das ich verschlüsselt speichere, was soweit auch funktioniert.
Nun muss das Passwort beim Aufruf des Adapters entschlüsselt werden.
Dies gelingt mir im Moment nicht, deshalb die Frage an euch Experte hier.
Wie müsste die Anpassung in der main.js sein, damit mir das Entschlüssen gelingt?
Inhalt der main.js:
* jshint -W097 */ /* jshint strict: false */ /* jslint node: true */ 'use strict'; const utils = require('@iobroker/adapter-core'); const adapterName = require('./package.json').name.split('.').pop(); class modeshedule extends utils.Adapter { constructor(options) { super({ ...options, name: adapterName, }); this.killTimeout = null; this.on('ready', this.onReady.bind(this)); this.on('unload', this.onUnload.bind(this)); } // Fuktion um das Passwort zu entschlüssen function decrypt(key, value) { let result = ""; for (let i = 0; i < value.length; ++i) { result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i)); } this.log.debug("password decrypt ready"); return result; } async onReady() { this.log.info("onReady wird aufgerufen"); this.getForeignObject("system.config", (err, obj) => { if (obj && obj.native && obj.native.secret) { //noinspection JSUnresolvedVariable this.config.password = decrypt(obj.native.secret, this.config.password); } else { //noinspection JSUnresolvedVariable this.config.password = decrypt("Zgfr56gFe87jJOM", this.config.password); } let pass = this.config.password; this.log.info("Passwort lautet : " + pass); this.killTimeout = setTimeout(this.stop.bind(this), 5000); } onUnload(callback) { try { if (this.killTimeout) { this.log.debug('clearing kill timeout'); clearTimeout(this.killTimeout); } this.log.debug('cleaned everything up...'); callback(); } catch (e) { callback(); } } } // @ts-ignore parent is a valid property on module if (module.parent) { // Export the constructor in compact mode /** * @param {Partial<ioBroker.AdapterOptions>} [options={}] */ module.exports = (options) => new modeshedule(options); } else { // otherwise start the instance directly new modeshedule(); }
-
@652145 Du musst nichts selber verschlüsseln oder entschlüsseln.
Setze in der io-package.json einfach folgende Einträge:
"encryptedNative": ["meinFeldDasVerschlüsseltWerdenSoll"], "protectedNative": ["meinFeldDasVerschlüsseltWerdenSoll"],
Ein Beispiel findest du hier.
Dazu solltest du noch die js-controller dependency auf minimum 3.1 setzen (wenn ich mich nicht irre - das fehlt im obigen Beispiel noch).
-
@unclesam Deine Änderungen haben auf Anhieb funktioniert.
Vielen Dank für die schnelle Hilfe.
-
@UncleSam ist das richtig, wenn ich das über den js-Controller verschlüsseln lasse, dass beim download der config, dass das pw im klartext da steht? oder hab ich noch irgendwo einen fehler?
https://github.com/iobroker-community-adapters/ioBroker.wolf-smartset