mirror of
https://github.com/vector-im/element-ios.git
synced 2024-09-28 15:22:39 +00:00
Add first refresh token implementation pass. Account credential persistence and NotificationService handling.
This commit is contained in:
parent
6ce3c93b9b
commit
2fbd129456
5 changed files with 48 additions and 3 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -155,7 +155,7 @@ class UserSessionsService: NSObject {
|
|||
let isSessionStateValid: Bool
|
||||
|
||||
switch mxSession.state {
|
||||
case .closed, .unknownToken:
|
||||
case .closed, .unauthenticated:
|
||||
isSessionStateValid = false
|
||||
default:
|
||||
isSessionStateValid = true
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)!
|
||||
|
|
Loading…
Reference in a new issue