mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-05 06:05:56 -06:00
## Summary Most, but not all, of the changes necessary to support multi-server throughout the app and all its features. ## Screenshots | Light | Dark | | ----- | ---- | |  |  | |  |  | |  |  | |  |  | |  |  | ## Any other notes - Encapsulates all connectivity, token & server-specific knowledge in a Server model object which gets passed around. - Updates various places throughout the app to know about and use Server rather than accessing said information through non-server-specific methods. - Visually requests/notes server in places where it's ambiguous. For example, the Open Page widget will gain a subtitle if multiple servers are set up. - Allows switching which server is shown in the WebViews. Note that this doesn't take into account multi-window support on iPad/macOS yet. Most things will migrate successfully however adding an additional server causes things like Shortcuts to start erroring requiring you specify which to use in the particular Shortcut. Future work necessary: - Model objects currently clobber each other if their identifiers match. For example, both servers having a zone named `home` means one of them wins the fight for which is known to the app. - Being remotely logged out on any account causes the app to require onboarding again, when instead it should only do that if the last known server is logged out.
119 lines
4.1 KiB
Swift
119 lines
4.1 KiB
Swift
import Alamofire
|
|
import Foundation
|
|
import ObjectMapper
|
|
import PromiseKit
|
|
|
|
typealias URLRequestConvertible = Alamofire.URLRequestConvertible
|
|
|
|
public class AuthenticationAPI {
|
|
public enum AuthenticationError: LocalizedError {
|
|
case serverError(statusCode: Int, errorCode: String?, error: String?)
|
|
|
|
public var errorDescription: String? {
|
|
switch self {
|
|
case let .serverError(statusCode: statusCode, errorCode: errorCode, error: error):
|
|
return [String(describing: statusCode), errorCode, error].compactMap { $0 }.joined(separator: ", ")
|
|
}
|
|
}
|
|
}
|
|
|
|
let server: Server
|
|
init(server: Server) {
|
|
self.server = server
|
|
}
|
|
|
|
public func refreshTokenWith(tokenInfo: TokenInfo) -> Promise<TokenInfo> {
|
|
Promise { seal in
|
|
let token = tokenInfo.refreshToken
|
|
let routeInfo = RouteInfo(
|
|
route: AuthenticationRoute.refreshToken(token: token),
|
|
baseURL: server.info.connection.activeURL()
|
|
)
|
|
let request = Session.default.request(routeInfo)
|
|
|
|
let context = TokenInfo.TokenInfoContext(oldTokenInfo: tokenInfo)
|
|
request.validateAuth().responseObject(context: context) { (response: DataResponse<TokenInfo, AFError>) in
|
|
switch response.result {
|
|
case let .failure(error):
|
|
seal.reject(error)
|
|
case let .success(value):
|
|
seal.fulfill(value)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public func revokeToken(tokenInfo: TokenInfo) -> Promise<Bool> {
|
|
Promise { seal in
|
|
let token = tokenInfo.accessToken
|
|
let routeInfo = RouteInfo(
|
|
route: AuthenticationRoute.revokeToken(token: token),
|
|
baseURL: server.info.connection.activeURL()
|
|
)
|
|
let request = Session.default.request(routeInfo)
|
|
|
|
request.validateAuth().response { _ in
|
|
// https://developers.home-assistant.io/docs/en/auth_api.html#revoking-a-refresh-token says:
|
|
//
|
|
// The request will always respond with an empty body and HTTP status 200,
|
|
// regardless if the request was successful.
|
|
seal.fulfill(true)
|
|
}
|
|
}
|
|
}
|
|
|
|
public static func fetchToken(
|
|
authorizationCode: String,
|
|
baseURL: URL
|
|
) -> Promise<TokenInfo> {
|
|
Promise { seal in
|
|
let routeInfo = RouteInfo(
|
|
route: AuthenticationRoute.token(authorizationCode: authorizationCode),
|
|
baseURL: baseURL
|
|
)
|
|
let request = Session.default.request(routeInfo)
|
|
|
|
request.validateAuth().responseObject { (dataresponse: DataResponse<TokenInfo, AFError>) in
|
|
switch dataresponse.result {
|
|
case let .failure(error):
|
|
seal.reject(error)
|
|
case let .success(value):
|
|
seal.fulfill(value)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension DataRequest {
|
|
@discardableResult
|
|
func validateAuth() -> Self {
|
|
validate { _, response, data in
|
|
if case 200 ..< 300 = response.statusCode {
|
|
return .success(())
|
|
} else if let data = data {
|
|
let errorCode: String?
|
|
let error: String?
|
|
|
|
if let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
|
|
errorCode = json["error"] as? String
|
|
error = json["error_description"] as? String
|
|
} else {
|
|
errorCode = nil
|
|
error = String(data: data, encoding: .utf8)
|
|
}
|
|
|
|
return .failure(AuthenticationAPI.AuthenticationError.serverError(
|
|
statusCode: response.statusCode,
|
|
errorCode: errorCode,
|
|
error: error
|
|
))
|
|
} else {
|
|
return .failure(AFError.responseValidationFailed(
|
|
reason: .unacceptableStatusCode(code: response.statusCode)
|
|
))
|
|
}
|
|
}
|
|
}
|
|
}
|