Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I store api key in rails3?

What is the best practice for storing/retrieving API keys in rails3?

Should I create my own application yaml and access it through there? If so, how?

Sorry for the noob question...

like image 956
kayluhb Avatar asked Nov 12 '10 22:11

kayluhb


People also ask

Where should API keys be stored?

Instead of embedding your API keys in your applications, store them in environment variables or in files outside of your application's source tree. Do not store API keys in files inside your application's source tree.

Is it safe to store API keys in database?

API keys aren't as secure as authentication tokens (see Security of API keys), but they identify the application or project that's calling an API. They are generated on the project making the call, and you can restrict their use to an environment such as an IP address range, or an Android or iOS app.

Where can I pass API key?

You can pass in the API Key to our APIs either by using the HTTP Basic authentication header or by sending an api_key parameter via the query string or request body. If you use our client library CARTO. js, you only need to follow the authorization section and we will handle API Keys automatically for you.


1 Answers

I use the settingslogic plugin for things like this. Very easy to use.

Add settingslogic to your Gemfile and bundle install:

gem 'settingslogic'

Create a directory for your settings and place the settingslogic yaml in there:

/my_app/config/settings/my_settings.yml

You can include default settings and per environment settings. The file looks like this:

defaults: &defaults
  api_key: abc123

development:
  <<: *defaults

test:
  <<: *defaults

production:
  <<: *defaults

Add this file: app/models/my_settings.rb, start up your app and you are good to go

class MySettings < Settingslogic
  source "#{Rails.root}/config/settings/my_settings.yml"
  namespace Rails.env
end

Now you can use call these settings from anywhere in the app like so:

MySettings.api_key
like image 167
johnmcaliley Avatar answered Sep 29 '22 22:09

johnmcaliley