Skip to content

Commit 98137d2

Browse files
committed
Merge branch 'develop'
2 parents 38e5bc6 + 013c729 commit 98137d2

File tree

20 files changed

+145
-172
lines changed

20 files changed

+145
-172
lines changed

.travis.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
sudo: required
1+
sudo: false
22
language: ruby
33
cache: bundler
44
rvm:
55
- 2.3.0
66
before_install:
7-
- wget https://s3.amazonaws.com/cb-content-enablement-misc/resources/phantomjs-2.1.1-linux-x86_64.tar.bz2
8-
- tar -xjf phantomjs-2.1.1-linux-x86_64.tar.bz2
9-
- sudo rm -rf /usr/local/phantomjs/bin/phantomjs
10-
- sudo mv phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/phantomjs/bin/phantomjs
11-
127
- mkdir /tmp/elasticsearch
138
- wget -O - https://download.elasticsearch.org/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/${ES_VERSION}/elasticsearch-${ES_VERSION}.tar.gz | tar xz --directory=/tmp/elasticsearch --strip-components=1
149
- /tmp/elasticsearch/bin/elasticsearch -d -D es.path.data=/tmp -D es.discovery.zen.ping.multicast.enabled=false

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ gem 'cortex-exceptions', '~> 0.0.4'
1717
gem 'grape', '~> 0.14'
1818
gem 'grape-entity', '~> 0.5.1'
1919
gem 'grape-swagger', '~> 0.20.1'
20-
gem 'grape-cache_control', '~> 1.0.1'
2120
gem 'redis-rails', '~> 4.0'
2221

2322
# Authorization

Gemfile.lock

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,6 @@ GEM
291291
rack-accept
292292
rack-mount
293293
virtus (>= 1.0.0)
294-
grape-cache_control (1.0.1)
295-
grape (~> 0.3)
296294
grape-entity (0.5.1)
297295
activesupport
298296
multi_json (>= 1.3.2)
@@ -618,7 +616,6 @@ DEPENDENCIES
618616
font-awesome-sass (~> 4.5.0)
619617
gon (~> 6.0.1)
620618
grape (~> 0.14)
621-
grape-cache_control (~> 1.0.1)
622619
grape-entity (~> 0.5.1)
623620
grape-kaminari!
624621
grape-swagger (~> 0.20.1)

app/api/api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class API < Grape::API
44
rack_response({message: 'Validation failed', errors: errors}.to_json, 422)
55
end
66

7-
include ::V1::Auth
8-
helpers ::V1::APIHelper
7+
include ::Helpers::AuthHelper
8+
helpers ::Helpers::APIHelper
99
mount ::V1::API
1010
end

app/api/api_helper.rb

Lines changed: 0 additions & 30 deletions
This file was deleted.

app/api/auth.rb

Lines changed: 0 additions & 92 deletions
This file was deleted.

app/api/helpers/api_helper.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Helpers
2+
module APIHelper
3+
def logger
4+
::API.logger
5+
end
6+
7+
def current_tenant
8+
current_user.tenant
9+
end
10+
11+
# API Errors
12+
def bad_request!
13+
render_api_error!('(400) Bad Request', 400)
14+
end
15+
16+
def forbidden!
17+
render_api_error!('(403) Forbidden', 403)
18+
end
19+
20+
def not_found!
21+
render_api_error!('(404) Not found', 404)
22+
end
23+
24+
def unauthorized!
25+
render_api_error!('(401) Unauthorized', 401)
26+
end
27+
28+
def render_api_error!(message, status)
29+
error!({message: message}, status)
30+
end
31+
end
32+
end

app/api/helpers/auth_helper.rb

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
require 'doorkeeper/grape/authorization_decorator'
2+
3+
module Helpers
4+
module AuthHelper
5+
extend ActiveSupport::Concern
6+
7+
included do
8+
use Rack::OAuth2::Server::Resource::Bearer, 'OAuth2' do |request|
9+
# Yield access token to store it in the env
10+
request.access_token
11+
end
12+
13+
helpers HelperMethods
14+
end
15+
16+
module HelperMethods
17+
def current_user
18+
@current_user ||= find_current_user
19+
end
20+
21+
def current_user!
22+
current_user.anonymous? ? unauthorized! : current_user
23+
end
24+
25+
def find_access_token
26+
@access_token ||= Doorkeeper.authenticate(doorkeeper_request, Doorkeeper.configuration.access_token_methods)
27+
end
28+
29+
def authenticate!
30+
unauthorized! unless current_user
31+
end
32+
33+
def authorize!(action, subject)
34+
unless abilities.allowed?(current_user, action, subject)
35+
# TODO: Un-comment on May 12, date of OAuth Scope Enforcement rollout
36+
# forbidden!
37+
end
38+
end
39+
40+
def require_scope!(scopes)
41+
return unless find_access_token
42+
scopes = [scopes] unless scopes.kind_of? Array
43+
44+
unless (find_access_token.scopes.to_a & scopes) == scopes
45+
forbidden!
46+
end
47+
end
48+
49+
def can?(object, action, subject)
50+
abilities.allowed?(object, action, subject)
51+
end
52+
53+
private
54+
55+
def abilities
56+
@abilities ||= begin
57+
abilities = Six.new
58+
abilities << Abilities::Ability
59+
abilities
60+
end
61+
end
62+
63+
def find_current_user
64+
if find_access_token
65+
lookup_owner
66+
elsif warden_current_user
67+
warden_current_user
68+
else
69+
User.anonymous
70+
end
71+
end
72+
73+
def lookup_owner
74+
if find_access_token.resource_owner_id.present?
75+
User.find_by_id(find_access_token.resource_owner_id)
76+
else
77+
find_access_token.application.owner
78+
end
79+
end
80+
81+
def doorkeeper_request
82+
@doorkeeper_request ||= Doorkeeper::Grape::AuthorizationDecorator.new(request)
83+
end
84+
85+
def warden
86+
@warden ||= env['warden']
87+
end
88+
89+
def warden_current_user
90+
warden ? warden.user : nil
91+
end
92+
end
93+
end
94+
end

app/api/v1/resources/posts.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ class Posts < Grape::API
44
helpers ::V1::Helpers::SharedParamsHelper
55
helpers ::V1::Helpers::ParamsHelper
66

7-
before do
8-
cache_control :public, max_age: 2592000, s_maxage: 2592000
9-
end
10-
117
resource :posts do
128
include Grape::Kaminari
139
helpers ::V1::Helpers::PostsHelper

app/api/v1/resources/webpages.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ module Resources
33
class Webpages < Grape::API
44
helpers ::V1::Helpers::ParamsHelper
55

6-
before do
7-
cache_control :public, max_age: 2592000, s_maxage: 2592000
8-
end
9-
106
resource :webpages do
117
include Grape::Kaminari
128
helpers ::V1::Helpers::WebpagesHelper

0 commit comments

Comments
 (0)