gatleon-authform-rails 0.1.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3ca7bb6cbffefb551a1a745285cb28ad6ba451811565199278b462c899ff2b1
4
- data.tar.gz: 790d1b536bf057cf929b5c7b975d9ecfdfa583477f51c1af15045ad5846ed795
3
+ metadata.gz: 685b07de50135a4e5370690768aa9d3c35ee9ec6d69067cb64bf8d7114a17e15
4
+ data.tar.gz: 4575c9167863052ef09dbf8485d9ae61d3d31a3ab6e398f5ff13f877e5b7da92
5
5
  SHA512:
6
- metadata.gz: 6923b17d438ded1ed5fbc8ab8dbe8eecb475b7b760e6a7ed782aa672af8ebbb45cc71b84cfbea3ef49f057dea43a26fc61900e16f4fe2892521f88d96826015d
7
- data.tar.gz: 3b890fb44898fbd97d919a06cd439c1459c31e5e590f23083b6cce3b8c42f2cb94c65c63c33090f9b5738cf398b9398dde831ce56469a9ca3c2a4e78f712136c
6
+ metadata.gz: 0faeb596324770954d58d7561a6269e2564e0c87bc46f5929a97a6af553787a6e2dfa04be77c7502ee9c3d8e13f02f46efcc92d6cc3af9114e846e0aa80a0318
7
+ data.tar.gz: d468569f24c293468138382d51d6eafa44d219307c2586daadd266a9dd22d3657651b2c6256635cb7f897e04b02ff5cfbfc338af43578f81fe091d1dfc8d2458
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ .byebug_history
@@ -0,0 +1 @@
1
+ 2.6.6
data/Gemfile CHANGED
@@ -7,3 +7,4 @@ gem "rake", "~> 12.0"
7
7
  gem "rspec", "~> 3.0"
8
8
  gem "rails"
9
9
  gem "byebug"
10
+ gem "yard"
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gatleon-authform-rails (0.1.0)
4
+ gatleon-authform-rails (0.5.0)
5
+ xxhash
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
@@ -143,6 +144,8 @@ GEM
143
144
  websocket-driver (0.7.1)
144
145
  websocket-extensions (>= 0.1.0)
145
146
  websocket-extensions (0.1.4)
147
+ xxhash (0.4.0)
148
+ yard (0.9.24)
146
149
  zeitwerk (2.3.0)
147
150
 
148
151
  PLATFORMS
@@ -154,6 +157,7 @@ DEPENDENCIES
154
157
  rails
155
158
  rake (~> 12.0)
156
159
  rspec (~> 3.0)
160
+ yard
157
161
 
158
162
  BUNDLED WITH
159
163
  2.1.4
data/README.md CHANGED
@@ -1,6 +1,4 @@
1
- ![authform-rails](https://raw.githubusercontent.com/gatleon/gatleon-rails/master/gatleon-authform-rails.png)
2
-
3
- # authform-rails by gatleon
1
+ # gatleon-authform-rails
4
2
 
5
3
  add authentication to your application - in 1 minute or less.
6
4
 
@@ -18,14 +16,25 @@ and then execute:
18
16
  $ bundle install
19
17
  ```
20
18
 
21
- add a profile controller
19
+ open rails credentials:
20
+
21
+ ```
22
+ $ EDITOR=vim rails credentials:edit
23
+ ```
24
+
25
+ set authform credentials:
26
+
27
+ ```
28
+ authform:
29
+ public_key: "Available at https://authform.gatleon.com"
30
+ secret_key: "Available at https://authform.gatleon.com"
31
+ ```
32
+
33
+ add a profile controller:
22
34
 
23
35
  ```ruby
24
36
  class ProfileController < ActionController::Base
25
- AUTHFORM_FORM_SECRET_KEY = "" # Available at https://authform.gatleon.com. coming soon!
26
- AUTHFORM_FORM_PUBLIC_KEY = "" # Available at https://authform.gatleon.com. coming soon!
27
-
28
- include Gatleon::Authform::Rails::Concern.new(public_key: AUTHFORM_FORM_PUBLIC_KEY, secret_key: AUTHFORM_FORM_SECRET_KEY)
37
+ include Gatleon::Authform::Rails::Concern.new(Rails.application.credentials.dig(:authform))
29
38
 
30
39
  before_action :require_login, only: [:index]
31
40
 
@@ -43,7 +52,8 @@ class ProfileController < ActionController::Base
43
52
  erb = <<~ERB
44
53
  <p style="color: red;"><%= flash[:error] %></p>
45
54
  <h1>Sign In</h1>
46
- <form action="https://authform.gatleon.com/v1/form/<%= ProfileController::AUTHFORM_FORM_PUBLIC_KEY %>" method="POST">
55
+ <form action="https://api.authform.io/v1/form/<%= Rails.application.credentials.dig(:authform, :public_key) %>" method="POST">
56
+ <input type="hidden" name="successPath" value="/profile">
47
57
  <input type="email" name="email">
48
58
  <button type="submit">Sign In</button>
49
59
  </form>
@@ -64,12 +74,12 @@ class ProfileController < ActionController::Base
64
74
  end
65
75
  ```
66
76
 
67
- add profile routes to routes.rb
77
+ add profile routes to routes.rb:
68
78
 
69
79
  ```ruby
70
80
  Rails.application.routes.draw do
71
- get '/profile', to: 'profile#index', as: 'profile'
72
- get '/profile/signin', to: 'profile#signin', as: 'profile_signin'
81
+ get "/profile", to: "profile#index", as: :profile
82
+ get "/profile/signin", to: "profile#signin", as: :profile_signin
73
83
  end
74
84
  ```
75
85
 
@@ -24,4 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.bindir = "exe"
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ["lib"]
27
+
28
+ spec.add_runtime_dependency "xxhash"
27
29
  end
@@ -1,11 +1,14 @@
1
+ require "xxhash"
2
+
1
3
  module Gatleon
2
4
  module Authform
3
5
  module Rails
4
6
  class Concern < Module
5
7
  def initialize(public_key:,
6
8
  secret_key:,
9
+ domain: nil,
7
10
  current_user_method_name: "current_user",
8
- _authform_base_url: "https://authform.gatleon.com")
11
+ _authform_base_url: "https://api.authform.io")
9
12
  super() do
10
13
  extend ActiveSupport::Concern
11
14
 
@@ -19,9 +22,11 @@ module Gatleon
19
22
  # defaults to current_user
20
23
  define_method current_user_method_name do
21
24
  begin
22
- json = JSON.parse(cookies[_authform_user_cookie_key])["data"]
23
-
24
- Gatleon::Authform::Rails::User.new(json: json, _form_secret_key: secret_key, _authform_base_url: _authform_base_url)
25
+ Gatleon::Authform::Rails::User.new(_cookies: cookies,
26
+ _authform_user_cookie_key: _authform_user_cookie_key,
27
+ _form_secret_key: secret_key,
28
+ _domain: domain,
29
+ _authform_base_url: _authform_base_url)
25
30
  rescue
26
31
  nil
27
32
  end
@@ -34,18 +39,7 @@ module Gatleon
34
39
  uri = URI("#{_authform_base_url}/v1/exchangeUserVoucherForUser/#{params[:_authformUserVoucher]}")
35
40
  response = Net::HTTP.get_response(uri)
36
41
 
37
- if response.code.to_i == 200
38
- # First attempt WITHOUT all - for setting on platforms like heroku that deny setting cookies across all subdomains
39
- cookies[_authform_user_cookie_key] = {
40
- value: response.body
41
- }
42
-
43
- # Then set all - desired behavior for hosting your own domain
44
- cookies[_authform_user_cookie_key] = {
45
- value: response.body,
46
- domain: :all
47
- }
48
- end
42
+ cookies[_authform_user_cookie_key] = _cookie_attrs(response.body) if response.code.to_i == 200
49
43
 
50
44
  q = Rack::Utils.parse_query(URI.parse(request.url).query)
51
45
  q.delete("_authformUserVoucher")
@@ -57,7 +51,14 @@ module Gatleon
57
51
  end
58
52
 
59
53
  define_method :_authform_user_cookie_key do
60
- public_key # allows for multiple forms per site
54
+ "#{public_key}_#{XXhash.xxh32(domain)}"
55
+ end
56
+
57
+ define_method :_cookie_attrs do |value|
58
+ {
59
+ value: value,
60
+ domain: domain
61
+ }.compact
61
62
  end
62
63
  end
63
64
  end
@@ -1,30 +1,45 @@
1
+ require "json"
2
+
1
3
  module Gatleon
2
4
  module Authform
3
5
  module Rails
4
6
  class User
5
7
  PERMITTED_CHARS = /\A[a-zA-Z0-9_)]*\z/
6
8
 
7
- def initialize(json:, _form_secret_key:, _authform_base_url:)
8
- @json = json
9
-
9
+ def initialize(_cookies:,
10
+ _authform_user_cookie_key:,
11
+ _form_secret_key:,
12
+ _domain:,
13
+ _authform_base_url:)
14
+ @_cookies = _cookies
15
+ @_authform_user_cookie_key = _authform_user_cookie_key
10
16
  @_form_secret_key = _form_secret_key
17
+ @_domain = _domain
11
18
  @_authform_base_url = _authform_base_url
19
+
20
+ parse!
21
+ end
22
+
23
+ def parse!
24
+ !!_id
25
+ rescue
26
+ raise Gatleon::Authform::Rails::Error
12
27
  end
13
28
 
14
29
  # Getters
15
30
  #
16
31
  def _id
17
- @json["_id"]
32
+ data["_id"]
18
33
  end
19
34
 
20
35
  def _email
21
- @json["_email"]
36
+ data["_email"]
22
37
  end
23
38
 
24
39
  # Getters
25
40
  #
26
41
  def [](key)
27
- @json[key.to_s]
42
+ data[key.to_s]
28
43
  end
29
44
 
30
45
  # Setters
@@ -38,8 +53,31 @@ module Gatleon
38
53
 
39
54
  raise Gatleon::Authform::Rails::Error, "only characters a-z, A-Z, 0-9, and _ permitted in field name" unless key.match?(PERMITTED_CHARS)
40
55
 
41
- @json[key] = value.to_s
56
+ data[key] = value.to_s
57
+ end
58
+
59
+ def data
60
+ _json["data"]
61
+ end
62
+
63
+ def _json
64
+ @_json ||= JSON.parse(@_cookies[@_authform_user_cookie_key])
65
+ end
66
+
67
+ def signoff!
68
+ if @_domain
69
+ @_cookies.delete(@_authform_user_cookie_key, domain: @_domain)
70
+ else
71
+ @_cookies.delete(@_authform_user_cookie_key)
72
+ end
42
73
  end
74
+ alias_method :sign_off!, :signoff!
75
+ alias_method :signout!, :signoff!
76
+ alias_method :sign_out!, :signoff!
77
+ alias_method :logout!, :signoff!
78
+ alias_method :log_out!, :signoff!
79
+ alias_method :logoff!, :signoff!
80
+ alias_method :log_off!, :signoff!
43
81
 
44
82
  private
45
83
 
@@ -1,7 +1,7 @@
1
1
  module Gatleon
2
2
  module Authform
3
3
  module Rails
4
- VERSION = "0.1.0"
4
+ VERSION = "0.5.0"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gatleon-authform-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - gatleon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-30 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2020-08-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: xxhash
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: add authentication to your application - in 1 minute or less
14
28
  email:
15
29
  - ''
@@ -19,6 +33,7 @@ extra_rdoc_files: []
19
33
  files:
20
34
  - ".gitignore"
21
35
  - ".rspec"
36
+ - ".ruby-version"
22
37
  - ".travis.yml"
23
38
  - Gemfile
24
39
  - Gemfile.lock
@@ -28,7 +43,6 @@ files:
28
43
  - bin/console
29
44
  - bin/setup
30
45
  - gatleon-authform-rails.gemspec
31
- - gatleon-authform-rails.png
32
46
  - lib/gatleon/authform/rails.rb
33
47
  - lib/gatleon/authform/rails/concern.rb
34
48
  - lib/gatleon/authform/rails/user.rb
Binary file