we are building a mobile app, that needs to access API gateway.
we want to secure the api by allowing only access from our mobile app.
reading some comments from a github source, they don’t recommend API keys.
but to use the signature version 4 instead.
This is impossible. Anything your mobile app can do can be done without the mobile app, by ripping the credentials out of your app.
A really half-assed approach is to validate the User Agent header, because this will exclude unmodified web browsers from hitting your endpoint. It doesn’t prevent anybody determined from doing it, however.
You’re probably better off tying the Auth in the API to your users, rather than to the App itself. When a user signs in to your app (let’s say via Cognito or Auth0), then you can have a custom authoriser within API gateway that checks the validity of the credentials (the JWT token) and allows or blocks access.
Three primary ways to secure your API only from your app are
IAM (The Sigv4 you mentioned)
Cognito
Lambda Authorizer
IAM is usually best suited for AWS to AWS type situations so you’ll be better off with either Cognito or Lambda Auth since the use case is a mobile app.
If your using amplify to build your mobile app, it has quite a few native integrations with Cognito.
You setup cognito to manage your mobile users for you and then you tell API gateway to authorize your request by deferring to the cognito user pool you set up to your app.
If your not using cognito, you can use lambda custom authorizers. This is usually best if your using an identity provider like AuthO or Okta. Or if your managing your own traditional user database (not recommended nowadays). Essentially lambda will pull the JWT token from the Authorization header and then ask your identity provider “hey, is this a valid token?” If it is then your can tell API gateway that the request is good to go
Confusing name, I know, but Private APIs are designed for when you need your API to be isolated to your local network (VPC). In this use case, mobile users shouldn’t have access to your VPC because we are hosting out API in a public fashion (mobile app users from around the world)
This. Tecnically you can’t verify if the request actually come from your app or not.
I would suggest to add cert pinning, so no one can sniff the request or put proxy between your app and your api. That way at least you make the cred leaking a bit harder.
You’ll need some logic to perform the authentication for you, API Gateway won’t do it on its own. Check into JWT and/or HMAC based API authorization. But this still won’t stop someone particularly invested in hitting your API simply doing so with their own code.
The best way is to leave auth to a service that does proper, secure auth and validate your users auth token through that service. Cognito would be the relevant AWS service but you can use something else if that’s your preference.
At work we use Okta and use a custom authorizer to read the users token, verify it with the okta auth server, pull some user related data and continue on with the request.
Depending on your app and whether you want the user to sign in each time or ‘remember’ them you’ll have to figure out how to store a secure token that can be used to reauthenticate at will. That I do not know, yet.
This method can be used by Lambda@edge to secure web content served from S3 also. We have a web front end that gets secured this way.
We also use API keys for B2B, but that API also uses ip whitelisting as there are only a handful of consumers.
Yeah someone who’s really devoting their time to hit any public API will be able to do so. You can get rid of a lot of the low hanging fruit, but it’s less security and more a matter of inconveniencing/discouraging others that might want to use your APIs in their apps.
I haven’t used it personally but shouldn’t it be possible to use Google’s SafetyNet and Apple’s DeviceCheck to validate that the requests are coming from real devices (in conjunction with any other methods)?
You need to authenticate your users first before anything else. You’ll want to put a WAF in front and use that to rate limit and prevent credential stuffing
You’ll want to issue your users some kind of token, a shared/private secret is one route to go (HMAC signing headers), use that to validate the users request to private APIs—again using WAF to protect the resource from rate limit issues