mirror of
https://github.com/home-assistant/iOS.git
synced 2026-02-05 14:56:41 -06:00
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> This PR removes the `options` parameters which use the default value. ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> <img width="1014" alt="Screenshot 2024-03-03 at 21 17 55" src="https://github.com/home-assistant/iOS/assets/35694712/85324caf-c36d-4366-a9f8-9c349192a361"> <img width="1049" alt="Screenshot 2024-03-03 at 21 17 17" src="https://github.com/home-assistant/iOS/assets/35694712/cebddbe6-8ea4-415e-b12a-0e35f86bf5a3"> <img width="1022" alt="Screenshot 2024-03-03 at 21 16 53" src="https://github.com/home-assistant/iOS/assets/35694712/ccccb902-9791-42f6-8868-5900093c40ae"> <img width="991" alt="Screenshot 2024-03-03 at 21 16 28" src="https://github.com/home-assistant/iOS/assets/35694712/f2f33685-a55c-47ed-a555-8cc0fc1561ce"> <img width="1329" alt="Screenshot 2024-03-03 at 21 16 00" src="https://github.com/home-assistant/iOS/assets/35694712/2dc156da-29d6-44e4-a064-4fbdb79139bd"> <img width="1130" alt="Screenshot 2024-03-03 at 21 14 53" src="https://github.com/home-assistant/iOS/assets/35694712/628dd2ff-5bba-4ee7-912b-cdbdf73f18b2">
129 lines
4.5 KiB
Swift
129 lines
4.5 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
|
|
let session: Session
|
|
|
|
init(server: Server) {
|
|
self.server = server
|
|
self.session = Session(serverTrustManager: CustomServerTrustManager(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.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.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,
|
|
exceptions: SecurityExceptions
|
|
) -> Promise<TokenInfo> {
|
|
let session = Session(serverTrustManager: CustomServerTrustManager(exceptions: exceptions))
|
|
|
|
return Promise { seal in
|
|
let routeInfo = RouteInfo(
|
|
route: AuthenticationRoute.token(authorizationCode: authorizationCode),
|
|
baseURL: baseURL
|
|
)
|
|
let request = session.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)
|
|
}
|
|
}
|
|
}.ensure {
|
|
withExtendedLifetime(session) {
|
|
// keeping session around until we're done
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
extension DataRequest {
|
|
@discardableResult
|
|
func validateAuth() -> Self {
|
|
validate { _, response, data in
|
|
if case 200 ..< 300 = response.statusCode {
|
|
return .success(())
|
|
} else if let data {
|
|
let errorCode: String?
|
|
let error: String?
|
|
|
|
if let json = try? JSONSerialization.jsonObject(with: data) 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)
|
|
))
|
|
}
|
|
}
|
|
}
|
|
}
|