Skip to content

Commit 04fe86a

Browse files
committed
Add app authorization for each request
1 parent 690e8c5 commit 04fe86a

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121
/gems/*
2222
/bin/*
2323

24+
25+
# Ignore application configuration
26+
/config/application.yml

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ gem 'bcrypt-ruby', "~> 3.0.0"
1313
# JavaScript runtime
1414
gem 'therubyracer'
1515

16+
# Secure configuration file for Open-Source projects
17+
gem "figaro"
18+
1619
group :production do
1720
gem 'pg'
1821
end

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ GEM
5151
faker (1.2.0)
5252
i18n (~> 0.5)
5353
ffi (1.9.0)
54+
figaro (0.7.0)
55+
bundler (~> 1.0)
56+
rails (>= 3, < 5)
5457
formatador (0.2.4)
5558
guard (2.0.3)
5659
formatador (>= 0.2.4)
@@ -164,6 +167,7 @@ DEPENDENCIES
164167
email_validator
165168
factory_girl_rails
166169
faker
170+
figaro
167171
guard-rspec
168172
launchy
169173
pg

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# LaboWebESGI API
2+
3+
## Requirements
4+
5+
- Ruby 2
6+
- Rails 4 `gem install rails`
7+
- RSpec `gem install rspec`
8+
- Bundler `gem install bundler`
9+
10+
## Development
11+
12+
bundle install
13+
rake db:migrate
14+
rails s -p 5000
15+
16+
## Tests
17+
18+
rake db:migrate db:test:prepare
19+
rspec spec
20+
21+
Make sure you have created the file `config/application.yml` with the following content:
22+
23+
# Add application configuration variables here, as shown below.
24+
25+
AUTHORIZED_APPS:
26+
- 127.0.0.1
27+
- localhost
28+
29+
It will enable your client application in development mode to access to the API.

README.rdoc

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
class ApplicationController < ActionController::API
22
before_action :allow_cors
3+
before_action :is_an_authenticated_app?
34

45
def allow_cors
56
headers["Access-Control-Allow-Origin"] = "*"
67
headers["Access-Control-Allow-Methods"] = %w{GET POST PUT DELETE}.join(",")
78
headers["Access-Control-Allow-Headers"] =
89
%w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(",")
910
end
11+
12+
def is_an_authenticated_app?
13+
can_access = ENV['AUTHORIZED_APPS'].include? request.remote_host
14+
unless can_access
15+
render json: {error: 'This is not an authenticated app'}, status: 403
16+
end
17+
end
1018
end

0 commit comments

Comments
 (0)