app_rail-airtable 0.2.9 → 0.2.10

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: 3e09f5f21d40b2186a9b4d985d85305c0f4e54e4066d74ee45a5c43a32a303ad
4
- data.tar.gz: 0ab6b313e24780b1f83e2359638e783ff3d7ca7b6958b14c7c80ba479262e07c
3
+ metadata.gz: 3e57f8d279b01f1a7672be43fce5dda36da9df13bc3152399646f5be729d522c
4
+ data.tar.gz: 7928368ae9d1b4edfd75c651af688be2aa0e7697f31fd5c87afb1a9e96e3945c
5
5
  SHA512:
6
- metadata.gz: 5cdd4ff73e660d69000ac91e1204047f7637a7d58e351e1253639a60423828ccf3c8417083aeaadf1d4d3660584302cbfab9a39f5f737a5b501257fdf6f0359e
7
- data.tar.gz: b9e534d2132bf753fd8cdf8d1126cdf629921177bbaf164e6ca21cabd64b9b207830dcbc9087a09f480c03d0c5ebb8e0101a8ae1d6625da851dc8bc8e589eee3
6
+ metadata.gz: 5eef1c52f304ffe1b0050ff5a7278e47c825dce1e7e08a7af69550894070e23a465dc719fce7dddabfa06de9336f2b63d230685098d09a5c02711b17fe43394d
7
+ data.tar.gz: e3629a453d333d78aac608f45ac04b303b27c636f4350fb0f5c2b42c739756a274faabd55a08278b0cc445a5777f62ae57431acea477dae20575ff1cfac53e59
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- app_rail-airtable (0.2.9)
4
+ app_rail-airtable (0.2.10)
5
5
  activesupport
6
6
  airrecord
7
7
  bcrypt
@@ -0,0 +1,47 @@
1
+ require 'bcrypt'
2
+ require 'securerandom'
3
+
4
+ module AppRail
5
+ module Airtable
6
+ module Authenticatable
7
+ include BCrypt
8
+
9
+ def self.included(klass)
10
+ klass.extend(ClassMethods)
11
+ end
12
+
13
+ module ClassMethods
14
+ def create(email:, password:)
15
+ user = User.new("Email" => email, "Password Hash" => password_hash(password), "Access Token" => access_token)
16
+ user.create
17
+ user
18
+ end
19
+
20
+ def create_session_as_json(email:, password:)
21
+ user = find_by_email_and_password(email, password)
22
+ user&.oauth_session
23
+ end
24
+
25
+ def find_by_email_and_password(email, password)
26
+ all(filter: "AND({Email} = \"#{email}\",{Password Hash} = \"#{password_hash(password)}\")").first
27
+ end
28
+
29
+ def find_by_access_token(access_token)
30
+ all(filter: "{Access Token} = \"#{access_token}\"").first
31
+ end
32
+
33
+ def password_hash(password)
34
+ BCrypt::Password.create(password)
35
+ end
36
+
37
+ def access_token
38
+ SecureRandom.hex
39
+ end
40
+ end
41
+
42
+ def oauth_session
43
+ { access_token: self["Access Token"], scope: :user, refresh_token: "", token_type: :bearer, expires_in: 60000 }
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,27 @@
1
+ module AppRail
2
+ module Airtable
3
+ module AuthenticationHelpers
4
+ def current_user
5
+ @current_user ||= find_current_user
6
+ end
7
+
8
+ def find_current_user
9
+ authorization_header && bearer_token ? User.find_by_access_token(bearer_token) : nil
10
+ end
11
+
12
+ def bearer_token
13
+ authorization_values = authorization_header.split(" ")
14
+ return nil unless authorization_values.count > 1
15
+ authorization_values[1]
16
+ end
17
+
18
+ def authorization_header
19
+ request.env["HTTP_AUTHORIZATION"]
20
+ end
21
+
22
+ def authenticate!
23
+ halt 401 unless current_user
24
+ end
25
+ end
26
+ end
27
+ end
@@ -31,7 +31,7 @@ module AppRail
31
31
  JSON.parse(request.body.read)
32
32
  end
33
33
 
34
- def params_and_json_body
34
+ def params_and_body_as_json
35
35
  request_body_as_json.merge(params)
36
36
  end
37
37
  end
@@ -61,7 +61,7 @@ module AppRail
61
61
  def self.create_route(name, authenticated)
62
62
  post "/#{name.to_s}" do
63
63
  authenticate! if authenticated
64
- as_json = name.classify_constantize.create_as_json(current_user: authenticated ? current_user : nil, params: params_and_json_body)
64
+ as_json = name.classify_constantize.create_as_json(current_user: authenticated ? current_user : nil, params: params_and_body_as_json)
65
65
  [201, as_json.to_json]
66
66
  end
67
67
  end
@@ -1,5 +1,5 @@
1
1
  module AppRail
2
2
  module Airtable
3
- VERSION = "0.2.9"
3
+ VERSION = "0.2.10"
4
4
  end
5
5
  end
@@ -1,7 +1,8 @@
1
1
  require "app_rail/airtable/version"
2
2
  require "app_rail/airtable/string_ext"
3
3
  require "app_rail/airtable/application_record"
4
- require "app_rail/airtable/authentication_record"
4
+ require "app_rail/airtable/authenticatable"
5
+ require "app_rail/airtable/authentication_helpers"
5
6
  require "app_rail/airtable/sinatra"
6
7
  require "app_rail/airtable/generator"
7
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_rail-airtable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Brooke-Smith
@@ -130,7 +130,8 @@ files:
130
130
  - bin/setup
131
131
  - lib/app_rail/airtable.rb
132
132
  - lib/app_rail/airtable/application_record.rb
133
- - lib/app_rail/airtable/authentication_record.rb
133
+ - lib/app_rail/airtable/authenticatable.rb
134
+ - lib/app_rail/airtable/authentication_helpers.rb
134
135
  - lib/app_rail/airtable/generator.rb
135
136
  - lib/app_rail/airtable/sinatra.rb
136
137
  - lib/app_rail/airtable/string_ext.rb
@@ -1,27 +0,0 @@
1
- require 'bcrypt'
2
-
3
- module AppRail
4
- module Airtable
5
- class AuthenticationRecord < ApplicationRecord
6
- include BCrypt
7
- airtable_attr "Email", "Password Hash", "Access Token"
8
-
9
- def self.create_session_as_json(email:, password:)
10
- user = find_by_email_and_password(email, Password.create(password))
11
- user&.oauth_session
12
- end
13
-
14
- def self.find_by_email_and_password(email, password_hash)
15
- all(filter: "AND({Email} = \"#{email}\",{Password Hash} = \"#{password_hash}\")").first
16
- end
17
-
18
- def self.find_by_access_token(access_token)
19
- all(filter: "{Access Token} = \"#{access_token}\"").first
20
- end
21
-
22
- def oauth_session
23
- { access_token: self["Access Token"], scope: :user, refresh_token: "", token_type: :bearer, expires_in: 60000 }
24
- end
25
- end
26
- end
27
- end