0

我正在尝试使用 Objective C 为多个范围、策略和 RMS 范围以交互方式获取令牌。我不确定我这样做是对还是错,我试图获取它的方式如下。我编写了一个方法“aquireToken”,在其中我为策略范围和其他 RMS 范围调用该函数两次,并在 NSDictionary 中更新结果。

“completionBlock = ^(MSALResult *result, NSError *error)”中有一个标志正在更新。但它的价值并没有体现在调用者“aquireToken”函数中。

代码片段如下:

- (void) aquireToken
{

    policyTokenResult = false;
    rmsTokenResult = false;

    NSError *error = nil;
    MSALPublicClientApplication *application = [self createPublicClientApplication:&error];


    [self retrieveTokens:application forScopes:scopesPolicy isPolicy:true];

    if (policyTokenResult)
        {
            [self retrieveTokens:application forScopes:scopesRMS isPolicy:false];
        }


    for (NSString* key in resultMap) {
        id value = resultMap[key];
      //  id object = [resultDict objectForKey:key];
        NSLog(@"%@ = %@", key, value);
        // do stuff
    }

}  

- (void)retrieveTokens:(MSALPublicClientApplication*) application
         forScopes: (NSArray<NSString *> *) scopes
          isPolicy: (BOOL) isPolicy
{

NSError *error = nil;
MSALAccount* userAccount  = nil;

for (MSALAccount *account in [application allAccounts:&error])
{
    if([[account.username uppercaseString] isEqualToString:[authID uppercaseString]])
    {
        NSLog(@"Account Found: \t%@", account.username);
        userAccount = account;
        break;
    }
}

MSALCompletionBlock completionBlock;
__block __weak MSALCompletionBlock weakCompletionBlock;

weakCompletionBlock = completionBlock = ^(MSALResult *result, NSError *error)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (!error)
        {
            if (isPolicy)
            {
                [resultMap setObject:result.accessToken forKey:@"PolicyAccessToken"];
                [resultMap setObject:result.account.username forKey:@"UserId"];
                authID = result.account.username;
                policyTokenResult = true;

            }
            else
            {
                [resultMap setObject:result.accessToken forKey:@"RMSAccessToken"];
                rmsTokenResult = true;
            }

            if(policyTokenResult && rmsTokenResult)
            {
                [resultMap setObject:@"" forKey:@"ResultStatusSuccess"];
            }

            return;
        }

        if ([error.domain isEqualToString:MSALErrorDomain] && error.code == MSALErrorInteractionRequired)
        {
            [self acquireTokenInteractive:application scopes:scopes isPolicy:isPolicy completionBlock:weakCompletionBlock];
            return;
        }

    });

};

if(userAccount)
{
    [self acquireTokenSilent:application scopes:scopes forAccount:userAccount isPolicy:isPolicy completionBlock:completionBlock];

}
else
{
    [self acquireTokenInteractive:application scopes:scopes isPolicy:isPolicy completionBlock:completionBlock];

}
}

- (void) acquireTokenSilent: (MSALPublicClientApplication *) application
                     scopes: (NSArray<NSString *> *) scopes
                 forAccount: (MSALAccount *) userAccount
                   isPolicy: (BOOL) isPolicy
            completionBlock: (MSALCompletionBlock) completionBlock
{

    MSALSilentTokenParameters *silentParams = [[MSALSilentTokenParameters alloc] initWithScopes:scopes account:userAccount];

    [application acquireTokenSilentWithParameters:silentParams completionBlock:completionBlock];
}

- (void) acquireTokenInteractive: (MSALPublicClientApplication *) application
                          scopes: (NSArray<NSString *> *) scopes
                        isPolicy: (BOOL) isPolicy
                 completionBlock: (MSALCompletionBlock)completionBlock
{

    MSALInteractiveTokenParameters *interactiveParams = [[MSALInteractiveTokenParameters alloc] initWithScopes:scopes];
    [interactiveParams setPromptType:MSALPromptTypeSelectAccount];
    interactiveParams.completionBlockQueue = dispatch_get_main_queue();
    [application acquireTokenWithParameters:interactiveParams completionBlock:completionBlock];
}
4

0 回答 0