gatleon-authform-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b3ca7bb6cbffefb551a1a745285cb28ad6ba451811565199278b462c899ff2b1
4
+ data.tar.gz: 790d1b536bf057cf929b5c7b975d9ecfdfa583477f51c1af15045ad5846ed795
5
+ SHA512:
6
+ metadata.gz: 6923b17d438ded1ed5fbc8ab8dbe8eecb475b7b760e6a7ed782aa672af8ebbb45cc71b84cfbea3ef49f057dea43a26fc61900e16f4fe2892521f88d96826015d
7
+ data.tar.gz: 3b890fb44898fbd97d919a06cd439c1459c31e5e590f23083b6cce3b8c42f2cb94c65c63c33090f9b5738cf398b9398dde831ce56469a9ca3c2a4e78f712136c
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.5
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gatleon-authform-rails.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "rails"
9
+ gem "byebug"
@@ -0,0 +1,159 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gatleon-authform-rails (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actioncable (6.0.2.2)
10
+ actionpack (= 6.0.2.2)
11
+ nio4r (~> 2.0)
12
+ websocket-driver (>= 0.6.1)
13
+ actionmailbox (6.0.2.2)
14
+ actionpack (= 6.0.2.2)
15
+ activejob (= 6.0.2.2)
16
+ activerecord (= 6.0.2.2)
17
+ activestorage (= 6.0.2.2)
18
+ activesupport (= 6.0.2.2)
19
+ mail (>= 2.7.1)
20
+ actionmailer (6.0.2.2)
21
+ actionpack (= 6.0.2.2)
22
+ actionview (= 6.0.2.2)
23
+ activejob (= 6.0.2.2)
24
+ mail (~> 2.5, >= 2.5.4)
25
+ rails-dom-testing (~> 2.0)
26
+ actionpack (6.0.2.2)
27
+ actionview (= 6.0.2.2)
28
+ activesupport (= 6.0.2.2)
29
+ rack (~> 2.0, >= 2.0.8)
30
+ rack-test (>= 0.6.3)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
33
+ actiontext (6.0.2.2)
34
+ actionpack (= 6.0.2.2)
35
+ activerecord (= 6.0.2.2)
36
+ activestorage (= 6.0.2.2)
37
+ activesupport (= 6.0.2.2)
38
+ nokogiri (>= 1.8.5)
39
+ actionview (6.0.2.2)
40
+ activesupport (= 6.0.2.2)
41
+ builder (~> 3.1)
42
+ erubi (~> 1.4)
43
+ rails-dom-testing (~> 2.0)
44
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
45
+ activejob (6.0.2.2)
46
+ activesupport (= 6.0.2.2)
47
+ globalid (>= 0.3.6)
48
+ activemodel (6.0.2.2)
49
+ activesupport (= 6.0.2.2)
50
+ activerecord (6.0.2.2)
51
+ activemodel (= 6.0.2.2)
52
+ activesupport (= 6.0.2.2)
53
+ activestorage (6.0.2.2)
54
+ actionpack (= 6.0.2.2)
55
+ activejob (= 6.0.2.2)
56
+ activerecord (= 6.0.2.2)
57
+ marcel (~> 0.3.1)
58
+ activesupport (6.0.2.2)
59
+ concurrent-ruby (~> 1.0, >= 1.0.2)
60
+ i18n (>= 0.7, < 2)
61
+ minitest (~> 5.1)
62
+ tzinfo (~> 1.1)
63
+ zeitwerk (~> 2.2)
64
+ builder (3.2.4)
65
+ byebug (11.1.3)
66
+ concurrent-ruby (1.1.6)
67
+ crass (1.0.6)
68
+ diff-lcs (1.3)
69
+ erubi (1.9.0)
70
+ globalid (0.4.2)
71
+ activesupport (>= 4.2.0)
72
+ i18n (1.8.2)
73
+ concurrent-ruby (~> 1.0)
74
+ loofah (2.5.0)
75
+ crass (~> 1.0.2)
76
+ nokogiri (>= 1.5.9)
77
+ mail (2.7.1)
78
+ mini_mime (>= 0.1.1)
79
+ marcel (0.3.3)
80
+ mimemagic (~> 0.3.2)
81
+ method_source (1.0.0)
82
+ mimemagic (0.3.4)
83
+ mini_mime (1.0.2)
84
+ mini_portile2 (2.4.0)
85
+ minitest (5.14.0)
86
+ nio4r (2.5.2)
87
+ nokogiri (1.10.9)
88
+ mini_portile2 (~> 2.4.0)
89
+ rack (2.2.2)
90
+ rack-test (1.1.0)
91
+ rack (>= 1.0, < 3)
92
+ rails (6.0.2.2)
93
+ actioncable (= 6.0.2.2)
94
+ actionmailbox (= 6.0.2.2)
95
+ actionmailer (= 6.0.2.2)
96
+ actionpack (= 6.0.2.2)
97
+ actiontext (= 6.0.2.2)
98
+ actionview (= 6.0.2.2)
99
+ activejob (= 6.0.2.2)
100
+ activemodel (= 6.0.2.2)
101
+ activerecord (= 6.0.2.2)
102
+ activestorage (= 6.0.2.2)
103
+ activesupport (= 6.0.2.2)
104
+ bundler (>= 1.3.0)
105
+ railties (= 6.0.2.2)
106
+ sprockets-rails (>= 2.0.0)
107
+ rails-dom-testing (2.0.3)
108
+ activesupport (>= 4.2.0)
109
+ nokogiri (>= 1.6)
110
+ rails-html-sanitizer (1.3.0)
111
+ loofah (~> 2.3)
112
+ railties (6.0.2.2)
113
+ actionpack (= 6.0.2.2)
114
+ activesupport (= 6.0.2.2)
115
+ method_source
116
+ rake (>= 0.8.7)
117
+ thor (>= 0.20.3, < 2.0)
118
+ rake (12.3.3)
119
+ rspec (3.9.0)
120
+ rspec-core (~> 3.9.0)
121
+ rspec-expectations (~> 3.9.0)
122
+ rspec-mocks (~> 3.9.0)
123
+ rspec-core (3.9.1)
124
+ rspec-support (~> 3.9.1)
125
+ rspec-expectations (3.9.1)
126
+ diff-lcs (>= 1.2.0, < 2.0)
127
+ rspec-support (~> 3.9.0)
128
+ rspec-mocks (3.9.1)
129
+ diff-lcs (>= 1.2.0, < 2.0)
130
+ rspec-support (~> 3.9.0)
131
+ rspec-support (3.9.2)
132
+ sprockets (4.0.0)
133
+ concurrent-ruby (~> 1.0)
134
+ rack (> 1, < 3)
135
+ sprockets-rails (3.2.1)
136
+ actionpack (>= 4.0)
137
+ activesupport (>= 4.0)
138
+ sprockets (>= 3.0.0)
139
+ thor (1.0.1)
140
+ thread_safe (0.3.6)
141
+ tzinfo (1.2.7)
142
+ thread_safe (~> 0.1)
143
+ websocket-driver (0.7.1)
144
+ websocket-extensions (>= 0.1.0)
145
+ websocket-extensions (0.1.4)
146
+ zeitwerk (2.3.0)
147
+
148
+ PLATFORMS
149
+ ruby
150
+
151
+ DEPENDENCIES
152
+ byebug
153
+ gatleon-authform-rails!
154
+ rails
155
+ rake (~> 12.0)
156
+ rspec (~> 3.0)
157
+
158
+ BUNDLED WITH
159
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 gatleon
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.
22
+
@@ -0,0 +1,81 @@
1
+ ![authform-rails](https://raw.githubusercontent.com/gatleon/gatleon-rails/master/gatleon-authform-rails.png)
2
+
3
+ # authform-rails by gatleon
4
+
5
+ add authentication to your application - in 1 minute or less.
6
+
7
+ ## installation
8
+
9
+ add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "gatleon-authform-rails"
13
+ ```
14
+
15
+ and then execute:
16
+
17
+ ```
18
+ $ bundle install
19
+ ```
20
+
21
+ add a profile controller
22
+
23
+ ```ruby
24
+ 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)
29
+
30
+ before_action :require_login, only: [:index]
31
+
32
+ def index
33
+ erb = <<~ERB
34
+ <h1>Profile</h1>
35
+ <p style="color: green;">You are signed in.</p>
36
+ <p><%= current_user %></p>
37
+ ERB
38
+
39
+ render inline: erb
40
+ end
41
+
42
+ def signin
43
+ erb = <<~ERB
44
+ <p style="color: red;"><%= flash[:error] %></p>
45
+ <h1>Sign In</h1>
46
+ <form action="https://authform.gatleon.com/v1/form/<%= ProfileController::AUTHFORM_FORM_PUBLIC_KEY %>" method="POST">
47
+ <input type="email" name="email">
48
+ <button type="submit">Sign In</button>
49
+ </form>
50
+ ERB
51
+
52
+ render inline: erb
53
+ end
54
+
55
+ private
56
+
57
+ def require_login
58
+ unless current_user
59
+ flash[:error] = "Sign in, please."
60
+
61
+ redirect_to(profile_signin_path) and return
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ add profile routes to routes.rb
68
+
69
+ ```ruby
70
+ Rails.application.routes.draw do
71
+ get '/profile', to: 'profile#index', as: 'profile'
72
+ get '/profile/signin', to: 'profile#signin', as: 'profile_signin'
73
+ end
74
+ ```
75
+
76
+ that's it!
77
+
78
+ ## license
79
+
80
+ the gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
81
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gatleon/authform/rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/gatleon/authform/rails/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "gatleon-authform-rails"
5
+ spec.version = Gatleon::Authform::Rails::VERSION
6
+ spec.authors = ["gatleon"]
7
+ spec.email = [""]
8
+
9
+ spec.summary = %q{add authentication to your application - in 1 minute or less}
10
+ spec.description = %q{add authentication to your application - in 1 minute or less}
11
+ spec.homepage = "https://gatleon.com"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/gatleon/gatleon-authform-rails"
17
+ spec.metadata["changelog_uri"] = "https://github.com/gatleon/gatleon-authform-rails"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,13 @@
1
+ require "net/http"
2
+ require "gatleon/authform/rails/version"
3
+ require "gatleon/authform/rails/user"
4
+ require "gatleon/authform/rails/concern"
5
+
6
+ module Gatleon
7
+ module Authform
8
+ module Rails
9
+ class Error < StandardError; end
10
+ # Your code goes here...
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,68 @@
1
+ module Gatleon
2
+ module Authform
3
+ module Rails
4
+ class Concern < Module
5
+ def initialize(public_key:,
6
+ secret_key:,
7
+ current_user_method_name: "current_user",
8
+ _authform_base_url: "https://authform.gatleon.com")
9
+ super() do
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ helper_method "#{current_user_method_name}".to_sym
14
+ before_action :_exchange_user_voucher_for_user
15
+ end
16
+
17
+ private
18
+
19
+ # defaults to current_user
20
+ define_method current_user_method_name do
21
+ 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
+ rescue
26
+ nil
27
+ end
28
+ end
29
+
30
+ define_method :_exchange_user_voucher_for_user do
31
+ if params[:_authformForm] == public_key && params[:_authformUserVoucher]
32
+ # TODO: headers for api verification
33
+
34
+ uri = URI("#{_authform_base_url}/v1/exchangeUserVoucherForUser/#{params[:_authformUserVoucher]}")
35
+ response = Net::HTTP.get_response(uri)
36
+
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
49
+
50
+ q = Rack::Utils.parse_query(URI.parse(request.url).query)
51
+ q.delete("_authformUserVoucher")
52
+ q.delete("_authformForm")
53
+ url = q.empty? ? request.path : "#{request.path}?#{q.to_query}"
54
+
55
+ redirect_to url, status: 302 # redirect to finish removal of query param
56
+ end
57
+ end
58
+
59
+ define_method :_authform_user_cookie_key do
60
+ public_key # allows for multiple forms per site
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
@@ -0,0 +1,63 @@
1
+ module Gatleon
2
+ module Authform
3
+ module Rails
4
+ class User
5
+ PERMITTED_CHARS = /\A[a-zA-Z0-9_)]*\z/
6
+
7
+ def initialize(json:, _form_secret_key:, _authform_base_url:)
8
+ @json = json
9
+
10
+ @_form_secret_key = _form_secret_key
11
+ @_authform_base_url = _authform_base_url
12
+ end
13
+
14
+ # Getters
15
+ #
16
+ def _id
17
+ @json["_id"]
18
+ end
19
+
20
+ def _email
21
+ @json["_email"]
22
+ end
23
+
24
+ # Getters
25
+ #
26
+ def [](key)
27
+ @json[key.to_s]
28
+ end
29
+
30
+ # Setters
31
+ #
32
+ def []=(key, value)
33
+ key = _clean_key(key)
34
+
35
+ raise Gatleon::Authform::Rails::Error, "can't set reserved field name #{key}" if key[0] == "_" # anything starting with _
36
+
37
+ raise Gatleon::Authform::Rails::Error, "can't set empty field name" if key == ""
38
+
39
+ raise Gatleon::Authform::Rails::Error, "only characters a-z, A-Z, 0-9, and _ permitted in field name" unless key.match?(PERMITTED_CHARS)
40
+
41
+ @json[key] = value.to_s
42
+ end
43
+
44
+ private
45
+
46
+ def _persist(key, value)
47
+ uri = _persist_url(key, vlue)
48
+
49
+ Net::HTTP.get_response(uri) # TODO: move to post request
50
+ end
51
+
52
+ def _persist_url(key, value)
53
+ URI("#{@_authform_base_url}/v1/setUser?_id=#{_id}&_secretKey=#{@_form_secret_key}&#{key}=#{value}")
54
+ end
55
+
56
+ def _clean_key(k_or_v)
57
+ k_or_v.to_s.strip
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
@@ -0,0 +1,7 @@
1
+ module Gatleon
2
+ module Authform
3
+ module Rails
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gatleon-authform-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - gatleon
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: add authentication to your application - in 1 minute or less
14
+ email:
15
+ - ''
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - gatleon-authform-rails.gemspec
31
+ - gatleon-authform-rails.png
32
+ - lib/gatleon/authform/rails.rb
33
+ - lib/gatleon/authform/rails/concern.rb
34
+ - lib/gatleon/authform/rails/user.rb
35
+ - lib/gatleon/authform/rails/version.rb
36
+ homepage: https://gatleon.com
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://gatleon.com
41
+ source_code_uri: https://github.com/gatleon/gatleon-authform-rails
42
+ changelog_uri: https://github.com/gatleon/gatleon-authform-rails
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.3.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.0.3
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: add authentication to your application - in 1 minute or less
62
+ test_files: []