@@ -12,6 +12,9 @@ import {
1212 partNativeIrc ,
1313 sendNativePrivmsg ,
1414 sendNativeRaw ,
15+ secureDeleteServerSecret ,
16+ secureGetServerSecret ,
17+ secureSetServerSecret ,
1518} from './irc-native'
1619
1720function hexToHSL ( hex : string ) : string {
@@ -160,10 +163,8 @@ type ServerSnapshot = Pick<
160163 | 'nickname'
161164 | 'username'
162165 | 'realName'
163- | 'password'
164166 | 'saslEnabled'
165167 | 'saslUsername'
166- | 'saslPassword'
167168 | 'autoJoinChannels'
168169 | 'onJoinCommands'
169170>
@@ -202,17 +203,33 @@ function saveServersSnapshot(servers: IRCServer[]) {
202203 nickname : s . nickname ,
203204 username : s . username ,
204205 realName : s . realName ,
205- password : s . password ,
206206 saslEnabled : s . saslEnabled ,
207207 saslUsername : s . saslUsername ,
208- saslPassword : s . saslPassword ,
209208 autoJoinChannels : s . autoJoinChannels ,
210209 onJoinCommands : s . onJoinCommands ,
211210 } ) )
212211 localStorage . setItem ( 'patchcord-servers' , JSON . stringify ( snapshot ) )
213212 } catch { }
214213}
215214
215+ function persistServerSecrets ( server : IRCServer ) {
216+ if ( ! isLiveBuild ) return
217+
218+ const pw = ( server . password || '' ) . trim ( )
219+ if ( pw . length > 0 ) {
220+ void secureSetServerSecret ( server . id , 'password' , pw ) . catch ( ( ) => { } )
221+ } else {
222+ void secureDeleteServerSecret ( server . id , 'password' ) . catch ( ( ) => { } )
223+ }
224+
225+ const saslPw = ( server . saslPassword || '' ) . trim ( )
226+ if ( saslPw . length > 0 ) {
227+ void secureSetServerSecret ( server . id , 'saslPassword' , saslPw ) . catch ( ( ) => { } )
228+ } else {
229+ void secureDeleteServerSecret ( server . id , 'saslPassword' ) . catch ( ( ) => { } )
230+ }
231+ }
232+
216233interface IRCStore {
217234 servers : IRCServer [ ]
218235 settings : AppSettings
@@ -382,6 +399,7 @@ export const useIRCStore = create<IRCStore>((set, get) => ({
382399 activeView : state . activeView . serverId ? state . activeView : { serverId : id , channelId : '' } ,
383400 }
384401 } )
402+ persistServerSecrets ( server )
385403 get ( ) . connectServer ( id )
386404 return
387405 }
@@ -470,12 +488,20 @@ export const useIRCStore = create<IRCStore>((set, get) => ({
470488 const servers = state . servers . map ( ( s ) =>
471489 s . id === serverId ? { ...s , ...data } : s
472490 )
491+ const updated = servers . find ( ( s ) => s . id === serverId )
492+ if ( updated ) {
493+ persistServerSecrets ( updated )
494+ }
473495 saveServersSnapshot ( servers )
474496 return { servers }
475497 } )
476498 } ,
477499
478500 removeServer : ( serverId ) => {
501+ if ( isLiveBuild ) {
502+ void secureDeleteServerSecret ( serverId , 'password' ) . catch ( ( ) => { } )
503+ void secureDeleteServerSecret ( serverId , 'saslPassword' ) . catch ( ( ) => { } )
504+ }
479505 set ( ( state ) => {
480506 const newServers = state . servers . filter ( ( s ) => s . id !== serverId )
481507 let newView = state . activeView
@@ -509,18 +535,21 @@ export const useIRCStore = create<IRCStore>((set, get) => ({
509535 if ( ! server ) return
510536 get ( ) . updateServerStatus ( serverId , 'connecting' )
511537 get ( ) . addServerMessage ( serverId , `Connecting to ${ server . host } :${ server . port } ${ server . ssl ? ' (TLS)' : '' } ...` )
512- void connectNativeIrc ( {
513- serverId,
514- host : server . host ,
515- port : server . port ,
516- ssl : server . ssl ,
517- allowInvalidCerts : ! ! server . allowInvalidCerts ,
518- nickname : server . nickname ,
519- username : server . username ,
520- realName : server . realName ,
521- password : server . password ,
522- autoJoinChannels : server . autoJoinChannels ,
523- } ) . catch ( ( err ) => {
538+ void ( async ( ) => {
539+ const storedPassword = await secureGetServerSecret ( serverId , 'password' ) . catch ( ( ) => null )
540+ await connectNativeIrc ( {
541+ serverId,
542+ host : server . host ,
543+ port : server . port ,
544+ ssl : server . ssl ,
545+ allowInvalidCerts : ! ! server . allowInvalidCerts ,
546+ nickname : server . nickname ,
547+ username : server . username ,
548+ realName : server . realName ,
549+ password : storedPassword ?? server . password ,
550+ autoJoinChannels : server . autoJoinChannels ,
551+ } )
552+ } ) ( ) . catch ( ( err ) => {
524553 get ( ) . updateServerStatus ( serverId , 'disconnected' )
525554 get ( ) . addServerMessage ( serverId , `Connection failed: ${ String ( err ) } ` )
526555 } )
0 commit comments