jj-middleware 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/jj-middleware.gemspec +1 -0
- data/lib/jj/middleware.rb +1 -1
- data/lib/jj/middleware/auth.rb +14 -8
- data/lib/jj/middleware/base.rb +16 -1
- data/lib/jj/middleware/onboarding.rb +4 -4
- data/lib/jj/middleware/session.rb +16 -5
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e972d093a7be26e5746d753ca4df5efbd450044f
|
4
|
+
data.tar.gz: cc02197f544e446abda319824afddff3b9d66952
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35b16bc35278be173cb01e96637a438c74a94814e14f28d3e6221a1516f1aa57dc9268fb518707767741c737e0046091009e6d977037ef155d59e6b0a3221d59
|
7
|
+
data.tar.gz: 467590c24da14c2643c4547592855f3c5d42999620a11920cccb482ca7937e502b6fee8e69d27f18528c553b60885ebbb3a003053d4f6f3f36eca6f146cdea3f
|
data/jj-middleware.gemspec
CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.add_development_dependency "bundler", "~> 1.9"
|
21
21
|
spec.add_development_dependency "pry"
|
22
|
+
spec.add_development_dependency "rack", "~> 1.5"
|
22
23
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
24
|
spec.add_development_dependency "rspec", "~> 3.0"
|
24
25
|
|
data/lib/jj/middleware.rb
CHANGED
data/lib/jj/middleware/auth.rb
CHANGED
@@ -9,31 +9,37 @@ module JJ
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def call(env)
|
12
|
-
|
13
|
-
logged_in?(
|
12
|
+
session = Session.new(env)
|
13
|
+
logged_in?(session) ? @app.call(env) : redirect("/login")
|
14
14
|
end
|
15
15
|
|
16
16
|
private
|
17
17
|
|
18
|
-
def logged_in?(
|
19
|
-
unless
|
20
|
-
|
18
|
+
def logged_in?(session)
|
19
|
+
unless session.exist?
|
20
|
+
warn("Session does not exist")
|
21
21
|
return false
|
22
22
|
end
|
23
23
|
|
24
|
-
user =
|
24
|
+
user = session.fetch("user")
|
25
25
|
if user.nil?
|
26
|
-
|
26
|
+
warn("User key doesn't exist in session")
|
27
27
|
return false
|
28
28
|
end
|
29
29
|
|
30
30
|
unless user.id && user.email
|
31
|
-
|
31
|
+
warn("User ID/Email doesn't exist in session")
|
32
32
|
return false
|
33
33
|
end
|
34
34
|
|
35
35
|
true
|
36
36
|
end
|
37
|
+
|
38
|
+
def warn(msg)
|
39
|
+
log.warn(
|
40
|
+
format("[JJ::Auth] %s.", msg)
|
41
|
+
)
|
42
|
+
end
|
37
43
|
end
|
38
44
|
end
|
39
45
|
end
|
data/lib/jj/middleware/base.rb
CHANGED
@@ -6,12 +6,27 @@ module JJ
|
|
6
6
|
protected
|
7
7
|
|
8
8
|
def log
|
9
|
-
|
9
|
+
logger = test? ? ::Logger.new("/dev/null") : ::Logger.new(STDOUT)
|
10
|
+
logger.formatter = logging_formatter
|
11
|
+
@log ||= logger
|
10
12
|
end
|
11
13
|
|
12
14
|
def redirect(path, code = 302)
|
13
15
|
[code, { "Location" => path, "Content-Type" => "text/html" }, []]
|
14
16
|
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def logging_formatter
|
21
|
+
proc do |severity, datetime, _, msg|
|
22
|
+
time = datetime.strftime("%Y/%m/%d %H:%M:%S")
|
23
|
+
format("%s %s - %s\n", severity, time, msg)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test?
|
28
|
+
ENV["APP_ENV"] == "test"
|
29
|
+
end
|
15
30
|
end
|
16
31
|
end
|
17
32
|
end
|
@@ -10,13 +10,13 @@ module JJ
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def call(env)
|
13
|
-
|
14
|
-
user =
|
15
|
-
|
13
|
+
session = Session.new(env)
|
14
|
+
user = session.fetch("user")
|
15
|
+
|
16
16
|
unless user_exist?(user)
|
17
17
|
log.info("[JJ::Onboarding] User record not yet created.")
|
18
18
|
record = create_user!(user.id)
|
19
|
-
|
19
|
+
session.update_user_model(record)
|
20
20
|
return redirect("/onboarding")
|
21
21
|
end
|
22
22
|
|
@@ -1,22 +1,33 @@
|
|
1
|
+
require "rack/request"
|
2
|
+
|
1
3
|
module JJ
|
2
4
|
module Middleware
|
3
5
|
class Session
|
4
6
|
def initialize(env)
|
5
|
-
|
7
|
+
raise(ArgumentError, "'env' must be Hash") unless env.is_a?(Hash)
|
8
|
+
@request = Rack::Request.new(env)
|
6
9
|
end
|
7
10
|
|
8
11
|
def exist?
|
9
|
-
|
12
|
+
!@request.session.nil?
|
10
13
|
end
|
11
14
|
|
12
15
|
def fetch(key = nil)
|
13
|
-
|
16
|
+
validate_fetch_key!(key)
|
17
|
+
@request.session.tap do |s|
|
14
18
|
return s[key] unless key.nil?
|
15
19
|
end
|
16
20
|
end
|
17
21
|
|
18
|
-
def
|
19
|
-
|
22
|
+
def update_user_model(record)
|
23
|
+
fetch("user").model = record
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def validate_fetch_key!(k)
|
29
|
+
return if k.nil? || k.is_a?(String)
|
30
|
+
raise(ArgumentError, "'key' must be String/nil")
|
20
31
|
end
|
21
32
|
end
|
22
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jj-middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ''
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rack
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rake
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|