app_rail-airtable 0.2.9 → 0.2.13

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: b50f647ec9cbe6a4872a61db88b1bdb15eb9c960b90c219fc5023ea38078e940
4
+ data.tar.gz: ab041ecd7134b4f611460f0e7cacf94fdb1e873066d148d7e69217dc904a018c
5
5
  SHA512:
6
- metadata.gz: 5cdd4ff73e660d69000ac91e1204047f7637a7d58e351e1253639a60423828ccf3c8417083aeaadf1d4d3660584302cbfab9a39f5f737a5b501257fdf6f0359e
7
- data.tar.gz: b9e534d2132bf753fd8cdf8d1126cdf629921177bbaf164e6ca21cabd64b9b207830dcbc9087a09f480c03d0c5ebb8e0101a8ae1d6625da851dc8bc8e589eee3
6
+ metadata.gz: dbbe3242c32cf4a94a045e58085024224c1b18423936f89228dc00f6672f6c76db979ac6bc242ec89792d3f330626d0a0b900154efd941cb9314545b4109e033
7
+ data.tar.gz: 2c3827c50ebdda11aebd971160d45d2fedae642628f59f757f43c48ee020607eca527da0cc159ecb6b7387b371e5c74d27660e960746dff5bf4ad06c1f6442d9
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.13)
5
5
  activesupport
6
6
  airrecord
7
7
  bcrypt
@@ -0,0 +1,56 @@
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
+ user = all(filter: "{Email} = \"#{email}\"").first
27
+ user.valid_password?(password) ? user : nil
28
+ end
29
+
30
+ def find_by_access_token(access_token)
31
+ all(filter: "{Access Token} = \"#{access_token}\"").first
32
+ end
33
+
34
+ def password_hash(password)
35
+ BCrypt::Password.create(password)
36
+ end
37
+
38
+ def access_token
39
+ SecureRandom.hex
40
+ end
41
+ end
42
+
43
+ def valid_password?(password)
44
+ BCrypt::Password.new(password_hash) == password
45
+ end
46
+
47
+ def password_hash
48
+ self["Password Hash"]
49
+ end
50
+
51
+ def oauth_session
52
+ { access_token: self["Access Token"], scope: :user, refresh_token: "", token_type: :bearer, expires_in: 60000 }
53
+ end
54
+ end
55
+ end
56
+ 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.13"
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.13
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