Skip to content

Commit

Permalink
Merge pull request #6246 from vector-im/ismail/6177_wellknown_IS
Browse files Browse the repository at this point in the history
FTUE: Use the Identity server specified in the well-known
  • Loading branch information
ismailgulek authored Jun 6, 2022
2 parents a9beeac + 81410f0 commit 4bf15d7
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Riot/Modules/MatrixKit/Models/Account/MXKAccountManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ extern NSString *const MXKAccountManagerDataType;
*/
@interface MXKAccountManager : NSObject

/// Flag indicating that saving accounts enabled. Defaults to `YES`.
@property (nonatomic, assign, getter=isSavingAccountsEnabled) BOOL savingAccountsEnabled;

/**
The class of store used to open matrix session for the accounts. This class must be conformed to MXStore protocol.
By default this class is MXFileStore.
Expand Down
12 changes: 9 additions & 3 deletions Riot/Modules/MatrixKit/Models/Account/MXKAccountManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ - (instancetype)init
{
_storeClass = [MXFileStore class];
_dehydrationService = [MXDehydrationService new];
_savingAccountsEnabled = YES;

// Migrate old account file to new format
[self migrateAccounts];
Expand Down Expand Up @@ -108,10 +109,15 @@ - (void)prepareSessionForActiveAccounts
#pragma clang diagnostic ignored "-Wdeprecated"
- (void)saveAccounts
{
NSDate *startDate = [NSDate date];

MXLogDebug(@"[MXKAccountManager] saveAccounts...");


if (!self.isSavingAccountsEnabled)
{
MXLogDebug(@"[MXKAccountManager] saveAccounts: saving disabled.");
return;
}
NSDate *startDate = [NSDate date];

NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Foundation
protocol AuthenticationRestClient: AnyObject {
// MARK: Configuration
var homeserver: String! { get }
var identityServer: String! { get }
var identityServer: String! { get set }
var credentials: MXCredentials! { get }
var acceptableContentTypes: Set<String>! { get set }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,24 @@ class AuthenticationService: NSObject {
MXLog.error("[AuthenticationService] Unable to create a URL from the supplied homeserver address when calling loginFlow.")
throw AuthenticationError.invalidHomeserver
}

var identityServerURL: URL?

if let wellKnown = try? await wellKnown(for: homeserverURL),
let baseURL = URL(string: wellKnown.homeServer.baseUrl) {
homeserverURL = baseURL
if let wellKnown = try? await wellKnown(for: homeserverURL) {
if let baseURL = URL(string: wellKnown.homeServer.baseUrl) {
homeserverURL = baseURL
}
if let identityServer = wellKnown.identityServer,
let baseURL = URL(string: identityServer.baseUrl) {
identityServerURL = baseURL
}
}

#warning("Add an unrecognized certificate handler.")
let client = clientType.init(homeServer: homeserverURL, unrecognizedCertificateHandler: nil)
if let identityServerURL = identityServerURL {
client.identityServer = identityServerURL.absoluteString
}

let loginFlow = try await getLoginFlowResult(client: client)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ protocol SessionCreatorProtocol {

/// A struct that provides common functionality to create a new session.
struct SessionCreator: SessionCreatorProtocol {

private let accountManager: MXKAccountManager

init(withAccountManager accountManager: MXKAccountManager = .shared()) {
self.accountManager = accountManager
}

func createSession(credentials: MXCredentials, client: AuthenticationRestClient) -> MXSession {
// Report the new account in account manager
if credentials.identityServer == nil {
#warning("Check that the client is actually updated with this info?")
credentials.identityServer = client.identityServer
}

Expand All @@ -40,7 +46,7 @@ struct SessionCreator: SessionCreatorProtocol {
account.identityServerURL = identityServer
}

MXKAccountManager.shared().addAccount(account, andOpenSession: true)
accountManager.addAccount(account, andOpenSession: true)
return account.mxSession
}
}
49 changes: 49 additions & 0 deletions RiotTests/SessionCreatorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import XCTest
@testable import Riot

class SessionCreatorTests: XCTestCase {

func testIdentityServer() throws {
let sessionCreator = SessionCreator(withAccountManager: .mock)

let mockIS = "mock_identity_server"

let credentials = MXCredentials(homeServer: "mock_home_server",
userId: "mock_user_id",
accessToken: "mock_access_token")
let client = MXRestClient(credentials: credentials)
client.identityServer = mockIS
let session = sessionCreator.createSession(credentials: credentials, client: client)

XCTAssertEqual(credentials.identityServer, mockIS)
XCTAssertEqual(session.credentials.identityServer, mockIS)
XCTAssertEqual(session.identityService?.identityServer, mockIS)
}

}

private extension MXKAccountManager {

static var mock: MXKAccountManager {
let result = MXKAccountManager.shared()
result!.isSavingAccountsEnabled = false
return result!
}

}
1 change: 1 addition & 0 deletions changelog.d/6177.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AuthenticationService: Use identity server from well-known if provided when creating the client.

0 comments on commit 4bf15d7

Please sign in to comment.