Skip to content

Commit

Permalink
Fix last commits of lissine
Browse files Browse the repository at this point in the history
  • Loading branch information
tmolitor-stud-tu committed Jan 4, 2025
1 parent 1bf01d4 commit 730ea5c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 14 deletions.
8 changes: 7 additions & 1 deletion Monal/Classes/ChangePassword.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
// Copyright © 2024 monal-im.org. All rights reserved.
//

class ChangePasswordDefaultsDB: ObservableObject {
@defaultsDB("Quicksy_autogeneratedPassword")
var autogeneratedPassword: Bool
}

struct ChangePassword: View {
@Environment(\.dismiss) private var dismiss

Expand All @@ -16,6 +21,7 @@ struct ChangePassword: View {
@State private var alertPrompt = AlertPrompt(dismissLabel: Text("Close"))

@StateObject private var overlay = LoadingOverlayState()
@ObservedObject var changePasswordDefaultsDB = ChangePasswordDefaultsDB()

let accountID: NSNumber

Expand Down Expand Up @@ -66,7 +72,7 @@ struct ChangePassword: View {
Form {
Section(header: Text("Enter your new password. Passwords may not be empty. They may also be governed by server or company policies.")) {
#if IS_QUICKSY
if HelperTools.defaultsDB().bool(forKey: "autogeneratedPassword") {
if changePasswordDefaultsDB.autogeneratedPassword {
TextField(NSLocalizedString("Current Password", comment: ""), text: $oldPass)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
Expand Down
1 change: 1 addition & 0 deletions Monal/Classes/MLConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static const DDLogLevel ddLogLevel = LOG_LEVEL_STDOUT;
#endif

#define kMonalKeychainName @"Monal"
#define kMonalTmpKeychainName @"Monal.tmp"

//this is in seconds
#if TARGET_OS_MACCATALYST
Expand Down
8 changes: 5 additions & 3 deletions Monal/Classes/MLIQProcessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -737,17 +737,18 @@ +(BOOL) processRosterWithAccount:(xmpp*) account andIqNode:(XMPPIQ*) iqNode
$$class_handler(handlePasswordChangeInvalidation, $$ID(xmpp*, account), $$ID(NSString*, uuid), $$ID(MLPromise*, promise))
NSString* jid = account.connectionProperties.identity.jid;
DDLogError(@"Could not change the password of '%@'", jid);
[SAMKeychain deletePasswordForService:uuid account:jid];
NSString* errorMessage = [NSString stringWithFormat:NSLocalizedString(@"Could not change the password of '%@'. Please try again.", @""), jid];
NSError* error = [NSError errorWithDomain:@"Monal" code:0 userInfo:@{NSLocalizedDescriptionKey: errorMessage}];
[promise reject:error];
[SAMKeychain deletePasswordForService:uuid account:jid];
$$

$$class_handler(handlePasswordChange, $$ID(XMPPIQ*, iqNode), $$ID(xmpp*, account), $$ID(NSString*, uuid), $$ID(MLPromise*, promise))
NSString* jid = account.connectionProperties.identity.jid;
if([iqNode check:@"/<type=error>"])
{
DDLogError(@"Changing the password of '%@' returned error: %@", jid, [iqNode findFirst:@"error"]);
[SAMKeychain deletePasswordForService:uuid account:jid];
NSString* errorMessage = [HelperTools extractXMPPError:iqNode withDescription:[NSString stringWithFormat:NSLocalizedString(@"Could not change the password of '%@'", @""), jid]];
NSError* error = [NSError errorWithDomain:@"Monal" code:0 userInfo:@{NSLocalizedDescriptionKey: errorMessage}];
[promise reject:error];
Expand All @@ -756,13 +757,14 @@ +(BOOL) processRosterWithAccount:(xmpp*) account andIqNode:(XMPPIQ*) iqNode
{
NSString* newPass = [SAMKeychain passwordForService:uuid account:jid];
[[MLXMPPManager sharedInstance] updatePassword:newPass forAccount: account.accountID];
[SAMKeychain deletePasswordForService:uuid account:jid];
#if IS_QUICKSY
[[HelperTools defaultsDB] setBool:NO forKey:@"autogeneratedPassword"];
[[HelperTools defaultsDB] setBool:NO forKey:@"Quicksy_autogeneratedPassword"];
#endif
DDLogInfo(@"Successfully changed the password of '%@'", jid);
[promise fulfill:nil];
}
[SAMKeychain deletePasswordForService:uuid account:jid];

$$

$$class_handler(handleVersionResponse, $$ID(xmpp*, account), $$ID(XMPPIQ*, iqNode))
Expand Down
4 changes: 2 additions & 2 deletions Monal/Classes/MLXMPPManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ -(id) init
_onMobile = NO;
_isConnectBlocked = NO;

[SAMKeychain setAccessibilityType:kSecAttrAccessibleAfterFirstUnlock];

[self defaultSettings];
[self setPushToken:nil]; //load push settings from defaultsDB (can be overwritten later on in mainapp, but *not* in appex)

Expand Down Expand Up @@ -634,7 +636,6 @@ -(void) connectIfNecessary

-(void) updatePassword:(NSString*) password forAccount:(NSNumber*) accountID
{
[SAMKeychain setAccessibilityType:kSecAttrAccessibleAfterFirstUnlock];
[SAMKeychain setPassword:password forService:kMonalKeychainName account:accountID.stringValue];
xmpp* xmpp = [self getEnabledAccountForID:accountID];
[xmpp.connectionProperties.identity updatPassword:password];
Expand Down Expand Up @@ -779,7 +780,6 @@ -(void) addNewAccountToKeychainAndConnectWithPassword:(NSString*) password andAc
{
if(accountID != nil && password != nil)
{
[SAMKeychain setAccessibilityType:kSecAttrAccessibleAfterFirstUnlock];
[SAMKeychain setPassword:password forService:kMonalKeychainName account:accountID.stringValue];
[self connectAccount:accountID];
}
Expand Down
5 changes: 4 additions & 1 deletion Monal/Classes/Quicksy_RegisterAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class Quicksy_State: ObservableObject {

@defaultsDB("Quicksy_country")
var country: Quicksy_Country?

@defaultsDB("Quicksy_autogeneratedPassword")
var autogeneratedPassword: Bool
}

struct Quicksy_RegisterAccount: View {
Expand Down Expand Up @@ -110,7 +113,7 @@ struct Quicksy_RegisterAccount: View {
startLoginTimeout()
showLoadingOverlay(overlay, headline:NSLocalizedString("Logging in", comment: ""))
self.errorObserverEnabled = true
HelperTools.defaultsDB().set(true, forKey: "autogeneratedPassword")
state.autogeneratedPassword = true
//check if account is already configured and reset its password and its enabled and needs_password_migration states
if let newAccountID = DataLayer.sharedInstance().accountID(forUser:number, andDomain:"quicksy.im") {
self.newAccountID = newAccountID
Expand Down
5 changes: 2 additions & 3 deletions Monal/Classes/XMPPEdit.m
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ -(IBAction) save:(id) sender
if(accountID != nil)
{
self.accountID = accountID;
[SAMKeychain setAccessibilityType:kSecAttrAccessibleAfterFirstUnlock];
[SAMKeychain setPassword:self.password forService:kMonalKeychainName account:self.accountID.stringValue];
if(self.enabled)
{
Expand Down Expand Up @@ -411,7 +410,7 @@ -(IBAction) save:(id) sender
[[MLXMPPManager sharedInstance] updatePassword:self.password forAccount:self.accountID];
#if IS_QUICKSY
if (self.passwordChanged)
[[HelperTools defaultsDB] setBool:NO forKey:@"autogeneratedPassword"];
[[HelperTools defaultsDB] setBool:NO forKey:@"Quicksy_autogeneratedPassword"];
#endif
}
if(self.enabled)
Expand Down Expand Up @@ -644,7 +643,7 @@ -(UITableViewCell*) tableView:(UITableView*) tableView cellForRowAtIndexPath:(NS
{
case SettingsChangePasswordRow: {
#ifdef IS_QUICKSY
if([[HelperTools defaultsDB] boolForKey:@"autogeneratedPassword"])
if([[HelperTools defaultsDB] boolForKey:@"Quicksy_autogeneratedPassword"])
[thecell initTapCell:NSLocalizedString(@"Change/View Password", @"")];
else
[thecell initTapCell:NSLocalizedString(@"Change Password", @"")];
Expand Down
6 changes: 2 additions & 4 deletions Monal/Classes/xmpp.m
Original file line number Diff line number Diff line change
Expand Up @@ -4643,11 +4643,9 @@ -(AnyPromise*) changePassword:(NSString*) newPass
[iqNode changePasswordForUser:self.connectionProperties.identity.user newPassword:newPass];

//temporarily store the new password in the keychain.
//this way, we don't store the password in the db if the app is put
//in the background while awaiting the iq result.
//this way, we don't store the password in the db when serializing the handler
NSString* uuid = [[NSUUID UUID] UUIDString];
[SAMKeychain setAccessibilityType:kSecAttrAccessibleAfterFirstUnlock];
[SAMKeychain setPassword:newPass forService:uuid account:self.connectionProperties.identity.jid];
[SAMKeychain setPassword:newPass forService:kMonalTmpKeychainName account:uuid];

[self sendIq:iqNode withHandler:$newHandlerWithInvalidation(MLIQProcessor, handlePasswordChange,handlePasswordChangeInvalidation, $ID(uuid), $ID(promise))];
return [promise toAnyPromise];
Expand Down

0 comments on commit 730ea5c

Please sign in to comment.