clerk-rails 0.1.10 → 0.1.11

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
  SHA256:
3
- metadata.gz: 8c63b3805b3c868266fa141994d51861c36937e129d2ceac798968edd1179d65
4
- data.tar.gz: 93f0dad921f7969221f5355a1f193ff12352b2b63acf7f97f6aa2e22ca2ec33b
3
+ metadata.gz: 954cbd3886921775435d732704b6ae645237571ff1eb538979e86b252419a38d
4
+ data.tar.gz: bbcaa255ffb48a83f694398467e6afca6df2fc9e7138bdf45e9ed0628ad1c8fd
5
5
  SHA512:
6
- metadata.gz: c2cac89e76f26076e369e9682db9b9ca9b1a75c3511fcf78462f3b6997c2ace36eb571c48c2a4118837012a0e1a9b805b931f2ba2bdbda74e8a2c8444c04ba59
7
- data.tar.gz: beda17af8b83240de22cdb56d8c256b1a0ee7d27f4edbc98ed4abc5650f864c83109fb207e53016c9aad0fe72566af48171daa251232a5c4c0a234c11e4505e0
6
+ metadata.gz: 3e34b00a89204fa089c6534e385d167b3e0ec961257b1e18bc8463e598adad7ab1580c0799fcdcf8c37d2dabd928d7fe7d6d4fe888a2876f4712c5ae647640a8
7
+ data.tar.gz: e401f93db2c7bee14b7f70def532e247f97e400bc545c08040315be1c36c84d2f8a7b3724ca4f1d865018144bbec30709ac18977190d036a08b5c391e7b8e120
@@ -3,8 +3,8 @@ module Clerk
3
3
  self.abstract_class = true
4
4
  establish_connection Clerk.database_connection_url
5
5
  def self.clerk_table_name(table_name)
6
- version = Clerk::VERSION.split(".").map.with_index{|x, i| (i==2 ? "00" : x.rjust(2, '0')) }.join
7
- "instance.#{table_name}_#{version}_#{Clerk.key_secret}"
6
+ # version = Clerk::VERSION.split(".").map.with_index{|x, i| (i==2 ? "00" : x.rjust(2, '0')) }.join
7
+ "#{table_name}_01"
8
8
  end
9
9
 
10
10
  def self.clerk_table_name_nc(table_name)
@@ -0,0 +1,25 @@
1
+ module Clerk
2
+ class SessionToken < Clerk::ApplicationRecord
3
+ self.table_name = self.clerk_table_name("session_tokens")
4
+ self.primary_key = 'id'
5
+
6
+ belongs_to :account, class_name: "Clerk::Account"
7
+
8
+ def self.find_account(cookie:)
9
+ require "bcrypt"
10
+ begin
11
+ id, token, token_hash = Clerk.decrypt(cookie).split("--")
12
+ if BCrypt::Password.new(token_hash) == token
13
+ Account.joins(:session_token).where(token_hash: token_hash, )
14
+ SessionToken.eager_load(:account).find_by!(id: id, token_hash: token_hash)&.account
15
+ else
16
+ nil
17
+ end
18
+ rescue => e
19
+ puts "Error finding acount #{e}"
20
+ puts "Cookie #{cookie}"
21
+ nil
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1 +1,2 @@
1
- ::ActionController::Base.send :include, Clerk::ApplicationHelper
1
+ ::ActionController::Base.send :helper, Clerk::Helpers
2
+ ::ActionController::Base.send :include, Clerk::Helpers
data/lib/clerk-rails.rb CHANGED
@@ -2,6 +2,7 @@ require "clerk/version"
2
2
  require "clerk/api"
3
3
  require "clerk/engine"
4
4
  require "clerk/id"
5
+ require "clerk/helpers"
5
6
  require "faraday"
6
7
 
7
8
  module Clerk
@@ -11,15 +12,6 @@ module Clerk
11
12
  def configure
12
13
  @config = Configuration.new
13
14
  yield config
14
- # if Rails.env.development?
15
- config.environment = :development
16
- config.session_mode = :cookie_session
17
- config.cookie_host = nil
18
- # else
19
- # config.environment = client.environment.to_sym
20
- # config.session_mode = client.session_mode.to_sym
21
- # config.cookie_host = client.cookie_host
22
- # end
23
15
  end
24
16
 
25
17
  def database_connection_url
@@ -48,9 +40,41 @@ module Clerk
48
40
  def app_url
49
41
  @app_url ||= "https://#{Clerk.config.app_host}"
50
42
  end
43
+
44
+ def cipher_key
45
+ @cipher_key ||= ::Base64.strict_decode64(Clerk.config.cipher_key)
46
+ end
47
+
48
+ def decrypt(encrypted_message)
49
+ cipher = OpenSSL::Cipher.new("aes-256-gcm")
50
+
51
+ encrypted_data, iv, auth_tag = encrypted_message.split("--".freeze).map { |v| ::Base64.strict_decode64(v) }
52
+
53
+ # Currently the OpenSSL bindings do not raise an error if auth_tag is
54
+ # truncated, which would allow an attacker to easily forge it. See
55
+ # https://github.com/ruby/openssl/issues/63
56
+ raise InvalidMessage if (auth_tag.nil? || auth_tag.bytes.length != 16)
57
+
58
+ cipher.decrypt
59
+ cipher.key = cipher_key
60
+ cipher.iv = iv
61
+ cipher.auth_tag = auth_tag
62
+ cipher.auth_data = ""
63
+
64
+ message = cipher.update(encrypted_data)
65
+ message << cipher.final
66
+ message
67
+ end
51
68
  end
52
69
 
53
70
  class Configuration
54
- attr_accessor :app_host, :accounts_host, :database_url, :ngrok_authtoken, :environment, :session_mode, :last_account, :cookie_host, :tunnel_cert, :tunnel_key
71
+ attr_accessor :accounts_host,
72
+ :app_host,
73
+ :cipher_key,
74
+ :database_url,
75
+ :last_account,
76
+ :ngrok_authtoken,
77
+ :tunnel_cert,
78
+ :tunnel_key
55
79
  end
56
80
  end
data/lib/clerk/engine.rb CHANGED
@@ -13,8 +13,9 @@ module Clerk
13
13
  begin
14
14
  config_data = Clerk.api.get('/api/environment').data
15
15
  Clerk.configure do |config|
16
- config.app_host = config_data[:APP_HOST]
17
16
  config.accounts_host = config_data[:ACCOUNTS_HOST]
17
+ config.app_host = config_data[:APP_HOST]
18
+ config.cipher_key = config_data[:CIPHER_KEY]
18
19
  config.database_url = config_data[:DATABASE_URL]
19
20
  config.ngrok_authtoken = config_data[:NGROK_AUTHTOKEN]
20
21
  config.tunnel_cert = config_data[:TUNNEL_CERT]
@@ -0,0 +1,28 @@
1
+ # We're not including this in clerk-rails/app/helpers because it is injected
2
+ # into ActionController::Base via initializes/add_application_helpers and cannot be in the autoload path
3
+ # https://stackoverflow.com/questions/29636334/a-copy-of-xxx-has-been-removed-from-the-module-tree-but-is-still-active
4
+ module Clerk
5
+ module Helpers
6
+ def current_account
7
+ @clerk_current_account ||= SessionToken.find_account(
8
+ cookie: cookies[:clerk_session]
9
+ )
10
+ end
11
+
12
+ def clerk_sign_out_path
13
+ "#{Clerk.accounts_url}/sign_out"
14
+ end
15
+
16
+ def clerk_sign_in_path
17
+ "#{Clerk.accounts_url}"
18
+ end
19
+
20
+ def clerk_sign_up_path
21
+ "#{Clerk.accounts_url}/sign_up"
22
+ end
23
+
24
+ def clerk_add_card_path(account_id:, redirect_path:)
25
+ "#{Clerk.accounts_url}/add_card?account_id=#{account_id}&redirect_path=#{redirect_path}"
26
+ end
27
+ end
28
+ end
data/lib/clerk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Clerk
2
- VERSION = '0.1.10'
2
+ VERSION = '0.1.11'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clerk-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.1.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clerk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-08 00:00:00.000000000 Z
11
+ date: 2019-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bcrypt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: pg
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -136,7 +150,6 @@ files:
136
150
  - app/assets/javascripts/clerk/application.js
137
151
  - app/assets/stylesheets/clerk/application.css
138
152
  - app/controllers/clerk/application_controller.rb
139
- - app/helpers/clerk/application_helper.rb
140
153
  - app/jobs/clerk/application_job.rb
141
154
  - app/mailers/clerk/application_mailer.rb
142
155
  - app/models/clerk/account.rb
@@ -146,6 +159,7 @@ files:
146
159
  - app/models/clerk/payment_method.rb
147
160
  - app/models/clerk/plan.rb
148
161
  - app/models/clerk/role.rb
162
+ - app/models/clerk/session_token.rb
149
163
  - app/models/clerk/subscription.rb
150
164
  - app/models/concerns/clerk/clerked.rb
151
165
  - app/models/concerns/clerk/errors.rb
@@ -157,6 +171,7 @@ files:
157
171
  - lib/clerk-rails.rb
158
172
  - lib/clerk/api.rb
159
173
  - lib/clerk/engine.rb
174
+ - lib/clerk/helpers.rb
160
175
  - lib/clerk/id.rb
161
176
  - lib/clerk/tunnel.rb
162
177
  - lib/clerk/version.rb
@@ -1,48 +0,0 @@
1
- module Clerk
2
- module ApplicationHelper
3
- def current_account
4
- return @clerk_current_account if defined?(@clerk_current_account)
5
- @clerk_current_account ||= begin
6
- if Clerk.config.session_mode==:database_session
7
- raise "TODO"
8
- elsif Clerk.config.session_mode==:cookie_session
9
- Clerk::Account.find_by(id: current_account_id)
10
- end
11
- rescue => e
12
- puts e
13
- nil
14
- end
15
- end
16
-
17
- def current_account_id
18
- return @clerk_current_account_id if defined?(@clerk_current_account_id)
19
- @clerk_current_account_id ||= begin
20
- if Clerk.config.session_mode==:database_session
21
- current_account.id
22
- elsif Clerk.config.session_mode==:cookie_session
23
- session_cookie = JSON.parse(cookies[:clerk_session])
24
- nil if session_cookie.nil?
25
- JSON.parse(session_cookie["d"])["k"]
26
- end
27
- rescue => e
28
- nil
29
- end
30
- end
31
-
32
- def clerk_sign_out_path
33
- "#{Clerk.accounts_url}/sign_out"
34
- end
35
-
36
- def clerk_sign_in_path
37
- "#{Clerk.accounts_url}"
38
- end
39
-
40
- def clerk_sign_up_path
41
- "#{Clerk.accounts_url}/sign_up"
42
- end
43
-
44
- def clerk_add_card_path(account_id:, redirect_path:)
45
- "#{Clerk.accounts_url}/add_card?account_id=#{account_id}&redirect_path=#{redirect_path}"
46
- end
47
- end
48
- end