clerk_rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +28 -0
- data/Rakefile +32 -0
- data/app/assets/config/clerk_rails_manifest.js +2 -0
- data/app/assets/javascripts/clerk_rails/application.js +15 -0
- data/app/assets/stylesheets/clerk_rails/application.css +15 -0
- data/app/controllers/clerk_rails/application_controller.rb +5 -0
- data/app/helpers/clerk_rails/application_helper.rb +4 -0
- data/app/jobs/clerk_rails/application_job.rb +4 -0
- data/app/mailers/clerk_rails/application_mailer.rb +6 -0
- data/app/models/clerk_rails/application_record.rb +5 -0
- data/app/views/layouts/clerk_rails/application.html.erb +16 -0
- data/config/routes.rb +2 -0
- data/lib/clerk_rails/api.rb +43 -0
- data/lib/clerk_rails/clerked.rb +66 -0
- data/lib/clerk_rails/engine.rb +114 -0
- data/lib/clerk_rails/helpers.rb +37 -0
- data/lib/clerk_rails/tunnel.rb +229 -0
- data/lib/clerk_rails/version.rb +3 -0
- data/lib/clerk_rails.rb +8 -0
- data/lib/tasks/clerk_rails_tasks.rake +4 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0d761905d524cec6da93abc74a280132e2cfe15ce25dacb6b7a0b79e6e55c55b
|
4
|
+
data.tar.gz: 52711c6b2f0567562db2ea404a84078a3f8e2a7892dcfb92dc75020a4cee70e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eee9222a4bece51fbfa286e6839d6e6a37f16d0863f672d6338b78e463b074b415cc94d5d652f2131a1068dcabeecc6fdfa4584408c3f65a9df22f89650146f1
|
7
|
+
data.tar.gz: ab413038b9caf3a2d4c97c421bccacfd42bc9785cf85a355b8e00cca6383a2d9f6c32c806911c492939b9f61bc9bf6fbbc5d67a7606b0372939e5332b52d0c7d
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Clerk.dev
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# ClerkRails
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'clerk_rails'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install clerk_rails
|
22
|
+
```
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
Contribution directions go here.
|
26
|
+
|
27
|
+
## License
|
28
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'ClerkRails'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
load 'rails/tasks/statistics.rake'
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
t.pattern = 'test/**/*_test.rb'
|
29
|
+
t.verbose = false
|
30
|
+
end
|
31
|
+
|
32
|
+
task default: :test
|
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require rails-ujs
|
14
|
+
//= require activestorage
|
15
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Clerk rails</title>
|
5
|
+
<%= csrf_meta_tags %>
|
6
|
+
<%= csp_meta_tag %>
|
7
|
+
|
8
|
+
<%= stylesheet_link_tag "clerk_rails/application", media: "all" %>
|
9
|
+
<%= javascript_include_tag "clerk_rails/application" %>
|
10
|
+
</head>
|
11
|
+
<body>
|
12
|
+
|
13
|
+
<%= yield %>
|
14
|
+
|
15
|
+
</body>
|
16
|
+
</html>
|
data/config/routes.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
module ClerkRails
|
2
|
+
module Api
|
3
|
+
class Connection
|
4
|
+
def initialize(url, auth_token)
|
5
|
+
@c = Faraday.new(:url => url) do |conn|
|
6
|
+
conn.request :url_encoded
|
7
|
+
conn.authorization :Bearer, auth_token
|
8
|
+
conn.adapter Faraday.default_adapter
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def post(path, fields, &block)
|
13
|
+
ClerkRails::Api::Response.new(@c.post(path, fields.as_json, &block))
|
14
|
+
end
|
15
|
+
|
16
|
+
def patch(path, fields, &block)
|
17
|
+
ClerkRails::Api::Response.new(@c.patch(path, fields.as_json, &block))
|
18
|
+
end
|
19
|
+
|
20
|
+
def delete(path, fields, &block)
|
21
|
+
ClerkRails::Api::Response.new(@c.delete(path, fields.as_json, &block))
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(*args, &block)
|
25
|
+
ClerkRails::Api::Response.new(@c.get(*args, &block))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Response
|
30
|
+
def initialize(faraday_response)
|
31
|
+
@res = faraday_response
|
32
|
+
end
|
33
|
+
|
34
|
+
def data
|
35
|
+
JSON.parse(@res.body, symbolize_names: true)
|
36
|
+
end
|
37
|
+
|
38
|
+
def method_missing(m, *args, &block)
|
39
|
+
@res.send(m, *args, &block)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module ClerkRails
|
2
|
+
module Clerked
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
@@clerk_permissions_map = {}
|
7
|
+
@@clerk_roles_map = {}
|
8
|
+
|
9
|
+
has_many :clerk_roles, clerk_roles_scope, class_name: "Clerk::Role", foreign_key: :scope_id
|
10
|
+
|
11
|
+
has_many :accounts, through: :clerk_roles, source: :account do
|
12
|
+
|
13
|
+
def with(role: nil, permission: nil)
|
14
|
+
if (role.nil? and permission.nil?) or (not role.nil? and not permission.nil?)
|
15
|
+
raise ArgumentError.new("Invalid argument, must supply either a role or permission")
|
16
|
+
end
|
17
|
+
|
18
|
+
if not role.nil?
|
19
|
+
return where(Clerk::Role.table_name=>{name: role})
|
20
|
+
elsif not permission.nil?
|
21
|
+
all_roles = self.proxy_association.owner.class.roles_with_permission(permission)
|
22
|
+
return where(Clerk::Role.table_name=>{name: all_roles})
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_role?(role, account)
|
30
|
+
account.has_role?(role, self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def roles_for(account)
|
34
|
+
account.roles_for(self)
|
35
|
+
end
|
36
|
+
|
37
|
+
class_methods do
|
38
|
+
def clerk_permissions_map
|
39
|
+
@@clerk_permissions_map
|
40
|
+
end
|
41
|
+
|
42
|
+
def clerk_roles_map
|
43
|
+
@@clerk_roles_map
|
44
|
+
end
|
45
|
+
|
46
|
+
def roles_with_permission(permission)
|
47
|
+
@@clerk_roles_map[permission] ||= begin
|
48
|
+
roles_with_permission = []
|
49
|
+
|
50
|
+
clerk_permissions_map.keys.each do |key|
|
51
|
+
if clerk_permissions_map[key].include?(permission)
|
52
|
+
roles_with_permission << key
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
roles_with_permission
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def clerk_roles_scope
|
61
|
+
class_name = self.name
|
62
|
+
->{ where(scope_class: class_name) }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module ClerkRails
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace ClerkRails
|
4
|
+
engine_name "clerk"
|
5
|
+
|
6
|
+
API = ClerkRails::Api::Connection.new(
|
7
|
+
(ENV["CLERK_API_PATH"] || "https://api.clerk.dev"),
|
8
|
+
ENV["CLERK_KEY"]
|
9
|
+
)
|
10
|
+
|
11
|
+
puts "A0!!! "
|
12
|
+
|
13
|
+
res = API.get('/v1/environment').data
|
14
|
+
|
15
|
+
puts res
|
16
|
+
|
17
|
+
ClerkRails::ENV = res
|
18
|
+
|
19
|
+
# Set environment variables for Clerk::ActiveRecord
|
20
|
+
# assume ENV["CLERK_KEY"] is already set because ClerkRails relies on it
|
21
|
+
::ENV["CLERK_DATABASE_URL"] = ClerkRails::ENV[:database_url]
|
22
|
+
::ENV["CLERK_SESSION_KEY"] = ClerkRails::ENV[:session_key]
|
23
|
+
|
24
|
+
puts ClerkRails::ENV
|
25
|
+
|
26
|
+
if ClerkRails::ENV[:type] == "development"
|
27
|
+
config.before_configuration do
|
28
|
+
if defined?(Rails::Server)
|
29
|
+
require 'clerk_rails/tunnel'
|
30
|
+
ClerkRails::Tunnel.start!(**DATA[:development_tunnel])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
initializer 'clerk.configuration' do |app|
|
36
|
+
# Add authentication helpers
|
37
|
+
::ActionController::Base.send :helper, ClerkRails::Helpers::View
|
38
|
+
::ActionController::Base.send :include, ClerkRails::Helpers::Controller
|
39
|
+
::ActionController::Base.send :helper, ClerkRails::Helpers::ViewAndController
|
40
|
+
::ActionController::Base.send :include, ClerkRails::Helpers::ViewAndController
|
41
|
+
|
42
|
+
# Add route helpers
|
43
|
+
accounts_routes = {
|
44
|
+
sign_out: "/sign_out",
|
45
|
+
sign_in: "/",
|
46
|
+
sign_up: "/sign_up",
|
47
|
+
verify_email_address: "/verify_email_address",
|
48
|
+
}
|
49
|
+
|
50
|
+
app.routes.append do
|
51
|
+
accounts_routes.each do |k, v|
|
52
|
+
direct k do
|
53
|
+
"https://#{ClerkRails::ENV[:services][:ACCOUNTS][:host]}#{v}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Add roole management
|
59
|
+
# Note: This isn't publicly launched
|
60
|
+
::ActiveRecord::Base.class_eval do
|
61
|
+
def self.clerk(association_name, permissions: [])
|
62
|
+
plural_role = association_name.to_s.pluralize.to_sym
|
63
|
+
singular_role = association_name.to_s.singularize.to_sym
|
64
|
+
|
65
|
+
roles_association = :"clerk_roles_#{association_name}"
|
66
|
+
|
67
|
+
remote_class = self.name
|
68
|
+
|
69
|
+
unless self.respond_to? :clerk_roles
|
70
|
+
self.class_eval do
|
71
|
+
include ClerkRails::Clerked
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
has_many roles_association,
|
76
|
+
->{ where(scope_class: remote_class, name: singular_role) },
|
77
|
+
class_name: "Clerk::Role",
|
78
|
+
foreign_key: :scope_id
|
79
|
+
|
80
|
+
plural_role = association_name.to_s.pluralize.to_sym
|
81
|
+
singular_role = association_name.to_s.singularize.to_sym
|
82
|
+
|
83
|
+
account_accessor = self.name.underscore.pluralize.to_sym
|
84
|
+
|
85
|
+
# Add clerk permissions to `self` the associated class
|
86
|
+
self.clerk_permissions_map[singular_role] = permissions
|
87
|
+
|
88
|
+
# Add magic methods to Clerk::Acount
|
89
|
+
Clerk::Account.class_eval do
|
90
|
+
define_method account_accessor do
|
91
|
+
self.class::RolesWrapper.new(self, account_accessor)
|
92
|
+
end
|
93
|
+
|
94
|
+
define_method :"is_#{singular_role}?" do |*args|
|
95
|
+
has_role?(singular_role, *args)
|
96
|
+
end
|
97
|
+
|
98
|
+
permissions.each do |permission|
|
99
|
+
define_method :"can_#{permission}?" do |*args|
|
100
|
+
has_permission?(permission, *args)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
[
|
106
|
+
association_name.to_sym,
|
107
|
+
through: roles_association,
|
108
|
+
source: :account
|
109
|
+
]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,37 @@
|
|
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 ClerkRails
|
5
|
+
module Helpers
|
6
|
+
module View
|
7
|
+
end
|
8
|
+
|
9
|
+
module Controller
|
10
|
+
def authenticate_account!
|
11
|
+
if account_signed_in?
|
12
|
+
if !current_account.verified_email_address
|
13
|
+
redirect_to verify_email_address_url and return
|
14
|
+
end
|
15
|
+
else
|
16
|
+
redirect_to sign_in_url and return
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module ViewAndController
|
22
|
+
def account_signed_in?
|
23
|
+
!current_account.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
def current_account
|
27
|
+
@clerk_current_account ||= begin
|
28
|
+
if cookies[:__session]
|
29
|
+
Clerk::Account.from_session_token(cookies[:__session])
|
30
|
+
else
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,229 @@
|
|
1
|
+
module ClerkRails
|
2
|
+
module Tunnel
|
3
|
+
def self.data_dir
|
4
|
+
Rails.root.join(".clerk")
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.crt_path
|
8
|
+
data_dir.join("dev.crt")
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.key_path
|
12
|
+
data_dir.join("dev.key")
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.ngrok_path
|
16
|
+
data_dir.join("clerk_#{executable_type}")
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.ngrok_zip_path
|
20
|
+
data_dir.join("clerk_#{executable_type}.zip")
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.crt_ready?
|
24
|
+
File.exist? key_path and File.exist? crt_path
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.ngrok_ready?
|
28
|
+
File.exist? ngrok_path
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.setup_ngrok!
|
32
|
+
ngrok_paths = {
|
33
|
+
darwin_amd64: "/c/4VmDzA7iaHb/ngrok-stable-darwin-amd64.zip",
|
34
|
+
darwin_386: "/c/4VmDzA7iaHb/ngrok-stable-darwin-386.zip",
|
35
|
+
windows_amd64: "/c/4VmDzA7iaHb/ngrok-stable-windows-amd64.zip",
|
36
|
+
windows_386: "/c/4VmDzA7iaHb/ngrok-stable-windows-386.zip",
|
37
|
+
freebsd_amd64: "/c/4VmDzA7iaHb/ngrok-stable-freebsd-amd64.zip",
|
38
|
+
freebsd_386: "/c/4VmDzA7iaHb/ngrok-stable-freebsd-386.zip",
|
39
|
+
linux_amd64: "/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip",
|
40
|
+
linux_386: "/c/4VmDzA7iaHb/ngrok-stable-linux-386.zip",
|
41
|
+
linux_arm: "/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip",
|
42
|
+
linux_arm64: "/a/nmkK3DkqZEB/ngrok-2.2.8-linux-arm64.zip",
|
43
|
+
}
|
44
|
+
|
45
|
+
puts "=> [Clerk] Downloading tunnel executable."
|
46
|
+
require 'zip'
|
47
|
+
http = Net::HTTP.new("bin.equinox.io", 443)
|
48
|
+
http.use_ssl = true
|
49
|
+
resp = http.get(ngrok_paths[executable_type])
|
50
|
+
open(ngrok_zip_path, "wb") do |file|
|
51
|
+
file.write(resp.body)
|
52
|
+
end
|
53
|
+
|
54
|
+
puts "=> [Clerk] Unzipping tunnel executable."
|
55
|
+
Zip::File.open(ngrok_zip_path) do |zipfile|
|
56
|
+
zipfile.each do |file|
|
57
|
+
if file.name == "ngrok"
|
58
|
+
zipfile.extract(file, ngrok_path)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
File.delete(ngrok_zip_path)
|
64
|
+
|
65
|
+
puts "=> [Clerk] Setup done."
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.save_tunnel_cert_locally!(certificate, certificate_key)
|
69
|
+
Dir.mkdir(data_dir) unless Dir.exist? data_dir
|
70
|
+
File.write(crt_path, certificate)
|
71
|
+
File.write(key_path, certificate_key)
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.start!(certificate:, certificate_key:, authorization:)
|
75
|
+
save_tunnel_cert_locally!(certificate, certificate_key)
|
76
|
+
|
77
|
+
setup_ngrok! unless ngrok_ready?
|
78
|
+
|
79
|
+
# Ngrok currently does not send an X-Forwarded-Proto header with requests,
|
80
|
+
# which causes Rack to interpret them as HTTP instead of HTTPS. This patches
|
81
|
+
# Rack so it treats everthing as HTTPS
|
82
|
+
self.patch_rack_requests
|
83
|
+
|
84
|
+
# Ngrok only worked properly if the host was specified as 127.0.0.1, but
|
85
|
+
# the default was 0.0.0.0. This changes the host to 127.0.0.1
|
86
|
+
server = ObjectSpace.each_object(Rails::Server).first
|
87
|
+
server_options = server.instance_variable_get(:@options).dup
|
88
|
+
if !server.send(:use_puma?)
|
89
|
+
raise "Sorry, Clerk currently only supports Rails using the Puma server."
|
90
|
+
elsif server_options[:user_supplied_options].include? :Host
|
91
|
+
raise "Sorry, Clerk cannot boot with a custom host: #{server_options[:Host]}"
|
92
|
+
else
|
93
|
+
server_options[:user_supplied_options] << :Host
|
94
|
+
server_options[:Host] = "127.0.0.1"
|
95
|
+
server.instance_variable_set(:@options, server_options)
|
96
|
+
end
|
97
|
+
|
98
|
+
require 'ngrok/tunnel'
|
99
|
+
self.patch_ngrok_gem
|
100
|
+
puts "=> Booting https://#{ClerkRails::ENV[:host]} with Clerk"
|
101
|
+
options = {
|
102
|
+
addr: server_options[:Port],
|
103
|
+
authtoken: authorization,
|
104
|
+
hostname: "#{ClerkRails::ENV[:host]}",
|
105
|
+
region: "us",
|
106
|
+
crt: Rails.root.join(".clerk/dev.crt"),
|
107
|
+
key: Rails.root.join(".clerk/dev.key")
|
108
|
+
}
|
109
|
+
Ngrok::Tunnel.start(options)
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def self.executable_type
|
115
|
+
@@executable_type ||= begin
|
116
|
+
platform = begin
|
117
|
+
case RbConfig::CONFIG['host_os'].downcase
|
118
|
+
when /linux/
|
119
|
+
"linux"
|
120
|
+
when /darwin/
|
121
|
+
"darwin"
|
122
|
+
when /bsd/
|
123
|
+
"freebsd"
|
124
|
+
when /mingw|mswin/
|
125
|
+
"windows"
|
126
|
+
else
|
127
|
+
"linux"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
cpu = begin
|
132
|
+
case RbConfig::CONFIG['host_cpu'].downcase
|
133
|
+
when /amd64|x86_64/
|
134
|
+
"amd64"
|
135
|
+
when /^arm/
|
136
|
+
RbConfig::CONFIG['host_cpu'].include?("64") ? "arm64" : "arm"
|
137
|
+
else
|
138
|
+
"386"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
executable_type = :"#{platform}_#{cpu}"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
# This configured puma to terminate TLS, but since Puma's TLS terminator has a bug we moved termination to ngrok
|
147
|
+
# https://github.com/puma/puma/issues/1670
|
148
|
+
# def self.configure_puma_options
|
149
|
+
# server = ObjectSpace.each_object(Rails::Server).first
|
150
|
+
# server_options = server.instance_variable_get(:@options).dup
|
151
|
+
# if !server.send(:use_puma?)
|
152
|
+
# raise "Sorry, Clerk cannot boot with a custom host: #{server_options[:Host]}"
|
153
|
+
# elsif server_options[:user_supplied_options].include? :Host
|
154
|
+
# raise "Sorry, Clerk currently only supports Rails using the Puma server."
|
155
|
+
# else
|
156
|
+
# server_options[:user_supplied_options] << :Host
|
157
|
+
# server_options[:Host] = "ssl://127.0.0.1:#{server_options[:Port]}?key=.clerk/dev.key&cert=.clerk/dev.crt"
|
158
|
+
# server.instance_variable_set(:@options, server_options)
|
159
|
+
# end
|
160
|
+
# end
|
161
|
+
|
162
|
+
def self.patch_rack_requests
|
163
|
+
::ActionDispatch::Request.class_eval do
|
164
|
+
def scheme
|
165
|
+
"https"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def self.patch_ngrok_gem
|
171
|
+
# The ngrok-tunnel gem supports launching ngrok's HTTP tunnels, but Clerk uses TLS.
|
172
|
+
# This
|
173
|
+
::Ngrok::Tunnel.class_eval do
|
174
|
+
def self.start(params = {})
|
175
|
+
init(params)
|
176
|
+
|
177
|
+
if stopped?
|
178
|
+
@params[:log] = (@params[:log]) ? File.open(@params[:log], 'w+') : Tempfile.new('ngrok')
|
179
|
+
@pid = spawn("exec #{ClerkRails::Tunnel.ngrok_path} tls " + ngrok_exec_params)
|
180
|
+
at_exit { Ngrok::Tunnel.stop }
|
181
|
+
fetch_urls
|
182
|
+
end
|
183
|
+
|
184
|
+
@status = :running
|
185
|
+
@ngrok_url.gsub("tls", "https")
|
186
|
+
end
|
187
|
+
|
188
|
+
private
|
189
|
+
|
190
|
+
def self.ngrok_exec_params
|
191
|
+
exec_params = "-log=stdout -log-level=debug "
|
192
|
+
exec_params << "-region=#{@params[:region]} " if @params[:region]
|
193
|
+
exec_params << "-host-header=#{@params[:host_header]} " if @params[:host_header]
|
194
|
+
exec_params << "-authtoken=#{@params[:authtoken]} " if @params[:authtoken]
|
195
|
+
exec_params << "-subdomain=#{@params[:subdomain]} " if @params[:subdomain]
|
196
|
+
exec_params << "-hostname=#{@params[:hostname]} " if @params[:hostname]
|
197
|
+
exec_params << "-crt=#{@params[:crt]} " if @params[:crt]
|
198
|
+
exec_params << "-key=#{@params[:key]} " if @params[:key]
|
199
|
+
exec_params << "-inspect=#{@params[:inspect]} " if @params.has_key? :inspect
|
200
|
+
exec_params << "-config=#{@params[:config]} #{@params[:addr]} > #{@params[:log].path}"
|
201
|
+
end
|
202
|
+
|
203
|
+
def self.fetch_urls
|
204
|
+
@params[:timeout].times do
|
205
|
+
log_content = @params[:log].read
|
206
|
+
|
207
|
+
result = log_content.scan(/URL:(.+)\sProto:(tls)\s/)
|
208
|
+
if !result.empty?
|
209
|
+
result = Hash[*result.flatten].invert
|
210
|
+
@ngrok_url = result['tls']
|
211
|
+
return @ngrok_url if @ngrok_url
|
212
|
+
end
|
213
|
+
|
214
|
+
error = log_content.scan(/msg="command failed" err="([^"]+)"/).flatten
|
215
|
+
unless error.empty?
|
216
|
+
self.stop
|
217
|
+
raise Ngrok::Error, error.first
|
218
|
+
end
|
219
|
+
|
220
|
+
sleep 1
|
221
|
+
@params[:log].rewind
|
222
|
+
end
|
223
|
+
self.stop
|
224
|
+
raise Ngrok::FetchUrlError, "Unable to fetch external url"
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
data/lib/clerk_rails.rb
ADDED
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clerk_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Colin Sidoti
|
8
|
+
- Braden Sidoti
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2019-06-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 5.2.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 5.2.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bcrypt
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: ngrok-tunnel
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: faraday
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: faraday_middleware
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :runtime
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rubyzip
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :runtime
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
description:
|
99
|
+
email:
|
100
|
+
- hello@clerk.dev
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- app/assets/config/clerk_rails_manifest.js
|
109
|
+
- app/assets/javascripts/clerk_rails/application.js
|
110
|
+
- app/assets/stylesheets/clerk_rails/application.css
|
111
|
+
- app/controllers/clerk_rails/application_controller.rb
|
112
|
+
- app/helpers/clerk_rails/application_helper.rb
|
113
|
+
- app/jobs/clerk_rails/application_job.rb
|
114
|
+
- app/mailers/clerk_rails/application_mailer.rb
|
115
|
+
- app/models/clerk_rails/application_record.rb
|
116
|
+
- app/views/layouts/clerk_rails/application.html.erb
|
117
|
+
- config/routes.rb
|
118
|
+
- lib/clerk_rails.rb
|
119
|
+
- lib/clerk_rails/api.rb
|
120
|
+
- lib/clerk_rails/clerked.rb
|
121
|
+
- lib/clerk_rails/engine.rb
|
122
|
+
- lib/clerk_rails/helpers.rb
|
123
|
+
- lib/clerk_rails/tunnel.rb
|
124
|
+
- lib/clerk_rails/version.rb
|
125
|
+
- lib/tasks/clerk_rails_tasks.rake
|
126
|
+
homepage:
|
127
|
+
licenses:
|
128
|
+
- MIT
|
129
|
+
metadata: {}
|
130
|
+
post_install_message:
|
131
|
+
rdoc_options: []
|
132
|
+
require_paths:
|
133
|
+
- lib
|
134
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements: []
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 2.7.8
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: Initializes the Clerk environment for Rails
|
150
|
+
test_files: []
|