问题
I established a authentication flow with Facebook Login and AWS Cognito on the client site. Works fine. But now I need a reference of the user with its facebook id in a dynambodb table. Of course I could just call a AWS lambda function exposed via AWS API gateway, but how can I verify that the API call actually has a valid facebook id and that this facebook id matches the AWS Cognito Id. Maybe I am missing something here, I hope you guys can point me in the right direction ;) thanks!
回答1:
If you can key your ddb table by cognito id instead of facebook id, you can invoke api gateway with cognito credentials. If you use callee credentials when calling lambda you can access the cognito id via the token $context.identity.cognitoIdentityId
. This ensures the call was made by the owner of this id. You can further check that $context.identity.cognitoAuthenticationProvider
is graph.facebook.com
to ensure they authed via Facebook. Unfortunately, the facebook id is not passed in the credentials, so if you need it you will need a lookup table mapping cognito id to facebook id. For more details on the available tokens see here.
回答2:
I would suggest to do the following (I'm new to AWS as well, Let me know if you have any suggestions):
- Create a API Gateway
/fblogin
endpoint where you will POST the Facebook Access Token (You will authenticate with Facebook on the client side to get this token). That endpoint is linked to a Lambda function sayfb_login
. - In
fb_login
function you will authenticate with Amazon Cognito to get its credentials. You should have created a Federated Identity user pool in Cognito and assign appropriate roles to assume for this user pool. This helps if you want to restrict your API only to those who are authenticated and authorized. So, the result from Cognito will haveIdentityId
andCredentials
. You can return them as a result of your request to/fblogin
. - Using the above result you can sign the requests you send to your API on API Gateway (Or you can use AWS custom generated SDK for your API to handle the signing). On the API Gateway endpoints, enable CORS and authentication as
AWS_IAM
. This way, API Gateway verifies the user automatically by checking the signature in the request. You can get the User Id from$context.identity.cognitoIdentityId
as others suggested. This way, you can be sure that the user is authorized and authenticated.
Note: Make sure you implement /fblogin
endpoint on HTTPS, then the FB Access token will be secure. If not, it will be visible as plain text over HTTP.
Also, use a Dynamo DB table as a log for CognitoID - FacebookID
. You can incorporate this in the Step 2 Lambda function ifself or anything you think is appropriate.
来源:https://stackoverflow.com/questions/35140545/aws-cognito-lambda-user-credentials-in-dynamodb