Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial tests on the authentication service. #6229

Merged
merged 2 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

import Foundation

protocol AuthenticationRestClient {
protocol AuthenticationRestClient: AnyObject {
// MARK: Configuration
var credentials: MXCredentials! { get }
var homeserver: String! { get }
var identityServer: String! { get }
var credentials: MXCredentials! { get }
var acceptableContentTypes: Set<String>! { get set }
stefanceriu marked this conversation as resolved.
Show resolved Hide resolved

init(homeServer: URL, unrecognizedCertificateHandler handler: MXHTTPClientOnUnrecognizedCertificate?)

// MARK: Login
var loginFallbackURL: URL { get }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class AuthenticationService: NSObject {

// MARK: Private

/// The rest client used to make authentication requests.
private var client: AuthenticationRestClient
/// The object used to create a new `MXSession` when authentication has completed.
private var sessionCreator = SessionCreator()
private var sessionCreator: SessionCreatorProtocol

// MARK: Public

/// The current state of the authentication flow.
private(set) var state: AuthenticationState
/// The rest client used to make authentication requests.
private(set) var client: AuthenticationRestClient
/// The current login wizard or `nil` if `startFlow` hasn't been called.
private(set) var loginWizard: LoginWizard?
/// The current registration wizard or `nil` if `startFlow` hasn't been called for `.registration`.
Expand All @@ -53,16 +53,21 @@ class AuthenticationService: NSObject {
/// The authentication service's delegate.
weak var delegate: AuthenticationServiceDelegate?

/// The type of client to use during the flow.
var clientType: AuthenticationRestClient.Type = MXRestClient.self

// MARK: - Setup

override init() {
init(sessionCreator: SessionCreatorProtocol = SessionCreator()) {
guard let homeserverURL = URL(string: BuildSettings.serverConfigDefaultHomeserverUrlString) else {
MXLog.failure("[AuthenticationService]: Failed to create URL from default homeserver URL string.")
fatalError("Invalid default homeserver URL string.")
}

state = AuthenticationState(flow: .login, homeserverAddress: BuildSettings.serverConfigDefaultHomeserverUrlString)
client = MXRestClient(homeServer: homeserverURL, unrecognizedCertificateHandler: nil)
client = clientType.init(homeServer: homeserverURL, unrecognizedCertificateHandler: nil)

self.sessionCreator = sessionCreator

super.init()
}
Expand Down Expand Up @@ -96,12 +101,12 @@ class AuthenticationService: NSObject {
func startFlow(_ flow: AuthenticationFlow, for homeserverAddress: String) async throws {
var (client, homeserver) = try await loginFlow(for: homeserverAddress)

let loginWizard = LoginWizard(client: client)
let loginWizard = LoginWizard(client: client, sessionCreator: sessionCreator)
self.loginWizard = loginWizard

if flow == .register {
do {
let registrationWizard = RegistrationWizard(client: client)
let registrationWizard = RegistrationWizard(client: client, sessionCreator: sessionCreator)
homeserver.registrationFlow = try await registrationWizard.registrationFlow()
self.registrationWizard = registrationWizard
} catch {
Expand Down Expand Up @@ -193,7 +198,7 @@ class AuthenticationService: NSObject {
}

#warning("Add an unrecognized certificate handler.")
let client = MXRestClient(homeServer: homeserverURL, unrecognizedCertificateHandler: nil)
let client = clientType.init(homeServer: homeserverURL, unrecognizedCertificateHandler: nil)

let loginFlow = try await getLoginFlowResult(client: client)

Expand All @@ -219,7 +224,7 @@ class AuthenticationService: NSObject {
return (client, homeserver)
}

private func getLoginFlowResult(client: MXRestClient) async throws -> LoginFlowResult {
private func getLoginFlowResult(client: AuthenticationRestClient) async throws -> LoginFlowResult {
// Get the login flow
let loginFlowResponse = try await client.getLoginSession()

Expand All @@ -231,7 +236,7 @@ class AuthenticationService: NSObject {

/// Perform a well-known request on the specified homeserver URL.
private func wellKnown(for homeserverURL: URL) async throws -> MXWellKnown {
let wellKnownClient = MXRestClient(homeServer: homeserverURL, unrecognizedCertificateHandler: nil)
let wellKnownClient = clientType.init(homeServer: homeserverURL, unrecognizedCertificateHandler: nil)

// The .well-known/matrix/client API is often just a static file returned with no content type.
// Make our HTTP client compatible with this behaviour
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class LoginWizard {
}

let client: AuthenticationRestClient
let sessionCreator: SessionCreator
let sessionCreator: SessionCreatorProtocol

private(set) var state: State

init(client: AuthenticationRestClient, sessionCreator: SessionCreator = SessionCreator()) {
init(client: AuthenticationRestClient, sessionCreator: SessionCreatorProtocol) {
self.client = client
self.sessionCreator = sessionCreator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import Foundation

/// The parameters used for registration requests.
struct RegistrationParameters: DictionaryEncodable {
struct RegistrationParameters: DictionaryEncodable, Equatable {
/// Authentication parameters
var auth: AuthenticationParameters?

Expand All @@ -44,7 +44,7 @@ struct RegistrationParameters: DictionaryEncodable {
}

/// The data passed to the `auth` parameter in authentication requests.
struct AuthenticationParameters: Encodable {
struct AuthenticationParameters: Encodable, Equatable {
/// The type of authentication taking place. The identifier from `MXLoginFlowType`.
let type: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class RegistrationWizard {
}

let client: AuthenticationRestClient
let sessionCreator: SessionCreator
let sessionCreator: SessionCreatorProtocol

private(set) var state: State

Expand All @@ -59,7 +59,7 @@ class RegistrationWizard {
state.isRegistrationStarted
}

init(client: AuthenticationRestClient, sessionCreator: SessionCreator = SessionCreator()) {
init(client: AuthenticationRestClient, sessionCreator: SessionCreatorProtocol) {
self.client = client
self.sessionCreator = sessionCreator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@

import Foundation

/// A WIP class that has common functionality to create a new session.
class SessionCreator {
protocol SessionCreatorProtocol {
/// Creates an `MXSession` using the supplied credentials and REST client.
/// - Parameters:
/// - credentials: The `MXCredentials` for the account.
/// - client: The client that completed the authentication.
/// - Returns: A new `MXSession` for the account.
func createSession(credentials: MXCredentials, client: AuthenticationRestClient) -> MXSession
}

/// A struct that provides common functionality to create a new session.
struct SessionCreator: SessionCreatorProtocol {
func createSession(credentials: MXCredentials, client: AuthenticationRestClient) -> MXSession {
// Report the new account in account manager
if credentials.identityServer == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ enum RegisterThreePID {
case msisdn(msisdn: String, countryCode: String)
}

struct ThreePIDCredentials: Codable {
struct ThreePIDCredentials: Codable, Equatable {
var clientSecret: String?

var identityServer: String?
Expand Down
Loading