【发布时间】:2016-01-16 18:33:25
【问题描述】:
总体问题: 我在前端 (iOS) 中使用开发人员验证身份时遇到问题。我知道我的后端会生成正确的令牌和身份 ID,但我的刷新方法永远不会被调用。我也看过样本,但我对正在发生的一切感到有些困惑。 流程说明: 目前我有一个带有登录按钮的登录屏幕。用户按下登录按钮,然后我的 api 类获取凭据,加密密码并将其存储在钥匙串中(现在注释掉,因为它在模拟器上不起作用)。我的 DeveloperAuthenticatedIdentityProvider 被称为我的应用程序 BusytimeAuthenticated。我已经完成了所有方法(我使用 AWS lambda 和 DynamoDB 对用户进行身份验证)我从未经身份验证的访问开始,它只允许我访问两种方法,登录和注册。然后我想假设我的认证用户允许我调用我的其他方法。
我的 API 代码:
[AWSLogger defaultLogger].logLevel = AWSLogLevelVerbose;
id<AWSCognitoIdentityProvider> identityProvider = [[BusytimeAuthenticated alloc] initWithRegionType:AWSRegionUSEast1
identityId:nil
identityPoolId:@"SOMEIDENTITYPOOLID"
logins:@{@"SOMEPROVIDERNAME": @"SOMEUSERNAME"}
providerName:@"SOMEPROVIDERNAME"
];
credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1
identityProvider:identityProvider
unauthRoleArn:nil
authRoleArn:nil];
configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1
credentialsProvider:self.credentialsProvider];
AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
[[credentialsProvider refresh] continueWithBlock:^id(BFTask *task){
[self testAuth];
return nil;
}];
我的 DeveloperAuthenticatedIdentityProvider 代码(BusytimeAuthenticated):
#import "BusytimeAuthenticated.h"
@interface BusytimeAuthenticated()
@property (strong, atomic) NSString *providerName;
@property (strong, atomic) NSString *token;
@end
@implementation BusytimeAuthenticated
@synthesize providerName=_providerName;
@synthesize token=_token;
- (instancetype)initWithRegionType:(AWSRegionType)regionType
identityId:(NSString *)identityId
identityPoolId:(NSString *)identityPoolId
logins:(NSDictionary *)logins
providerName:(NSString *)providerName{
if (self = [super initWithRegionType:regionType identityId:identityId accountId:nil identityPoolId:identityPoolId logins:logins]) {
self.providerName = providerName;
}
return self;
}
// Return the developer provider name which you choose while setting up the
// identity pool in the Amazon Cognito Console
- (BOOL)authenticatedWithProvider {
return [self.logins objectForKey:self.providerName] != nil;
}
// If the app has a valid identityId return it, otherwise get a valid
// identityId from your backend.
- (BFTask *)getIdentityId {
// already cached the identity id, return it
if (self.identityId) {
return [BFTask taskWithResult:nil];
}
// not authenticated with our developer provider
else if (![self authenticatedWithProvider]) {
return [super getIdentityId];
}
// authenticated with our developer provider, use refresh logic to get id/token pair
else {
return [[BFTask taskWithResult:nil] continueWithBlock:^id(BFTask *task) {
if (!self.identityId) {
return [self refresh];
}
return [BFTask taskWithResult:self.identityId];
}];
}
}
// Use the refresh method to communicate with your backend to get an
// identityId and token.
- (BFTask *)refresh {
if (![self authenticatedWithProvider]) {
return [super getIdentityId];
}else{
// KeychainWrapper *keychain = [[KeychainWrapper alloc]init];
AWSLambdaInvoker *lambdaInvoker = [AWSLambdaInvoker defaultLambdaInvoker];
NSDictionary *parameters = @{@"username" : @"SOMEUSERNAME",
@"password":@"SOMEENCRYPTEDPASS",
@"isError" : @NO};
NSLog(@"Here");
[[lambdaInvoker invokeFunction:@"login" JSONObject:parameters] continueWithBlock:^id(BFTask* task) {
if (task.error) {
NSLog(@"Error: %@", task.error);
}
if (task.exception) {
NSLog(@"Exception: %@", task.exception);
}
if (task.result) {
self.identityId = [task.result objectForKey:@"IdentityId" ];
self.token = [task.result objectForKey:@"Token" ];
// [keychain mySetObject:[task.result objectForKey:@"Token" ] forKey:@"Token"];
// [keychain mySetObject:[task.result objectForKey:@"IdentityId" ] forKey:@"IdentityId"];
NSLog(@"Result: %@", task.result);
}
return [BFTask taskWithResult:self.identityId];
}];
}
return NULL;
}
@end
总结问题: 不幸的是,当我测试我的新权限时,我从错误中看到:“Unauth_Role/CognitoIdentityCredentials 未被授权执行:lambda:InvokeFunction”。显然我没有正确切换。我在刷新方法中放置了一个断点,以查看它是否被调用。它不是。我不太了解如何正确切换。非常感谢任何有关使其正常工作的帮助。
注意:我所做的一项重大更改是我去掉了“DeveloperAuthenticationClient”类,因为我认为没有它我也可以做到。
【问题讨论】:
标签: ios authentication amazon-web-services aws-lambda amazon-cognito