AFOAuth2Client和刷新令牌 [英] AFOAuth2Client and refresh token

查看:126
本文介绍了AFOAuth2Client和刷新令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在iPad应用程序中实现Oauth?

How to implement the Oauth in iPad application?

AFOAuth2Client如何管理oauth 2.0中的刷新令牌机制?

How does AFOAuth2Client manages refreshing token mechanism in oauth 2.0?

有什么方法可以在类中实现它,还是我们必须以自己的方式实现它?如何检查令牌是否已过期?

Is there any method to implement it inside the class or do we have to implement it in our own way? How to check the token is expired or not?

推荐答案

我解决这个问题的方法是用一个包裹我的所有请求代码块,如果需要将刷新访问令牌,例如

The way that I have solved this is to wrap all my requests with a code block which will refresh the access token if needed e.g.

为成功和失败块添加一些typedef:

Add some typedefs for success and failure blocks:

typedef void (^YFRailsSaasApiClientSuccess)(AFJSONRequestOperation *operation, id responseObject);
typedef void (^YFRailsSaasApiClientFailure)(AFJSONRequestOperation *operation, NSError *error);

然后请求方法是:

- (void)getProductsWithSuccess:(YFRailsSaasApiClientSuccess)success failure:(YFRailsSaasApiClientFailure)failure {
    NSLog(@"getProductsWithSuccess");

    success = ^(AFJSONRequestOperation *operation, id responseObject) {
        [self getPath:@"api/1/products"
           parameters:nil
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  NSLog(@"getProductsWithSuccess: success");

                  // TODO: handle response

                  if (success) {
                      success((AFJSONRequestOperation *)operation, responseObject);
                  }
              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  NSLog(@"getProductsWithSuccess: failure");
              if (failure) {
                  failure((AFJSONRequestOperation *)operation, error);
              }
          }];
    };

    [self refreshAccessTokenWithSuccess:success failure:failure];
}

检查令牌到期并在需要时刷新的方法是:

And the method which checks for token expiry and refreshes it if needed is:

- (void)refreshAccessTokenWithSuccess:(YFRailsSaasApiClientSuccess)success failure:(YFRailsSaasApiClientFailure)failure {
    NSLog(@"refreshAccessTokenWithSuccess");

    if (self.credential == nil) {
        if (failure) {
            NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
            [errorDetail setValue:@"Failed to get credentials" forKey:NSLocalizedDescriptionKey];
            NSError *error = [NSError errorWithDomain:@"world" code:200 userInfo:errorDetail];
            failure(nil, error);
        }
        return;
    }  

    if (!self.credential.isExpired) {
        NSLog(@"refreshAccessTokenWithSuccess: credential has not expired");

        if (success) {
            success(nil, nil);
        }
        return;
    }

    NSLog(@"refreshAccessTokenWithSuccess: refreshing credential");

    [self authenticateUsingOAuthWithPath:@"oauth/token"
                            refreshToken:self.credential.refreshToken
                                 success:^(AFOAuthCredential *newCredential) {
                                     NSLog(@"Successfully refreshed OAuth credentials %@", newCredential.accessToken);
                                     self.credential = newCredential;
                                     [AFOAuthCredential storeCredential:newCredential
                                                     withIdentifier:self.serviceProviderIdentifier];

                                     if (success) {
                                         success(nil, nil);
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     NSLog(@"An error occurred refreshing credential: %@", error);
                                     if (failure) {
                                         failure(nil, error);
                                     }
                                 }];
}

GitHub上的完整源代码:https://github.com/yellowfeather/rails-saas-ios

Full source code is up on GitHub: https://github.com/yellowfeather/rails-saas-ios.

这篇关于AFOAuth2Client和刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆