4

I'm developing for iOS and I need to make requests to certain APIs using an API key and a secret. However, I wouldn't like for it to be exposed in my source code and have the secret compromised when I push to my repository.

What is the best practice for this case? Write it in a separate file which I'll include in .gitignore?

Thanks

Renan
  • 1,910
  • 4
  • 22
  • 36

2 Answers2

1

Write it in a separate file which I'll include in .gitignore?

No, don't write it ever.
That means:

  • you don't write that secret within your repo (no need to gitignore it, or ot worry about adding/committing/pushing it by mistake)
  • you don't write it anywhere on your local drive (no need to worry about your computer stolen with that "secret" on it)

Store in your repo a script able to seek that secret from an external source (from outside of git repo) and load it in memory.
This is similar to a git credential-helper process, and that script would launch a process listening to localhost:port in order to serve that "secret" to you when you whenever you need it in the current session only.
Once the session is done, there is no trace left.
And that is the best practice to manage secret data.

You can trigger automatically that script on git checkout, if you declare it in a .gitattributes file as a content filter:

content filter

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    I didn't get the difference from doing what you suggested and writing it in a separate file which I will not commit (since it will be in gitignore). And if I do what you suggest, how can I retrieve the values of those keys inside my iOS application ("in memory" is a little vague at this point, no?). Thank you for your answer! – Renan May 25 '13 at 23:54
1

This is a very old question, but if anyone is seeing this in google I would suggest you try CloudKit for storing any App secrets (API keys, Oauth secrets). Only your app can access your app container and communication between Apple and your app is secure. You can check it out here.

matt
  • 515,959
  • 87
  • 875
  • 1,141
derickito
  • 695
  • 7
  • 25
  • And what if the user has iCloud turned off? What if we go into a tunnel and the network is unavailable? – matt Feb 16 '18 at 22:45
  • The user doesn't need to have iCloud turned on. Your apps public database is always available. As to your second point if you're using these secret keys to initiate a network call it will fail any ways. You don't have to call CloudKit every time you can use keychain to store it securely once you get it from the server. The point is to not ship it with the app. – derickito Feb 16 '18 at 22:53
  • Sorry my initial comment got cutoff. I answered above. – derickito Feb 16 '18 at 22:54