【问题标题】:objective c How to refresh Access Token using Refresh Token目标 c 如何使用 Refresh Token 刷新 Access Token
【发布时间】:2019-05-02 07:12:23
【问题描述】:

我正在使用 Cognito 用户池对系统中的用户进行身份验证。成功的身份验证会提供一个 ID 令牌 (JWT)、访问令牌 (JWT)。每隔一小时 TokenExpiration 。我的问题是一旦我的访问令牌过期,我如何使用存储的刷新令牌再次刷新我的访问令牌?这是我的代码。

- (void)loginAWSMethod {
    NSString *emailId = @"the email";
    NSString *pwdTxt = @"the password";

    NSLog(@"entered the login method %@ %@",emailId,pwdTxt);
    AWSCognitoIdentityUser *user = [pool getUser:emailId];
    [[user getSession:emailId password:pwdTxt validationData:nil]
     continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession *> * _Nonnull task)
     {
         if (task.error) {
             dispatch_async(dispatch_get_main_queue(), ^{
                 dispatch_async(dispatch_get_main_queue(), ^{
                     NSLog(@"ERROR CATCHED++++++");
                     UIAlertController * alert = [UIAlertController
                                                  alertControllerWithTitle:@"Incorrect email or password."
                                                  message:@""
                                                  preferredStyle:UIAlertControllerStyleAlert];

                     UIAlertAction* yesButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
                                                 {
                                                 }];

                     [alert addAction:yesButton];
                     [self presentViewController:alert animated:YES completion:nil];
                 });

                 [self removeWaitingProgress];
             });

         }else{
             NSLog(@"the result is %@",task.result);
             AWSCognitoIdentityUserSession *response1 = task.result;
             token = response1.accessToken.tokenString;
             NSLog(@"the token is %@",token);
             [[user getDetails] continueWithSuccessBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserGetDetailsResponse *> * _Nonnull task) {
                 AWSCognitoIdentityUserGetDetailsResponse *response = task.result;
                 for (AWSCognitoIdentityUserAttributeType *attribute in response.userAttributes) {
                     //print the user attributes
                     NSLog(@"Attribute: %@ Value: %@", attribute.name, attribute.value);
                     if([attribute.name isEqualToString:@"sub"]){
                         cognitoID = attribute.value;
                     }
                     [defaults setValue:token forKey:@"token"];
                     [defaults setValue:@"yes" forKey:@"isLoggedIn"];
                     [defaults synchronize];
                     dispatch_async(dispatch_get_main_queue(), ^{
                         [self removeWaitingProgress];
                         [self gotoDashborad];
                     });
                 }
                 return nil;
             }];
         }
         return  nil;
     }];
}

【问题讨论】:

    标签: ios objective-c amazon-web-services


    【解决方案1】:

    您应该能够简单地调用-[AWSCognitoIdentityUser getSession],它会在后台返回当前有效的访问令牌,或者将刷新令牌交换为新的访问令牌:

    -(nullable NSString *)accessTokenStringForCurrentUser {
        AWSCognitoIdentityUser *currentUser = [pool currentUser];
        __block NSString *tokenString;
        // `getSession` automatically exchanges the refresh token for a valid access token if needed
        [[[currentUser getSession] continueWithBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserSession *> * _Nonnull task) {
            // (Error handling not shown)
            if (task.result) {
                AWSCognitoIdentityUserSessionToken *accessToken = task.result.accessToken;
                tokenString = accessToken.tokenString;
            }
            return nil;
        }] waitUntilFinished];
        return tokenString;
    }
    

    您可能还希望查看Cognito UserPools Sample app,其中包含使用 UserPools 的 Objective C 示例。

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 2020-05-26
      • 2017-12-30
      • 2017-11-11
      • 1970-01-01
      • 2019-02-18
      • 2014-04-20
      • 2019-06-07
      • 2023-02-02
      相关资源
      最近更新 更多