bookingsync_application 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f8fb2b1a6a8bcefd6d73a7e9e745b6aa7600df8
4
- data.tar.gz: cdd243437c4a1f7f6cdd41277699217b261ebb8e
3
+ metadata.gz: 90582eda500acd3957936b6500efc62c9d806a77
4
+ data.tar.gz: 86ae92357ae31b8d8665f2399e812c20a0093488
5
5
  SHA512:
6
- metadata.gz: 6cb676ca895252b170785de522dbd12581c1730f3cf57a988db515a8d6b053915277e0d355f0f0eb0e0dc6f40f238bb438d969cf2308cede8f36227a65a5ef18
7
- data.tar.gz: 0f3e893c58e1b3a569f0f2c8fe06fdda9d4d995af8256d8945fcb6e465b8e5dfeaa6a06fea982106578e7393c3e35202127bb764f228c18cccc20896906abc52
6
+ metadata.gz: 30b3703d3986fe9e4f275bfbdf5e65eb36ccc013a8fe343266a40b737ee2ab38f4753f8ddb34a26934953701ae39b33d260e68f3edb50bf0aeeb5408474b9758
7
+ data.tar.gz: 86e2ad190884380f79b11422faed1fbcd048b25056b2882979e4c687b169452c746e7668a2549cde504d2b144b6da5bc5de2a48c9b2eb1b04707c9ecaca5e2dc
data/README.md CHANGED
@@ -76,9 +76,9 @@ We now want to provide secured controllers, this controllers will be accessible
76
76
 
77
77
  You have 2 options pre built for this:
78
78
 
79
- 1) Create base admin controller (it will be `json` based):
79
+ 1) Create base API controller (it will be `json` based):
80
80
  ```
81
- class Admin::BaseController < BookingsyncApplication::Admin::BaseController
81
+ class Api::BaseController < BookingsyncApplication::Api::BaseController
82
82
  end
83
83
  ```
84
84
 
@@ -87,13 +87,46 @@ end
87
87
  class Admin::BaseHTMLController < ApplicationController
88
88
  respond_to :html
89
89
 
90
- include BookingsyncApplication::Admin::CommonBaseController
90
+ include BookingsyncApplication::Controllers::CommonBase
91
91
  end
92
92
  ```
93
93
 
94
94
 
95
95
  _Note: When saving new token, this gem uses a separate thread with new db connection to ensure token save (in case of a rollback in the main transaction). To make room for the new connections, it is recommended to increase db `pool` size by 2-3._
96
96
 
97
+ ### BookingSync Universe API
98
+
99
+ You can expose the application API by taking advantage of BookingSync Universe API concept. BookingSync Universe API is about making all the APIs accessible by the same token that is acquired in standard OAuth flow as outlined in the [docs](http://developers.bookingsync.com/reference/authorization/). The authentication is handled on BookingSync Core API, but the authorization is up to the application to handle.
100
+
101
+ To enable BookingSync Universe API for the controller, include `BookingsyncApplication::Controllers::BookingsyncUniverseApiAccess` module:
102
+
103
+ ``` ruby
104
+ class Api::ControllerForBookingsyncUniverseApi < BookingsyncApplication::Api::BaseController
105
+ include BookingsyncApplication::Controllers::BookingsyncUniverseApiAccess
106
+
107
+ def index
108
+ head 200
109
+ end
110
+ end
111
+ ```
112
+
113
+ If the request includes `Authorization` header (you can read more about the expected format in the [docs](http://developers.bookingsync.com/reference/)), it will be proxied to BookingSync Core API to check if the token is valid or not. If the token is not valid, `401` error will be returned with the corresponding error in the body. If it's valid, the account that is the owner of the token will be authenticated. By default there is no any extra authorization layer and as long as BookingSync Universe API is enabled all the endpoints will be accessible. To handle authorization you can use `bookingsync_universe_authorize_request!` method:
114
+
115
+ ``` ruby
116
+ class Api::ControllerForBookingsyncUniverseApi < BookingsyncApplication::Api::BaseController
117
+ include BookingsyncApplication::Controllers::BookingsyncUniverseApiAccess
118
+
119
+ before_action -> { bookingsync_universe_authorize_request! :clients_read, :clients_write },
120
+ only: :index
121
+
122
+ def index
123
+ head 200
124
+ end
125
+ end
126
+ ```
127
+
128
+ `bookingsync_universe_authorize_request!` expects the list of [scopes](http://developers.bookingsync.com/reference/authorization/#scopes) that are required to access this endpoint. If at least one of them is present on the token, the request will be authorized. Otherwise `403` error will be returned with the corresponding error message in the body.
129
+
97
130
  ## Configuration
98
131
 
99
132
  The engine is configured by the following ENV variables:
@@ -1,9 +1,9 @@
1
1
  require 'jsonapi/resource_controller'
2
2
 
3
- class BookingsyncApplication::Admin::BaseController < JSONAPI::ResourceController
3
+ class BookingsyncApplication::Api::BaseController < JSONAPI::ResourceController
4
4
  before_action :set_json_format
5
5
 
6
- include BookingsyncApplication::Admin::CommonBaseController
6
+ include BookingsyncApplication::Controllers::CommonBase
7
7
 
8
8
  protected
9
9
 
@@ -0,0 +1,74 @@
1
+ module BookingsyncApplication
2
+ module Controllers
3
+ module BookingsyncUniverseApiAccess
4
+ private
5
+
6
+ def authenticate_account!
7
+ if auth = request.headers["Authorization"].presence
8
+ response = Faraday.new(url: bookingsync_url).get do |req|
9
+ req.url auth_path
10
+ req.headers["Authorization"] = auth
11
+ end
12
+ if response.success?
13
+ @scope = AuthorizationScope.from_response(response)
14
+
15
+ session[:account_id] = scope.account_id
16
+ else
17
+ render json: response.body, status: response.status and return
18
+ end
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def bookingsync_url
25
+ "#{ENV['BOOKINGSYNC_URL']}"
26
+ end
27
+
28
+ def auth_path
29
+ "/api/v3/auth"
30
+ end
31
+
32
+ def scope
33
+ @scope
34
+ end
35
+
36
+ def bookingsync_universe_authorize_request!(*required_scopes)
37
+ if !scope.allows_access_for?(Array(required_scopes).map(&:to_s))
38
+ render json: { errors: [ { code: :forbidden } ] }, status: 403 and return
39
+ end
40
+ end
41
+
42
+ class AuthorizationScope
43
+ attr_reader :response_hash
44
+ private :response_hash
45
+
46
+ def self.from_response(response)
47
+ new(JSON.parse(response.body))
48
+ end
49
+
50
+ def initialize(response_hash)
51
+ @response_hash = response_hash
52
+ end
53
+
54
+ def scopes
55
+ auth.fetch("scopes").map(&:to_s)
56
+ end
57
+
58
+ def account_id
59
+ auth.fetch("account_id")
60
+ end
61
+
62
+ def allows_access_for?(required_scopes)
63
+ required_scopes.any? { |scope| scopes.include?(scope) }
64
+ end
65
+
66
+ private
67
+
68
+ def auth
69
+ @auth ||= response_hash.fetch("auth")
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,6 +1,6 @@
1
1
  module BookingsyncApplication
2
- module Admin
3
- module CommonBaseController
2
+ module Controllers
3
+ module CommonBase
4
4
  def self.included(base)
5
5
  base.class_eval do
6
6
  force_ssl
@@ -1,3 +1,3 @@
1
1
  module BookingsyncApplication
2
- VERSION = '0.5.0'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -3,7 +3,8 @@ require 'synced'
3
3
  require 'dotenv-rails'
4
4
  require 'jsonapi-resources'
5
5
  require 'bookingsync_application/engine'
6
- require 'bookingsync_application/admin/common_base_controller'
6
+ require 'bookingsync_application/controllers/common_base'
7
+ require 'bookingsync_application/controllers/bookingsync_universe_api_access'
7
8
 
8
9
  module BookingsyncApplication
9
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bookingsync_application
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Nowicki
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-12-24 00:00:00.000000000 Z
13
+ date: 2017-06-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -18,28 +18,34 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: '4.1'
21
+ version: '4.2'
22
+ - - "<"
23
+ - !ruby/object:Gem::Version
24
+ version: '5.1'
22
25
  type: :runtime
23
26
  prerelease: false
24
27
  version_requirements: !ruby/object:Gem::Requirement
25
28
  requirements:
26
29
  - - ">="
27
30
  - !ruby/object:Gem::Version
28
- version: '4.1'
31
+ version: '4.2'
32
+ - - "<"
33
+ - !ruby/object:Gem::Version
34
+ version: '5.1'
29
35
  - !ruby/object:Gem::Dependency
30
36
  name: bookingsync-engine
31
37
  requirement: !ruby/object:Gem::Requirement
32
38
  requirements:
33
39
  - - "~>"
34
40
  - !ruby/object:Gem::Version
35
- version: 1.1.0
41
+ version: 2.0.1
36
42
  type: :runtime
37
43
  prerelease: false
38
44
  version_requirements: !ruby/object:Gem::Requirement
39
45
  requirements:
40
46
  - - "~>"
41
47
  - !ruby/object:Gem::Version
42
- version: 1.1.0
48
+ version: 2.0.1
43
49
  - !ruby/object:Gem::Dependency
44
50
  name: jsonapi-resources
45
51
  requirement: !ruby/object:Gem::Requirement
@@ -190,11 +196,12 @@ files:
190
196
  - MIT-LICENSE
191
197
  - README.md
192
198
  - Rakefile
193
- - app/controllers/bookingsync_application/admin/base_controller.rb
199
+ - app/controllers/bookingsync_application/api/base_controller.rb
194
200
  - app/controllers/bookingsync_application/webhooks/base_controller.rb
195
201
  - config/routes.rb
196
202
  - lib/bookingsync_application.rb
197
- - lib/bookingsync_application/admin/common_base_controller.rb
203
+ - lib/bookingsync_application/controllers/bookingsync_universe_api_access.rb
204
+ - lib/bookingsync_application/controllers/common_base.rb
198
205
  - lib/bookingsync_application/engine.rb
199
206
  - lib/bookingsync_application/spec_helper.rb
200
207
  - lib/bookingsync_application/version.rb
@@ -219,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
226
  version: '0'
220
227
  requirements: []
221
228
  rubyforge_project:
222
- rubygems_version: 2.5.2
229
+ rubygems_version: 2.6.10
223
230
  signing_key:
224
231
  specification_version: 4
225
232
  summary: A Rails engine to simplify building BookingSync Applications