bookingsync-engine 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 +4 -4
- data/README.md +8 -2
- data/lib/bookingsync/engine.rb +0 -2
- data/lib/bookingsync/engine/auth_helpers.rb +3 -3
- data/lib/bookingsync/engine/model.rb +2 -2
- data/lib/bookingsync/engine/version.rb +1 -1
- data/lib/generators/bookingsync/install/templates/create_bookingsync_accounts.rb +1 -1
- data/spec/dummy/config/environments/production.rb +1 -1
- data/spec/dummy/config/environments/test.rb +1 -1
- data/spec/dummy/db/migrate/20140522110454_add_o_auth_fields_to_accounts.rb +2 -2
- data/spec/dummy/db/schema.rb +2 -2
- data/spec/fixtures/accounts.yml +2 -2
- data/spec/models/account_spec.rb +6 -6
- metadata +3 -4
- data/lib/bookingsync/engine/token_helpers.rb +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2e9e1a1b065139938c74227068d00fa52244973
|
4
|
+
data.tar.gz: 29b802bfa945e74dd09ad882090303e13d19966c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79ec41bdd62cf2891666617393585a9e9ee008bc4e6009332c0cdd4c17e5e7043e7e203a6b7f4db60f484811e7dd97689e47d6ff40aac199d19ec85a91c25f63
|
7
|
+
data.tar.gz: 3e64fbceabffa3bb2211098586222228b424b9520c33e9ec4a3d67e3d7d00ed058c75295bf25e701383b3dd90a780e22e6acc2f70cf7170bf4c86c4147db3b64
|
data/README.md
CHANGED
@@ -25,11 +25,17 @@ Then bundle install:
|
|
25
25
|
bundle install
|
26
26
|
```
|
27
27
|
|
28
|
-
Then BookingSync routes
|
28
|
+
Then mount BookingSync Authorization routes inside `routes.rb`:
|
29
29
|
```ruby
|
30
30
|
mount BookingSync::Engine => '/'
|
31
31
|
```
|
32
32
|
|
33
|
+
This will add the following routes:
|
34
|
+
|
35
|
+
* `/auth/bookingsync/callback`
|
36
|
+
* `/auth/failure`
|
37
|
+
* `/signout`
|
38
|
+
|
33
39
|
BookingSync Engine uses the `Account` model to authenticate each BookingSync Account, if you do not have an `Account` model yet, create one:
|
34
40
|
|
35
41
|
```console
|
@@ -39,7 +45,7 @@ rails g model Account
|
|
39
45
|
Then, generate a migration to add OAuth fields for the `Account` class:
|
40
46
|
|
41
47
|
```console
|
42
|
-
rails g migration AddOAuthFieldsToAccounts provider:string
|
48
|
+
rails g migration AddOAuthFieldsToAccounts provider:string synced_id:integer:index \
|
43
49
|
name:string oauth_access_token:string oauth_refresh_token:string \
|
44
50
|
oauth_expires_at:string
|
45
51
|
```
|
data/lib/bookingsync/engine.rb
CHANGED
@@ -25,13 +25,11 @@ module BookingSync
|
|
25
25
|
require "bookingsync/engine/helpers"
|
26
26
|
require "bookingsync/engine/session_helpers"
|
27
27
|
require "bookingsync/engine/auth_helpers"
|
28
|
-
require "bookingsync/engine/token_helpers"
|
29
28
|
|
30
29
|
ActiveSupport.on_load :action_controller do
|
31
30
|
include BookingSync::Engine::Helpers
|
32
31
|
include BookingSync::Engine::SessionHelpers
|
33
32
|
include BookingSync::Engine::AuthHelpers
|
34
|
-
include BookingSync::Engine::TokenHelpers
|
35
33
|
end
|
36
34
|
end
|
37
35
|
|
@@ -11,16 +11,16 @@ module BookingSync::Engine::AuthHelpers
|
|
11
11
|
|
12
12
|
# @return [Account, nil] currently authorized Account or nil if unauthorized
|
13
13
|
def current_account
|
14
|
-
@current_account ||= ::Account.find_by(
|
14
|
+
@current_account ||= ::Account.find_by(synced_id: session[:account_id]) if session[:account_id].present?
|
15
15
|
end
|
16
16
|
|
17
17
|
# Callback after account is authorized.
|
18
18
|
#
|
19
|
-
# Stores the authorized account's
|
19
|
+
# Stores the authorized account's synced_id in the session.
|
20
20
|
#
|
21
21
|
# @param account [Account] the just authorized account
|
22
22
|
def account_authorized(account)
|
23
|
-
session[:account_id] = account.
|
23
|
+
session[:account_id] = account.synced_id.to_s
|
24
24
|
end
|
25
25
|
|
26
26
|
# Clear authorization if the account passed from the BookingSync app store
|
@@ -2,13 +2,13 @@ module BookingSync::Engine::Model
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
included do
|
5
|
-
validates :
|
5
|
+
validates :synced_id, uniqueness: true
|
6
6
|
scope :authorized, -> { where.not(oauth_access_token: nil) }
|
7
7
|
end
|
8
8
|
|
9
9
|
module ClassMethods
|
10
10
|
def from_omniauth(auth)
|
11
|
-
find_or_initialize_by(
|
11
|
+
find_or_initialize_by(synced_id: auth.uid, provider: auth.provider).tap do |account|
|
12
12
|
account.name = auth.info.business_name
|
13
13
|
account.update_token(auth.credentials)
|
14
14
|
account.save!
|
@@ -20,7 +20,7 @@ Dummy::Application.configure do
|
|
20
20
|
# config.action_dispatch.rack_cache = true
|
21
21
|
|
22
22
|
# Disable Rails's static asset server (Apache or nginx will already do this).
|
23
|
-
config.
|
23
|
+
config.serve_static_files = false
|
24
24
|
|
25
25
|
# Compress JavaScripts and CSS.
|
26
26
|
config.assets.js_compressor = :uglifier
|
@@ -13,7 +13,7 @@ Dummy::Application.configure do
|
|
13
13
|
config.eager_load = false
|
14
14
|
|
15
15
|
# Configure static asset server for tests with Cache-Control for performance.
|
16
|
-
config.
|
16
|
+
config.serve_static_files = true
|
17
17
|
config.static_cache_control = "public, max-age=3600"
|
18
18
|
|
19
19
|
# Show full error reports and disable caching.
|
@@ -1,8 +1,8 @@
|
|
1
1
|
class AddOAuthFieldsToAccounts < ActiveRecord::Migration
|
2
2
|
def change
|
3
3
|
add_column :accounts, :provider, :string
|
4
|
-
add_column :accounts, :
|
5
|
-
add_index :accounts, :
|
4
|
+
add_column :accounts, :synced_id, :integer
|
5
|
+
add_index :accounts, :synced_id
|
6
6
|
add_column :accounts, :name, :string
|
7
7
|
add_column :accounts, :oauth_access_token, :string
|
8
8
|
add_column :accounts, :oauth_refresh_token, :string
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -17,13 +17,13 @@ ActiveRecord::Schema.define(version: 20140522110454) do
|
|
17
17
|
t.datetime "created_at"
|
18
18
|
t.datetime "updated_at"
|
19
19
|
t.string "provider"
|
20
|
-
t.integer "
|
20
|
+
t.integer "synced_id"
|
21
21
|
t.string "name"
|
22
22
|
t.string "oauth_access_token"
|
23
23
|
t.string "oauth_refresh_token"
|
24
24
|
t.string "oauth_expires_at"
|
25
25
|
end
|
26
26
|
|
27
|
-
add_index "accounts", ["
|
27
|
+
add_index "accounts", ["synced_id"], name: "index_accounts_on_synced_id"
|
28
28
|
|
29
29
|
end
|
data/spec/fixtures/accounts.yml
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
one:
|
4
4
|
provider: MyString
|
5
|
-
|
5
|
+
synced_id: 1
|
6
6
|
name: MyString
|
7
7
|
oauth_access_token: MyString
|
8
8
|
oauth_refresh_token: MyString
|
@@ -10,7 +10,7 @@ one:
|
|
10
10
|
|
11
11
|
two:
|
12
12
|
provider: MyString
|
13
|
-
|
13
|
+
synced_id: 1
|
14
14
|
name: MyString
|
15
15
|
oauth_access_token: MyString
|
16
16
|
oauth_refresh_token: MyString
|
data/spec/models/account_spec.rb
CHANGED
@@ -14,16 +14,16 @@ RSpec.describe Account, type: :model do
|
|
14
14
|
end
|
15
15
|
|
16
16
|
describe "validations" do
|
17
|
-
it { is_expected.to validate_uniqueness_of(:
|
17
|
+
it { is_expected.to validate_uniqueness_of(:synced_id) }
|
18
18
|
end
|
19
19
|
|
20
20
|
describe ".from_omniauth" do
|
21
|
-
before { Account.create!(provider: "bookingsync",
|
21
|
+
before { Account.create!(provider: "bookingsync", synced_id: 456) }
|
22
22
|
|
23
23
|
let(:auth) { OmniAuth.config.mock_auth[:bookingsync] }
|
24
24
|
|
25
25
|
context "when account exists" do
|
26
|
-
let!(:account) { Account.create!(provider: "bookingsync",
|
26
|
+
let!(:account) { Account.create!(provider: "bookingsync", synced_id: 123) }
|
27
27
|
|
28
28
|
it "loads the existing account" do
|
29
29
|
expect(Account.from_omniauth(auth)).to eql(account)
|
@@ -49,8 +49,8 @@ RSpec.describe Account, type: :model do
|
|
49
49
|
describe "the newly created account" do
|
50
50
|
let!(:account) { Account.from_omniauth(auth) }
|
51
51
|
|
52
|
-
it "sets
|
53
|
-
expect(account.
|
52
|
+
it "sets synced_id and provider from auth" do
|
53
|
+
expect(account.synced_id).to eq 123
|
54
54
|
expect(account.provider).to eq "bookingsync"
|
55
55
|
end
|
56
56
|
|
@@ -61,7 +61,7 @@ RSpec.describe Account, type: :model do
|
|
61
61
|
|
62
62
|
describe "#token" do
|
63
63
|
let(:expires_at) { 1.day.from_now.to_i }
|
64
|
-
let!(:account) { Account.create!(
|
64
|
+
let!(:account) { Account.create!(synced_id: 123, oauth_access_token: "token",
|
65
65
|
oauth_refresh_token: "refresh_token", oauth_expires_at: expires_at) }
|
66
66
|
|
67
67
|
context "when the stored token is fresh" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bookingsync-engine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sebastien Grosjean
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-07-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -158,7 +158,6 @@ files:
|
|
158
158
|
- lib/bookingsync/engine/helpers.rb
|
159
159
|
- lib/bookingsync/engine/model.rb
|
160
160
|
- lib/bookingsync/engine/session_helpers.rb
|
161
|
-
- lib/bookingsync/engine/token_helpers.rb
|
162
161
|
- lib/bookingsync/engine/version.rb
|
163
162
|
- lib/generators/bookingsync/install/install_generator.rb
|
164
163
|
- lib/generators/bookingsync/install/templates/create_bookingsync_accounts.rb
|
@@ -236,7 +235,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
236
235
|
version: '0'
|
237
236
|
requirements: []
|
238
237
|
rubyforge_project:
|
239
|
-
rubygems_version: 2.
|
238
|
+
rubygems_version: 2.4.5
|
240
239
|
signing_key:
|
241
240
|
specification_version: 4
|
242
241
|
summary: A Rails engine to simplify integration with BookingSync API
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module BookingSync::Engine::TokenHelpers
|
2
|
-
extend ActiveSupport::Concern
|
3
|
-
|
4
|
-
private
|
5
|
-
|
6
|
-
# OAuth access token for the current account. Will refresh the token
|
7
|
-
# if it's expired and store the new token in the database.
|
8
|
-
#
|
9
|
-
# @return [OAuth2::AccessToken] access token for current account
|
10
|
-
def current_account_token
|
11
|
-
current_account.token
|
12
|
-
end
|
13
|
-
|
14
|
-
# OAuth access token for the application. The token is obtained from
|
15
|
-
# {BookingSync::Engine#appliation_token}.
|
16
|
-
#
|
17
|
-
# Will fetch new token if the current one is expired.
|
18
|
-
#
|
19
|
-
# The token is stored in thread local storage, to reduce the amount of
|
20
|
-
# token requests.
|
21
|
-
#
|
22
|
-
# @return [OAuth2::AccessToken] access token for application
|
23
|
-
def application_token
|
24
|
-
token = Thread.current[:_bookingsync_application_token]
|
25
|
-
if token.nil? || token.expired?
|
26
|
-
token = Thread.current[:_bookingsync_application_token] =
|
27
|
-
BookingSync::Engine.application_token
|
28
|
-
end
|
29
|
-
token
|
30
|
-
end
|
31
|
-
end
|