2019-11-07 08:54:41 +01:00
//
// F e e d l y R e f r e s h A c c e s s T o k e n O p e r a t i o n . s w i f t
// A c c o u n t
//
// C r e a t e d b y K i e l G i l l a r d o n 4 / 1 1 / 1 9 .
// C o p y r i g h t © 2 0 1 9 R a n c h e r o S o f t w a r e , L L C . A l l r i g h t s r e s e r v e d .
//
import Foundation
import os . log
import RSWeb
final class FeedlyRefreshAccessTokenOperation : FeedlyOperation {
2020-01-19 23:19:06 +01:00
2019-11-07 08:54:41 +01:00
let service : OAuthAccessTokenRefreshing
let oauthClient : OAuthAuthorizationClient
let account : Account
let log : OSLog
init ( account : Account , service : OAuthAccessTokenRefreshing , oauthClient : OAuthAuthorizationClient , log : OSLog ) {
self . oauthClient = oauthClient
self . service = service
self . account = account
self . log = log
}
2020-01-16 06:30:37 +01:00
override func run ( ) {
2019-11-07 08:54:41 +01:00
let refreshToken : Credentials
do {
guard let credentials = try account . retrieveCredentials ( type : . oauthRefreshToken ) else {
os_log ( . debug , log : log , " Could not find a refresh token in the keychain. Check the refresh token is added to the Keychain, remove the account and add it again. " )
throw TransportError . httpError ( status : 403 )
}
refreshToken = credentials
} catch {
2020-01-19 23:19:06 +01:00
didFinish ( with : error )
2019-11-07 08:54:41 +01:00
return
}
os_log ( . debug , log : log , " Refreshing access token. " )
// I g n o r e c a n c e l l a t i o n a f t e r t h e r e q u e s t i s r e s u m e d o t h e r w i s e w e m a y c o n t i n u e s t o r i n g a p o t e n t i a l l y i n v a l i d t o k e n !
service . refreshAccessToken ( with : refreshToken . secret , client : oauthClient ) { result in
self . didRefreshAccessToken ( result )
}
}
private func didRefreshAccessToken ( _ result : Result < OAuthAuthorizationGrant , Error > ) {
assert ( Thread . isMainThread )
switch result {
case . success ( let grant ) :
do {
os_log ( . debug , log : log , " Storing refresh token. " )
// S t o r e t h e r e f r e s h t o k e n f i r s t b e c a u s e i t s e n d s t h i s t o k e n t o t h e a c c o u n t d e l e g a t e .
if let token = grant . refreshToken {
try account . storeCredentials ( token )
}
os_log ( . debug , log : log , " Storing access token. " )
// N o w s t o r e t h e a c c e s s t o k e n b e c a u s e w e w a n t t h e a c c o u n t d e l e g a t e t o u s e i t .
try account . storeCredentials ( grant . accessToken )
didFinish ( )
} catch {
2020-01-19 23:19:06 +01:00
didFinish ( with : error )
2019-11-07 08:54:41 +01:00
}
case . failure ( let error ) :
2020-01-19 23:19:06 +01:00
didFinish ( with : error )
2019-11-07 08:54:41 +01:00
}
}
}