Add AuthenticationRestClient protocol as an authentication interface to MXRestClient.

This commit is contained in:
Doug
2022-05-19 13:18:47 +01:00
committed by Doug
parent ac755f11f5
commit 59927e278c
9 changed files with 70 additions and 26 deletions
@@ -0,0 +1,45 @@
//
// 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 Foundation
protocol AuthenticationRestClient {
// MARK: Configuration
var credentials: MXCredentials! { get }
var identityServer: String! { get }
// MARK: Login
var loginFallbackURL: URL { get }
func wellKnown() async throws -> MXWellKnown
func getLoginSession() async throws -> MXAuthenticationSession
func login(parameters: LoginParameters) async throws -> MXCredentials
func login(parameters: [String : Any]) async throws -> MXCredentials
// MARK: Registration
var registerFallbackURL: URL { get }
func getRegisterSession() async throws -> MXAuthenticationSession
func isUsernameAvailable(_ username: String) async throws -> Bool
func register(parameters: RegistrationParameters) async throws -> MXLoginResponse
func register(parameters: [String : Any]) async throws -> MXLoginResponse
func requestTokenDuringRegistration(for threePID: RegisterThreePID, clientSecret: String, sendAttempt: UInt) async throws -> RegistrationThreePIDTokenResponse
// MARK: Forgot Password
func forgetPassword(for email: String, clientSecret: String, sendAttempt: UInt) async throws -> String
func resetPassword(parameters: CheckResetPasswordParameters) async throws
func resetPassword(parameters: [String : Any]) async throws
}
extension MXRestClient: AuthenticationRestClient { }
@@ -30,7 +30,7 @@ class AuthenticationService: NSObject {
// MARK: Private
/// The rest client used to make authentication requests.
private var client: MXRestClient
private var client: AuthenticationRestClient
/// The object used to create a new `MXSession` when authentication has completed.
private var sessionCreator = SessionCreator()
@@ -141,7 +141,7 @@ class AuthenticationService: NSObject {
let address = state.homeserver.addressFromUser ?? state.homeserver.address
self.state = AuthenticationState(flow: .login, homeserverAddress: address)
}
/// Create a session after a SSO successful login
func makeSessionFromSSO(credentials: MXCredentials) -> MXSession {
sessionCreator.createSession(credentials: credentials, client: client)
@@ -16,7 +16,7 @@
import Foundation
/// An `Encodable` type that can be used as the parameters of a login request.
/// A `DictionaryEncodable` type that can be used as the parameters of a login request.
protocol LoginParameters: DictionaryEncodable {
var type: String { get }
}
@@ -94,11 +94,10 @@ struct LoginPasswordParameters: LoginParameters {
}
}
/// The parameters used when checking the user has confirmed their email to reset their password.
/// The parameters used when checking that the user has confirmed their email in order to reset their password.
struct CheckResetPasswordParameters: DictionaryEncodable {
/// Authentication parameters
let auth: AuthenticationParameters
/// The new password
let newPassword: String
@@ -30,12 +30,12 @@ class LoginWizard {
var sendAttempt: UInt = 0
}
let client: MXRestClient
let client: AuthenticationRestClient
let sessionCreator: SessionCreator
private(set) var state: State
init(client: MXRestClient, sessionCreator: SessionCreator = SessionCreator()) {
init(client: AuthenticationRestClient, sessionCreator: SessionCreator = SessionCreator()) {
self.client = client
self.sessionCreator = sessionCreator
@@ -111,8 +111,8 @@ class LoginWizard {
}
let parameters = CheckResetPasswordParameters(clientSecret: state.clientSecret,
sessionID: resetPasswordData.addThreePIDSessionID,
newPassword: resetPasswordData.newPassword)
sessionID: resetPasswordData.addThreePIDSessionID,
newPassword: resetPasswordData.newPassword)
try await client.resetPassword(parameters: parameters)
@@ -35,7 +35,7 @@ class RegistrationWizard {
var sendAttempt: UInt = 0
}
let client: MXRestClient
let client: AuthenticationRestClient
let sessionCreator: SessionCreator
private(set) var state: State
@@ -59,7 +59,7 @@ class RegistrationWizard {
state.isRegistrationStarted
}
init(client: MXRestClient, sessionCreator: SessionCreator = SessionCreator()) {
init(client: AuthenticationRestClient, sessionCreator: SessionCreator = SessionCreator()) {
self.client = client
self.sessionCreator = sessionCreator
@@ -19,7 +19,7 @@ import Foundation
/// A WIP class that has common functionality to create a new session.
class SessionCreator {
/// Creates an `MXSession` using the supplied credentials and REST client.
func createSession(credentials: MXCredentials, client: MXRestClient) -> MXSession {
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?")