I am developing an portal for IoT-units where they can register and add new units, where each unit can send measurements and data to a API.
The app is consisting of a user-portal, a bunch of nodeJs-clients and a REST API which the nodejs-clients send information to.
In my portal the users can log in and I can get a user session, but I cannot find any good manuals on how the nodejs-clients are to programmaticly authenticate against Cognito and get a JWT-token that they can pass to my API.
Any ideas where I can find a good guide for this setup? What im trying to accomplish is posting username/password and getting back a JWT that I can use for request to my API.
I have been able to find my endpoint, but can not find any good resources on how to configure the http-call to authenticate a user and not a API-Client. POST: https://my-domain.auth.eu-west-1.amazoncognito.com/oauth2/token
All help appreciated!
Related discussions:
For service-to-service auth you should consider not using the regular username/password flows but instead using client apps and the client credentials grant. I wasn’t able to find any specific docs on this in the infamous cognito docs, but here’s a quick walkthrough I found with Google: https://lobster1234.github.io/2018/05/31/server-to-server-auth-with-amazon-cognito/
I’m generally an auth0 user so I’ve never done this with cognito myself, but the docs I linked line up with what I know about the process so it should be good to go.
Note that if one service is invoking another on behalf of a user instead of on behalf of itself, you should instead authenticate using the requestor’s own JWT. This makes things easier to reason about and more auditable in the long run - but it’s not easy or possible with all use cases (eg async invocations that may trigger after the jwt has expired). But do keep it in mind.
That’s my question too. How to handle service to service auth vs user invocation of service auth. I am not on aws but I assume practices/design would be same. Would you happen to have good resource to read more about it?
On my phone right now but I found a great video for this I will post a link tomorrow from my computer.
Thank You for the answer! API-keys sound like a good solution, do you know if this is supported in cognito or do I have to build a store for them? Also, are there no oauth2-flows that support this?
Thanks for your reply! I’ve used client apps successfully but I can’t generate a new client app for each unit that a user has I’m afraid. What I’m looking for I think is the oauth2 way of using api-keys.
So i actually found a solution on Google on how to login via rest and get a jwt, but a better way would be to be able to generate api-keys that the consumer can use. All help on this is appreciated!
Solution found on: https://stackoverflow.com/questions/37941780/what-is-the-rest-or-cli-api-for-logging-in-to-amazon-cognito-user-pools
https://www.youtube.com/watch?v=EaDMG4amEfk&t=1270s
As promised here is the video. It’s 1 of 3 and it is set up to use a react environment but you could change it to any javascript environment. Goes over persistent connection too. Really amazing job I thought.