mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2024-12-22 23:58:36 +01:00
Retrieve session id for later requests
This commit is contained in:
parent
034aabbfff
commit
b7fe991b7b
@ -17,7 +17,8 @@ public enum CredentialsType: String {
|
||||
case basic = "password"
|
||||
case feedWranglerBasic = "feedWranglerBasic"
|
||||
case feedWranglerToken = "feedWranglerToken"
|
||||
case newsBlur = "newsBlur"
|
||||
case newsBlurBasic = "newsBlurBasic"
|
||||
case newsBlurSessionId = "newsBlurSessionId"
|
||||
case readerBasic = "readerBasic"
|
||||
case readerAPIKey = "readerAPIKey"
|
||||
case oauthAccessToken = "oauthAccessToken"
|
||||
|
@ -33,11 +33,14 @@ public extension URLRequest {
|
||||
])
|
||||
case .feedWranglerToken:
|
||||
self.url = url.appendingQueryItem(URLQueryItem(name: "access_token", value: credentials.secret))
|
||||
case .newsBlur:
|
||||
case .newsBlurBasic:
|
||||
setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||
httpMethod = "POST"
|
||||
let postData = "username=\(credentials.username)&password=\(credentials.secret)"
|
||||
httpBody = postData.data(using: String.Encoding.utf8)
|
||||
case .newsBlurSessionId:
|
||||
setValue("\(NewsBlurAPICaller.SessionIdCookie)=\(credentials.secret)", forHTTPHeaderField: "Cookie")
|
||||
httpShouldHandleCookies = true
|
||||
case .readerBasic:
|
||||
setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
|
||||
httpMethod = "POST"
|
||||
|
@ -21,6 +21,7 @@ enum NewsBlurError: LocalizedError {
|
||||
}
|
||||
|
||||
final class NewsBlurAPICaller: NSObject {
|
||||
static let SessionIdCookie = "newsblur_sessionid"
|
||||
|
||||
private let baseURL = URL(string: "https://www.newsblur.com/")!
|
||||
private var transport: Transport!
|
||||
@ -39,8 +40,8 @@ final class NewsBlurAPICaller: NSObject {
|
||||
|
||||
transport.send(request: request, resultType: NewsBlurLoginResponse.self) { result in
|
||||
switch result {
|
||||
case .success(_, let payload):
|
||||
guard payload?.code != -1 else {
|
||||
case .success(let response, let payload):
|
||||
guard let url = response.url, let headerFields = response.allHeaderFields as? [String: String], payload?.code != -1 else {
|
||||
let error = payload?.errors?.username ?? payload?.errors?.others
|
||||
if let message = error?.first {
|
||||
completion(.failure(NewsBlurError.general(message: message)))
|
||||
@ -49,7 +50,20 @@ final class NewsBlurAPICaller: NSObject {
|
||||
}
|
||||
return
|
||||
}
|
||||
completion(.success(self.credentials))
|
||||
|
||||
guard let username = self.credentials?.username else {
|
||||
completion(.failure(NewsBlurError.general(message: "Failed to log in")))
|
||||
return
|
||||
}
|
||||
|
||||
let cookies = HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: url)
|
||||
for cookie in cookies where cookie.name == Self.SessionIdCookie {
|
||||
let credentials = Credentials(type: .newsBlurSessionId, username: username, secret: cookie.value)
|
||||
completion(.success(credentials))
|
||||
return
|
||||
}
|
||||
|
||||
completion(.failure(NewsBlurError.general(message: "Failed to retrieve session")))
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
|
@ -116,7 +116,7 @@ final class NewsBlurAccountDelegate: AccountDelegate {
|
||||
}
|
||||
|
||||
func accountDidInitialize(_ account: Account) {
|
||||
credentials = try? account.retrieveCredentials(type: .newsBlur)
|
||||
credentials = try? account.retrieveCredentials(type: .newsBlurSessionId)
|
||||
}
|
||||
|
||||
func accountWillBeDeleted(_ account: Account) {
|
||||
|
@ -87,7 +87,7 @@ class NewsBlurAccountViewController: UITableViewController {
|
||||
|
||||
// When you fill in the email address via auto-complete it adds extra whitespace
|
||||
let trimmedUsername = username.trimmingCharacters(in: .whitespaces)
|
||||
let credentials = Credentials(type: .newsBlur, username: trimmedUsername, secret: password)
|
||||
let credentials = Credentials(type: .newsBlurBasic, username: trimmedUsername, secret: password)
|
||||
Account.validateCredentials(type: .newsBlur, credentials: credentials) { result in
|
||||
|
||||
self.stopAnimtatingActivityIndicator()
|
||||
|
Loading…
Reference in New Issue
Block a user