Add first refresh token implementation pass. Account credential persistence and NotificationService handling.

This commit is contained in:
David Langley 2021-12-09 17:28:56 +00:00
parent 6ce3c93b9b
commit 2fbd129456
5 changed files with 48 additions and 3 deletions

View file

@ -344,6 +344,9 @@ final class BuildSettings: NSObject {
static let authScreenShowForgotPassword = true
static let authScreenShowCustomServerOptions = true
// MARK: - Authentication Options
static let authEnableRefreshTokens = true
// MARK: - Unified Search
static let unifiedSearchScreenShowPublicDirectory = true

View file

@ -76,6 +76,8 @@ class CommonConfiguration: NSObject, Configurable {
sdkOptions.enableKeyBackupWhenStartingMXCrypto = false
sdkOptions.clientPermalinkBaseUrl = BuildSettings.clientPermalinkBaseUrl
sdkOptions.authEnableRefreshTokens = BuildSettings.authEnableRefreshTokens
// Configure key provider delegate
MXKeyProvider.sharedInstance().delegate = EncryptionKeyManager.shared
}

View file

@ -155,7 +155,7 @@ class UserSessionsService: NSObject {
let isSessionStateValid: Bool
switch mxSession.state {
case .closed, .unknownToken:
case .closed, .unauthenticated:
isSessionStateValid = false
default:
isSessionStateValid = true

View file

@ -1771,7 +1771,7 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
{
isPauseRequested = NO;
}
else if (mxSession.state == MXSessionStateUnknownToken)
else if (mxSession.state == MXSessionStateUnauthenticated)
{
// Logout this account
[[MXKAccountManager sharedManager] removeAccount:self completion:nil];
@ -2225,4 +2225,29 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
}
}
#pragma mark - Homeserver Access/Refresh Token updates
- (void)registerRestClientDidRefreshTokensNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRestClientDidRefreshTokensNotification:) name:MXRestClientDidRefreshTokensNotification object:nil];
}
- (void)handleRestClientDidRefreshTokensNotification:(NSNotification*)notification
{
NSDictionary *userInfo = notification.userInfo;
NSString *userId = userInfo[MXIdentityServiceNotificationUserIdKey];
NSString *identityServer = userInfo[MXIdentityServiceNotificationIdentityServerKey];
NSString *accessToken = userInfo[MXIdentityServiceNotificationAccessTokenKey];
if (userId && identityServer && accessToken && [mxCredentials.identityServer isEqualToString:identityServer])
{
mxCredentials.identityServerAccessToken = accessToken;
// Archive updated field
[[MXKAccountManager sharedManager] saveAccounts];
}
}
@end

View file

@ -56,8 +56,23 @@ class NotificationService: UNNotificationServiceExtension {
guard let userAccount = userAccount else {
return nil
}
return MXRestClient(credentials: userAccount.mxCredentials, unrecognizedCertificateHandler: nil)
let restClient = MXRestClient(credentials: userAccount.mxCredentials, unrecognizedCertificateHandler: nil)
restClient.refreshTokensFailedHandler = { mxError in
MXLog.debug("[NotificationService] mxRestClient: The rest client is no longer authenticated.")
if let mxError = mxError,
mxError.httpResponse.statusCode == 401,
let softLogout = mxError.userInfo[kMXErrorSoftLogoutKey] as? Bool,
softLogout {
MXLog.debug("[NotificationService] mxRestClient: soft logout");
userAccount.softLogout()
} else {
MXLog.debug("[NotificationService] mxRestClient: full logout");
MXKAccountManager.shared().removeAccount(userAccount, completion: nil)
}
}
return restClient
}()
private static var isLoggerInitialized: Bool = false
private lazy var pushGatewayRestClient: MXPushGatewayRestClient = {
let url = URL(string: BuildSettings.serverConfigSygnalAPIUrlString)!