@@ -123,35 +123,36 @@ export default class LNC {
123123 this . salt = '' ;
124124 this . testCipher = '' ;
125125
126- // load salt and testCipher from localStorage or generate new ones
127- if ( localStorage . getItem ( `lnc-web:${ this . _namespace } :salt` ) ) {
128- this . salt =
129- localStorage . getItem ( `lnc-web:${ this . _namespace } :salt` ) || '' ;
130- } else if ( ! this . _onLocalPrivCreate && ! this . _onRemoteKeyReceive ) {
131- this . salt = generateSalt ( ) ;
132- localStorage . setItem ( `lnc-web:${ this . _namespace } :salt` , this . salt ) ;
133- }
134-
135- if ( localStorage . getItem ( `lnc-web:${ this . _namespace } :testCipher` ) ) {
136- this . testCipher =
137- localStorage . getItem ( `lnc-web:${ this . _namespace } :testCipher` ) ||
138- '' ;
139- } else if ( ! this . _onLocalPrivCreate && ! this . _onRemoteKeyReceive ) {
140- this . testCipher = createTestCipher ( this . _password , this . salt ) ;
141- localStorage . setItem (
142- `lnc-web:${ this . _namespace } :testCipher` ,
143- this . testCipher
144- ) ;
145- }
146-
147- // save pairingPhrase to localStorage for backwards compatibility
148- if ( this . _pairingPhrase ) {
149- localStorage . setItem (
150- `lnc-web:${ this . _namespace } :pairingPhrase` ,
151- this . _password
152- ? encrypt ( this . _pairingPhrase , this . _password , this . salt )
153- : this . _pairingPhrase
154- ) ;
126+ // don't load the salt & cipher if a password is not provided. the
127+ // storing will be done when a password is set
128+ if ( this . _password ) {
129+ if ( localStorage . getItem ( `lnc-web:${ this . _namespace } :salt` ) ) {
130+ this . salt =
131+ localStorage . getItem ( `lnc-web:${ this . _namespace } :salt` ) || '' ;
132+ } else if ( ! this . _onLocalPrivCreate && ! this . _onRemoteKeyReceive ) {
133+ this . salt = generateSalt ( ) ;
134+ localStorage . setItem ( `lnc-web:${ this . _namespace } :salt` , this . salt ) ;
135+ }
136+
137+ if ( localStorage . getItem ( `lnc-web:${ this . _namespace } :testCipher` ) ) {
138+ this . testCipher =
139+ localStorage . getItem ( `lnc-web:${ this . _namespace } :testCipher` ) ||
140+ '' ;
141+ } else if ( ! this . _onLocalPrivCreate && ! this . _onRemoteKeyReceive ) {
142+ this . testCipher = createTestCipher ( this . _password , this . salt ) ;
143+ localStorage . setItem (
144+ `lnc-web:${ this . _namespace } :testCipher` ,
145+ this . testCipher
146+ ) ;
147+ }
148+
149+ // save pairingPhrase to localStorage for backwards compatibility
150+ if ( this . _pairingPhrase ) {
151+ localStorage . setItem (
152+ `lnc-web:${ this . _namespace } :pairingPhrase` ,
153+ encrypt ( this . _pairingPhrase , this . _password , this . salt )
154+ ) ;
155+ }
155156 }
156157
157158 // TODO: pull Go off of the global state
@@ -200,8 +201,68 @@ export default class LNC {
200201 ) ;
201202 }
202203
204+ /**
205+ * Returns `true` if this client had previously connected with a pairing phrase. If `true`
206+ * then you do not need to supply a pairing phrase to reconnect, only a password.
207+ */
208+ get isPaired ( ) {
209+ const hasRemoteKey = ! ! localStorage . getItem (
210+ `lnc-web:${ this . _namespace } :remoteKey`
211+ ) ;
212+ const hasPhrase = ! ! localStorage . getItem (
213+ `lnc-web:${ this . _namespace } :pairingPhrase`
214+ ) ;
215+ return hasRemoteKey || hasPhrase ;
216+ }
217+
203218 setPairingPhrase ( pairingPhrase : string ) {
204219 this . _pairingPhrase = pairingPhrase ;
220+
221+ // store the new pairing phrase if it is not already stored
222+ if ( ! this . isPaired ) {
223+ localStorage . setItem (
224+ `lnc-web:${ this . _namespace } :pairingPhrase` ,
225+ this . _password
226+ ? encrypt ( this . _pairingPhrase , this . _password , this . salt )
227+ : this . _pairingPhrase
228+ ) ;
229+ }
230+ }
231+
232+ /**
233+ * Sets the password that will be used to encrypt/decrypt sensitive data
234+ * @param password the new or previously used password
235+ */
236+ setPassword ( password : string ) {
237+ this . _password = password ;
238+
239+ // re generate the salt & cipher if the client hasn't been paired
240+ if ( ! this . isPaired ) {
241+ this . clearStorage ( ) ;
242+ this . salt = generateSalt ( ) ;
243+ localStorage . setItem ( `lnc-web:${ this . _namespace } :salt` , this . salt ) ;
244+
245+ this . testCipher = createTestCipher ( this . _password , this . salt ) ;
246+ localStorage . setItem (
247+ `lnc-web:${ this . _namespace } :testCipher` ,
248+ this . testCipher
249+ ) ;
250+ if ( this . _pairingPhrase ) {
251+ localStorage . setItem (
252+ `lnc-web:${ this . _namespace } :pairingPhrase` ,
253+ this . _password
254+ ? encrypt ( this . _pairingPhrase , this . _password , this . salt )
255+ : this . _pairingPhrase
256+ ) ;
257+ }
258+ } else {
259+ // load the pre-existing salt and cipher
260+ this . salt =
261+ localStorage . getItem ( `lnc-web:${ this . _namespace } :salt` ) || '' ;
262+ this . testCipher =
263+ localStorage . getItem ( `lnc-web:${ this . _namespace } :testCipher` ) ||
264+ '' ;
265+ }
205266 }
206267
207268 setLocalKey ( localKey : string ) {
0 commit comments