How do I implement AFOAuth2Client to get the refresh token?

I am trying to do Oauth authentication in my iphone application.

Scenario

Created a subclass of AFOAuth2Client and overrides the method

- (void)authenticateWithUsernameAndPassword:(NSString *)username
                                   password:(NSString *)password
                                    success:(void (^)(AFOAuthCredential *credential))success
                                    failure:(void (^)(NSError *error))failure;

      

to authenticate and set an authentication token for the header in the client and use its sharedInstance to invoke web services (using Restkit).

Question What I cannot do is refresh the token. I have an api to get a refresh token.

  • How does my application know that the token has expired?
  • Where to check the token has expired and the call to get a new refresh token
  • Is there an option in AFOAuth2Client for this?
+3


source to share


1 answer


- (NSURLSessionTask *)authenticateUsingOAuthWithURLString:(NSString *)URLString 
                               refreshToken:(NSString *)refreshToken
                               success:(void (^)(AFOAuthCredential *credential))success
                               failure:(void (^)(NSError *error))failure;

      

With this method, you can present a new refreshToken to the auth server. And before that you need to store AFOAuthCredential as NSData in user default settings and then when you call api get AFOAuthCredential and check if it has expired or not using this method.



func refreshTokenIfUnavailable(completionHandler : @escaping(_ finished:Bool,_ error:Error) -> ()) {
    let credentialData = UserDefaults.standard.object(forKey: CREDENTIAL) as? NSData

    if let credentialData = credentialData {
        let credential = NSKeyedUnarchiver.unarchiveObject(with: credentialData as Data) as? AFOAuthCredential

        if let _ = credential
        {
            if credential!.isExpired {
                let urlString = OAUTH_BASE_URL
                let url = URL(string: urlString)
                let oAuthManager = AFOAuth2Manager(baseURL: url!, clientID: YOUR_AUTH_CLIENT_ID secret: YOUR_AUTH_SECRET_ID)
                oAuthManager.authenticateUsingOAuth(withURLString: "oauth/token", refreshToken: (credential?.refreshToken)!, success: { (credential) in
                    self.saveAccessToken(credential: credential)
                    completionHandler(true,YOUR_ERROR))
                }, failure: { (error) in
                    completionHandler((credential != nil),NSError(domain: "", code: 204, userInfo: [NSLocalizedDescriptionKey: error.localizedDescription]))
                })
            }
            completionHandler((credential != nil),YOUR_ERROR_MESSAGE))
        }
    }
}

func saveAccessToken(credential : AFOAuthCredential) {

    KeychainService.saveAccessToken(service: ACCESS_TOKEN as NSString, data: credential.accessToken as NSString)

    let credentialData = NSKeyedArchiver.archivedData(withRootObject: credential)
    let userDefaults = UserDefaults.standard
    userDefaults.set(credentialData, forKey: "credentials")
}

      

Hope this suits your needs. Happy coding :)

+2


source







All Articles