Retrieve session id for later requests

This commit is contained in:
Anh Do 2020-03-09 23:26:11 -04:00
parent 034aabbfff
commit b7fe991b7b
No known key found for this signature in database
GPG Key ID: 451E3092F917B62D
5 changed files with 25 additions and 7 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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))
}

View File

@ -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) {

View File

@ -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()