Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'd like to get Amplify auth username on Amplify function with Amplify REST API. #2534

Closed
hi120ki opened this issue Oct 9, 2019 · 9 comments
Labels
duplicate If marked with duplicate, issue will be closed & original will be added for traceability feature-request Request a new feature functions Issues tied to the functions category

Comments

@hi120ki
Copy link

hi120ki commented Oct 9, 2019

This is future request of Amplify REST API authorisation.

I'd like to get Cognito (Amplify auth) username on Amplify function with Amplify REST API.
(A logged-in user call my api, I'd like to get this user's username on Lambda.)

First, I created Amplify auth, and created Amplify REST API.
(function type > Serverless framework with express.js)
(Restrict API access > Authenticated users only)

I'd like to get Cognito username on Amplify function, but I can't get property related with Cognito username on Lambda function.

(This is my lambda code. I think I can get username from "req" property, but I can't.)

app.post("/test", function(req, res) {
  ...

Maybe Amplify REST API's "Restrict API access" is configured by IAM Permissions with Cognito.
https://docs.aws.amazon.com/apigateway/latest/developerguide/permissions.html
I think this is why I can't get username on Lambda.

An alternative solution is using "Control Access to a REST API Using Amazon Cognito User Pools as Authorizer".
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html

Will you support this way?

Getting username who call API is necessary to build secure system.
I'm waiting for your great support. Thank you.

@jkeys-ecg-nmsu
Copy link

jkeys-ecg-nmsu commented Oct 9, 2019

Hey @hi120ki, since you're talking about authenticated users, you should be able to get away with something like this:

import { Auth} from 'aws-amplify';

  //inside some async function, AFTER the user has authenticated with Cognito
  const tokens = await Auth.currentSession();
  const userName = tokens.getIdToken().payload['cognito:username'];

Let me know if that is helpful.

Edit: Wow, sorry, I missed the point. This is inside a Lambda, not the client, so importing Amplify is a bit overkill.

It seems like the best solution would be to set a custom header on app initialization that always sends the username as a header to your lambda. Then you just check for that header in your lambda and return a 4xx if the header is not present.

Edit2: Okay, upon multiple tired readings, I'm not sure which you are asking, but either one of the two solutions I previously posted should be helpful for you.

@elorzafe
Copy link
Contributor

elorzafe commented Oct 9, 2019

@hi120ki what if you send the JWT on the body of the request and then validate the token on your function and extract the username?

@yuth yuth added functions Issues tied to the functions category question General question pending-response Issue is pending response from the issue author labels Oct 9, 2019
@hi120ki
Copy link
Author

hi120ki commented Oct 10, 2019

Thank you for answering my question.

https://aws-amplify.github.io/docs/js/api#cognito-user-pools-authorization
I read this document, and tried to set JWT token into header, but this causes 403 error. (API Gateway blocked API call).

To do this, I have to set "custom authorizer" to API Gateway.
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html

But, today's amplify-cli may not support to set this.

@yuth yuth added feature-request Request a new feature and removed question General question pending-response Issue is pending response from the issue author labels Oct 10, 2019
@yuth
Copy link
Contributor

yuth commented Oct 10, 2019

I am adding this to our backlog, once the product team prioritizes this we will work on this issue.

@yuth yuth added the pending-review Pending review from core-team label Oct 10, 2019
@jkeys-ecg-nmsu
Copy link

@hi120ki does the preflight request return 200?

@hi120ki
Copy link
Author

hi120ki commented Oct 16, 2019

@jkeys-ecg-nmsu Yes, OPTIONS request returns 200. But POST request returns 403.

@AlaDouagi
Copy link

@yuth is retrieving user data on back-end side (that made a request) still not possible with Amplify?
I'm concerned about many security flaws with the app that I'm currently working because of this feature lack...

@pnewhook
Copy link

pnewhook commented Jan 8, 2021

This seems strongly related to aws-amplify/amplify-category-api#345. I'm concerned the approach suggested above of passing username from the client would be a huge security hole.
Manually configuring a custom authorizer as @hi120ki suggests might be a good first step, but we need the CLI to not overwrite our configuration on every amplify push/publish

@josefaidt josefaidt removed the pending-review Pending review from core-team label May 2, 2022
@josefaidt
Copy link
Contributor

Hey @hi120ki 👋 as @pnewhook pointed out this is related to the feature request: aws-amplify/amplify-category-api#345

In the meantime we can leverage a REST API override to create and attach a Cognito authorizer to ultimately retrieve values such as username in our Lambda's:

/**
 * @type {import('@types/aws-lambda').APIGatewayProxyHandler}
 */
export async function handler(event) {
  console.log(`EVENT: ${JSON.stringify(event)}`)

  const { claims } = event.requestContext.authorizer
  return {
    statusCode: 200,
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Headers': '*',
    },
    body: JSON.stringify(`Hello, ${claims['cognito:username']}!`),
  }
}

Closing in favor of tracking aws-amplify/amplify-category-api#345, please subscribe to the linked issue for further updates 🙂

@josefaidt josefaidt closed this as not planned Won't fix, can't repro, duplicate, stale Jun 20, 2022
@josefaidt josefaidt added the duplicate If marked with duplicate, issue will be closed & original will be added for traceability label Jun 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate If marked with duplicate, issue will be closed & original will be added for traceability feature-request Request a new feature functions Issues tied to the functions category
Projects
None yet
Development

No branches or pull requests

7 participants