Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Can't see how to hide this API Key

I have the following code in my angular app declaration - an API key for Facebook (to implement Share button):

.run(function($FB){
  $FB.init('9xxxxxxxxxxxx94');
})

So i know the general answer to this - 'API keys should be kept on the server side', however I don't see how i actually implement this.

The share call-method is made on the front end, so even if my server kept the API key and sent it, surely it's still visible on the front end, else how would the share button work?

So my question, how do I hide that Facebook API Key?

Thanks.

like image 899
userMod2 Avatar asked Apr 05 '16 10:04

userMod2


1 Answers

Requesting the key

The first thing that happens is that the client will request a key. This will only happen on certain pages like the sign up and log in pages. The idea here is that we want to make sure that only users browsing with a known client (in this case the official website or core client as it’s called) are allowed to take actions like creating or authenticating a user.

So when the client app requests the login page the server generates a unique token based on information sent in the request. The information used is always something the server knows, something the client knows, and something both know. So for example the server can generate a unique key based on User agent + current time + secret key. The server generates a hash based on this information and then stores a cookie containing only the hash on the client machine.

Setting permissions

At this point our key really isn’t a key anymore. It has been transformed into an access token. The server should then take this access token and store it for later retrieval. You can put the key in a database but since data of this type needs to be retrieved often I would suggest using a key-value store like Redis to cut down on database reads/writes and boost performance.

When you store the token you should also store a separate piece of data to indicate what permissions are associated with the token. In this case our token is acting only as a way to register and authenticate users so we store it next to a value that indicates who the token belongs to (the app’s web UI) and what permissions it has (limited to create and authenticate users). We treat it just like we would any other API client that way we can capture stats and control how it is used.

Authorizing a request

When the client then makes the POST request to create a new user or log in the server will check to see if the client sent an identifying cookie along with the request. If not, we reject the request. If it does send the cookie, the server should once again generate the hash using the values used previously (these values are either already known or sent with the request anyway so we’re not really taxing the server much) compare it to the cookie being sent to us, and if the values match allow the request to proceed.

Sources - Securing API Keys

OR

Simply send a request to your Server and let him handle your request with the hidden API-key and just return the result of your request to your front-end.

like image 161
marpme Avatar answered Sep 30 '22 16:09

marpme