Commit af95869c authored by Jose Blaya's avatar Jose Blaya
Browse files

Refresh the token for users using existing version of the app

parent 49f14db3
......@@ -55,8 +55,9 @@ public protocol AccountProvider: class {
- Postcondition:
- Posts `Notification.Name.PIAAccountDidRefresh` on success.
- Parameter callback: Returns a refreshed `AccountInfo`.
- Parameter force: Force refresh.
*/
func refreshAccountInfo(_ callback: LibraryCallback<AccountInfo>?)
func refreshAccountInfo(force: Bool, _ callback: LibraryCallback<AccountInfo>?)
/**
Updates the account currently logged in.
......
......@@ -120,6 +120,18 @@ class DefaultAccountProvider: AccountProvider, ConfigurationAccess, DatabaseAcce
}
}
private func updateToken(_ token: String) {
let tokenComponents = token.split(by: token.count/2)
if let first = tokenComponents.first,
let last = tokenComponents.last {
self.accessedDatabase.secure.setUsername(first)
self.accessedDatabase.secure.setToken(token,
for: self.accessedDatabase.secure.tokenKey(for: first))
self.accessedDatabase.secure.setPassword(last,
for: first)
}
}
func login(with request: LoginRequest, _ callback: ((UserAccount?, Error?) -> Void)?) {
guard !isLoggedIn else {
preconditionFailure()
......@@ -155,9 +167,10 @@ class DefaultAccountProvider: AccountProvider, ConfigurationAccess, DatabaseAcce
}
func refreshAccountInfo(_ callback: ((AccountInfo?, Error?) -> Void)?) {
func refreshAccountInfo(force: Bool, _ callback: ((AccountInfo?, Error?) -> Void)?) {
guard let token = self.token,
guard force == false,
let token = self.token,
let _ = self.publicUsername else {
guard let user = currentUser else {
......@@ -167,8 +180,12 @@ class DefaultAccountProvider: AccountProvider, ConfigurationAccess, DatabaseAcce
self.webServices.token(credentials: user.credentials) { (token, error) in
if let token = token {
self.updateDatabaseWith(token,
andUsername: user.credentials.username)
if force {
self.updateToken(token)
} else {
self.updateDatabaseWith(token,
andUsername: user.credentials.username)
}
self.accountInfoWith(token, callback)
}
}
......@@ -451,6 +468,7 @@ class DefaultAccountProvider: AccountProvider, ConfigurationAccess, DatabaseAcce
accessedDatabase.plain.accountInfo = nil
accessedDatabase.plain.reset()
}
#endif
// MARK: WebServicesConsumer
......
......@@ -28,7 +28,7 @@ class PIAWebServices: WebServices, ConfigurationAccess {
]
let parameters = credentials.toJSONDictionary()
req(nil, .post, endpoint, parameters, status, JSONRequestExecutor() { (json, status, error) in
req(nil, .post, endpoint, useAuthToken: true, parameters, status, JSONRequestExecutor() { (json, status, error) in
if let knownError = self.knownError(endpoint, status, errors) {
callback?(nil, knownError)
return
......
......@@ -151,12 +151,12 @@ public class MockAccountProvider: AccountProvider, WebServicesConsumer {
}
/// :nodoc:
public func refreshAccountInfo(_ callback: ((AccountInfo?, Error?) -> Void)?) {
public func refreshAccountInfo(force: Bool, _ callback: ((AccountInfo?, Error?) -> Void)?) {
guard !mockIsUnauthorized else {
callback?(nil, ClientError.unauthorized)
return
}
delegate.refreshAccountInfo(callback)
delegate.refreshAccountInfo(force: false, callback)
}
/// :nodoc:
......
......@@ -362,7 +362,7 @@ class EphemeralAccountProvider: AccountProvider, ProvidersAccess, InAppAccess {
}
}
func refreshAccountInfo(_ callback: ((AccountInfo?, Error?) -> Void)?) {
func refreshAccountInfo(force: Bool, _ callback: ((AccountInfo?, Error?) -> Void)?) {
fatalError("Not implemented")
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment