app_rail-airtable 0.2.9 → 0.2.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/app_rail/airtable/authenticatable.rb +56 -0
- data/lib/app_rail/airtable/authentication_helpers.rb +27 -0
- data/lib/app_rail/airtable/sinatra.rb +2 -2
- data/lib/app_rail/airtable/version.rb +1 -1
- data/lib/app_rail/airtable.rb +2 -1
- metadata +3 -2
- data/lib/app_rail/airtable/authentication_record.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b50f647ec9cbe6a4872a61db88b1bdb15eb9c960b90c219fc5023ea38078e940
|
4
|
+
data.tar.gz: ab041ecd7134b4f611460f0e7cacf94fdb1e873066d148d7e69217dc904a018c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbbe3242c32cf4a94a045e58085024224c1b18423936f89228dc00f6672f6c76db979ac6bc242ec89792d3f330626d0a0b900154efd941cb9314545b4109e033
|
7
|
+
data.tar.gz: 2c3827c50ebdda11aebd971160d45d2fedae642628f59f757f43c48ee020607eca527da0cc159ecb6b7387b371e5c74d27660e960746dff5bf4ad06c1f6442d9
|
data/Gemfile.lock
CHANGED
@@ -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
|
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:
|
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
|
data/lib/app_rail/airtable.rb
CHANGED
@@ -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/
|
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.
|
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/
|
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
|