spa*_*par 18 amazon-web-services amazon-cognito
我想使用一个电话号码作为我的应用程序的用户名,我希望能够通过每次他们想要登录时验证电话号码来简化注册 - 没有杂乱的密码记住业务.
如何使用AWS Cognito User Pool执行此操作,因为它要求我强制为每个用户配置密码.
我想为每个用户使用虚拟密码并配置强制用户验证.每次用户退出时,我都可以"取消验证"用户,以便下次自动要求他们验证电话号码.此外,如果用户已经过验证,我会将我的应用程序连接到"登录".
如果这是最好的方法,请告诉我:(我是AWS的新手,我找不到这个场景的任何帖子.
谢谢 !!
Ash*_*han 17
由于AWS Cognito目前不支持无密码身份验证,因此您需要使用外部存储的随机密码实施变通方法.您可以按如下方式实现身份验证流程.
请查看以下代码示例以了解MFA的洞察力,并参阅此链接以获取更多详细信息.
var userData = {
Username : 'username',
Pool : userPool
};
cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
var authenticationData = {
Username : 'username',
Password : 'password',
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
alert('authentication successful!')
},
onFailure: function(err) {
alert(err);
},
mfaRequired: function(codeDeliveryDetails) {
var verificationCode = prompt('Please input verification code' ,'');
cognitoUser.sendMFACode(verificationCode, this);
}
});
Run Code Online (Sandbox Code Playgroud)
注意:此处带有手机号码的MFA不会用于MFA,而是作为满足您要求的解决方法.
这可能有效,但考虑到安全性,在 dynamoDB 中存储密码可能会出现问题。相反,我们可以这样尝试:
选项#1: - 用户使用用户名和密码注册。
选项#1: - 用户无需用户名和密码即可注册。 认知设置
对于触发器代码,请参阅 具有多个登录选项的 aws 认知池
这与OP所使用的单个秘密稍有不同,因为我认为这可能会帮助其他解决此问题的人。
通过为Cognito触发器创建自定义lambda,我能够做到这一点:定义身份验证质询,创建身份验证质询和验证身份质询。
我的要求是,我希望后端使用A secret
来获取任何Cognito用户的访问和刷新令牌。
定义身份验证挑战Lambda
exports.handler = async event => {
if (
event.request.session &&
event.request.session.length >= 3 &&
event.request.session.slice(-1)[0].challengeResult === false
) {
// The user provided a wrong answer 3 times; fail auth
event.response.issueTokens = false;
event.response.failAuthentication = true;
} else if (
event.request.session &&
event.request.session.length &&
event.request.session.slice(-1)[0].challengeResult === true
) {
// The user provided the right answer; succeed auth
event.response.issueTokens = true;
event.response.failAuthentication = false;
} else {
// The user did not provide a correct answer yet; present challenge
event.response.issueTokens = false;
event.response.failAuthentication = false;
event.response.challengeName = 'CUSTOM_CHALLENGE';
}
return event;
};
Run Code Online (Sandbox Code Playgroud)
创建身份验证挑战Lambda
exports.handler = async event => {
if (event.request.challengeName == 'CUSTOM_CHALLENGE') {
// The value set for publicChallengeParameters is arbitrary for our
// purposes, but something must be set
event.response.publicChallengeParameters = { foo: 'bar' };
}
return event;
};
Run Code Online (Sandbox Code Playgroud)
验证身份验证挑战Lambda
exports.handler = async event => {
if (event.request.challengeName == 'CUSTOM_CHALLENGE') {
// The value set for publicChallengeParameters is arbitrary for our
// purposes, but something must be set
event.response.publicChallengeParameters = { foo: 'bar' };
}
return event;
};
Run Code Online (Sandbox Code Playgroud)
然后,我可以使用amazon-cognito-identity-js使用一些JS 来提供机密并获得令牌:
var authenticationData = {
Username : 'username'
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId : '...', // Your user pool id here
ClientId : '...' // Your client id here
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username : 'username',
Pool : userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.setAuthenticationFlowType('CUSTOM_AUTH');
cognitoUser.initiateAuth(authenticationDetails, {
onSuccess: function(result) {
// User authentication was successful
},
onFailure: function(err) {
// User authentication was not successful
},
customChallenge: function(challengeParameters) {
// User authentication depends on challenge response
var challengeResponses = 'secret'
cognitoUser.sendCustomChallengeAnswer(challengeResponses, this);
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6432 次 |
最近记录: |