【发布时间】:2016-03-19 22:39:08
【问题描述】:
在我的应用程序中,所有用户都从 Cognito 的 identityId 开始(直接从应用程序的 Cognito/GetId 获取),经过身份验证的用户通过开发人员身份验证更新 identityId(GetOpenIdTokenForDeveloperIdentity 和旧 IdentityId 通过开发人员端服务器设置)。 经过身份验证的用户可以随时注销/登录,但是一旦注销或令牌过期,开发者端令牌就会更改。
在上述条件下,为用户保持相同的 identityId 的最佳方法是什么?
例如:当同一个用户注销然后登录。
- 用户第一次运行应用程序。他将 IdentityA 作为未经身份验证的 identityId,登录为空。他在 DatasetA 上保存了一些数据。
- 用户使用 USER/PASSWORD 登录。他获得 {"DEVDOMAIN":"TokenA"} 作为登录名。 GetOpenIdTokenForDeveloperIdentity(IdentityA, DEVDOMAIN:TokenA) 链接它们,用户通过 IdentityA 进行身份验证并保留 DatasetA。
- 用户注销。他获得未经身份验证的 IdentityB,登录/数据集为空。他在 DatasetB 上保存了一些数据。
- 用户使用 USER/PASSWORD 登录。他获得 {"DEVDOMAIN":"TokenB"} 作为登录名。
在这种情况下我需要的是关联到这个用户:
- 最新的令牌TokenB。
- 以同一用户身份访问 AWS 的 IdentityId。
- DatasetB 合并到 DatasetA。
调用 GetOpenIdTokenForDeveloperIdentity(IdentityB, DEVDOMAIN:TokenB) 意味着创建一个新用户对吗? 那我应该...
- 在开发者端数据库中保存当前在 Cognito 上注册的用户令牌 (tokenA) 和不会更改的第一个 identityId (identityA)
- 调用GetOpenIdTokenForDeveloperIdentity(IdentityB, DEVDOMAIN:TokenB) 临时链接IdentityB和TokenB
- 然后调用 MergeDeveloperIdentities(TokenB to TokenA)(我希望这会合并数据集...)
- 也许调用 DeleteIdentities(IdentityB) 因为它永远不会被使用?
- 然后告诉应用“你是IdentityA”
有没有更好的办法?
谢谢。
编辑:
TokenA / TokenB 是开发者端服务器 API 的访问令牌。 每次用户登录时它们都会更改,它们会在 2 周后过期。
我用于注销的代码:
// Code in Logout
AWSCognitoCredentialsProvider* credentialsProvider = [AWSServiceManager defaultServiceManager].defaultServiceConfiguration.credentialsProvider;
[credentialsProvider clearKeychain];
[[AWSCognito defaultCognito] wipe];
登录/注销后,我以这种方式更新用户的身份标识:
AWSCognitoCredentialsProvider* credentialsProvider = [AWSServiceManager defaultServiceManager].defaultServiceConfiguration.credentialsProvider;
NSString* previousIdToken = credentialsProvider.logins[IDPROVIDER_OURSERVICE];
if (previousIdToken == nil || ![idToken isEqualToString:previousIdToken])
{
if (idToken.length == 0)
{
// Logout
credentialsProvider.logins = @{ };
return [credentialsProvider getIdentityId];
}
else
{
// Login/Update
credentialsProvider.logins = @{ IDPROVIDER_OURSERVICE:idToken };
return [credentialsProvider refresh];
}
}
return [AWSTask taskWithResult:idToken];
我还在启动时运行以下代码,以在用户重新安装应用程序时清除凭据(在我的应用程序中卸载 = 注销)
// Initialization code
AppIdentityProvider* appIdentityProvider = [[AppIdentityProvider alloc] initWithRegionType:AWSRegionUSEast1
identityId:nil
accountId:COGNITO_ACOUNT_ID
identityPoolId:COGNITO_IDENTITY_POOL_ID
logins:logins];
AWSCognitoCredentialsProvider* credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1
identityProvider:appIdentityProvider
unauthRoleArn:nil
authRoleArn:nil];
if ([AppContext sharedInstance].appInitRunCount == 1)
{
// Restart as guest after an uninstall
NSLog(@"=== AppUserManager: First run: clearing keychain");
[[AWSCognito defaultCognito] wipe];
[credentialsProvider clearKeychain];
}
AWSServiceConfiguration* configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1
credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
[[credentialsProvider getIdentityId] continueWithSuccessBlock:^id(AWSTask *task) {
[self testCognito];
return nil;
}];
我的自定义身份提供者如下所示:
@interface AppIdentityProvider : AWSAbstractCognitoIdentityProvider
// ...
@end
@interface AppIdentityProvider ()
@property (nonatomic, strong, readwrite) NSString *token;
@end
@implementation AppIdentityProvider
@synthesize token = _token;
-(id)initWithRegionType:(AWSRegionType)regionType
identityId:(NSString*)identityId
accountId:(NSString*)accountId
identityPoolId:(NSString*)identityPoolId
logins:(NSDictionary*)logins
{
self = [super initWithRegionType:regionType
identityId:identityId
accountId:accountId
identityPoolId:identityPoolId
logins:logins];
if (self == nil)
return nil;
return self;
}
-(BOOL)isAuthenticatedWithOurService
{
return self.logins != nil && [self.logins objectForKey:IDPROVIDER_OURSERVICE] != nil;
}
- (AWSTask *)getIdentityId
{
if (self.identityId != nil)
return [AWSTask taskWithResult:self.identityId];
if (![self isAuthenticatedWithOurService])
return [super getIdentityId];
return [[AWSTask taskWithResult:nil] continueWithBlock:^id(AWSTask *task) {
if (self.identityId != nil)
return [AWSTask taskWithResult:self.identityId];
return [self refresh];
}];
}
- (AWSTask *)refresh
{
AWSTaskCompletionSource *source = [AWSTaskCompletionSource taskCompletionSource];
if (![self isAuthenticatedWithOurService])
return [super getIdentityId];
ApiRequest* authApi = [[ApiRequestManager sharedInstance] generateAuthApiByIdToken:self.logins[IDPROVIDER_OURSERVICE]];
[authApi requestAsyncCompletionHandler:^(ApiRequest *request)
{
NSDictionary *response = request.response;
if ([request hasSucceeded] && [[response valueForKey:@"result"] intValue] == 1)
{
self.token = [[response valueForKey:@"data"] valueForKey:@"token"];
self.identityId = [[response valueForKey:@"data"] valueForKey:@"identityId"];
[source setResult:self.identityId];
}
else
{
[source setError:[NSError errorWithDomain:@"refresh" code:0 userInfo:response]];
}
}];
return source.task;
}
@end
【问题讨论】:
-
这里有一些后续问题。 1.) 这些“TokenA”和“TokenB”是从哪里来的?这些是来自 GetOpenIdTokenForDeveloperIdentity 调用响应的令牌吗? 2.) 在用户退出时,您是否在客户端手动清除用户身份 ID?
-
1) TokenA/TokenB 是我们的服务生成的令牌。这些是访问令牌(随机生成),用于访问开发者端服务器的 API,它们是唯一的,它们会在 2 周内过期,每次用户登录时都会更改。也许我不应该将其用作 Cognito 登录 [DEVDOMAIN] ? 2) 在退出时,我调用 [[AWSCognito defaultCognito] 擦除] 然后 getIdentityId 来获取一个新的。我将使用确切的代码编辑问题。
标签: ios amazon-web-services amazon-cognito