authie 1.0.1 → 1.0.2
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 +4 -4
- data/db/migrate/20150109144120_add_parent_id_to_authie_sessions.rb +5 -0
- data/lib/authie/rack_controller.rb +40 -0
- data/lib/authie/session.rb +28 -21
- data/lib/authie/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a79a25563e536247558b4b4e0110c54cb0131e6
|
4
|
+
data.tar.gz: 8e56f9b73ed4104169852d2bb1f87a3b41853e98
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12d450b4dae3f88855b68f6f0b30c8e445489681e632772c09c33ec98fa2c1326d36ba545bb02f6aa51d8998505ebb10407c57150586e2033c87abd96f0ec8c4
|
7
|
+
data.tar.gz: 9873f56ce1b14561c033cfdbf1a3d3282147b2176b1167468ba6638e29db0b64b2f2df238375935c48648df8548dec9f03b1381ebeff5890e52950241eb027d4
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# If you're dealing with your authentication in a middleware and you only have
|
2
|
+
# access to your rack environment, this will wrap around rack and make it look
|
3
|
+
# close enough to an ActionController to work with Authie
|
4
|
+
#
|
5
|
+
# Usage:
|
6
|
+
#
|
7
|
+
# controller = Authie::RackController.new(@env)
|
8
|
+
# controller.current_user = user
|
9
|
+
|
10
|
+
module Authie
|
11
|
+
class RackController
|
12
|
+
|
13
|
+
attr_reader :request
|
14
|
+
|
15
|
+
def initialize(env)
|
16
|
+
@env = env
|
17
|
+
@request = ActionDispatch::Request.new(@env)
|
18
|
+
set_browser_id
|
19
|
+
end
|
20
|
+
|
21
|
+
def cookies
|
22
|
+
@request.cookie_jar
|
23
|
+
end
|
24
|
+
|
25
|
+
# Set a random browser ID for this browser.
|
26
|
+
def set_browser_id
|
27
|
+
until cookies[:browser_id]
|
28
|
+
proposed_browser_id = SecureRandom.uuid
|
29
|
+
unless Session.where(:browser_id => proposed_browser_id).exists?
|
30
|
+
cookies[:browser_id] = {:value => proposed_browser_id, :expires => 20.years.from_now}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def current_user=(user)
|
36
|
+
Session.start(self, :user => user)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
data/lib/authie/session.rb
CHANGED
@@ -1,25 +1,26 @@
|
|
1
1
|
module Authie
|
2
2
|
class Session < ActiveRecord::Base
|
3
|
-
|
3
|
+
|
4
4
|
# Define some errors which may be used
|
5
5
|
class InactiveSession < Error; end
|
6
6
|
class ExpiredSession < Error; end
|
7
7
|
class BrowserMismatch < Error; end
|
8
|
-
|
8
|
+
|
9
9
|
# Set table name
|
10
10
|
self.table_name = "authie_sessions"
|
11
|
-
|
11
|
+
|
12
12
|
# Relationships
|
13
13
|
belongs_to :user, :polymorphic => true
|
14
|
-
|
14
|
+
belongs_to :parent, :class_name => "Authie::Session"
|
15
|
+
|
15
16
|
# Scopes
|
16
17
|
scope :active, -> { where(:active => true) }
|
17
18
|
scope :asc, -> { order(:last_activity_at => :desc) }
|
18
|
-
|
19
|
+
|
19
20
|
# Attributes
|
20
21
|
serialize :data, Hash
|
21
22
|
attr_accessor :controller
|
22
|
-
|
23
|
+
|
23
24
|
before_create do
|
24
25
|
self.token = SecureRandom.base64(32)
|
25
26
|
if controller
|
@@ -31,7 +32,7 @@ module Authie
|
|
31
32
|
before_destroy do
|
32
33
|
cookies.delete(:user_session) if controller
|
33
34
|
end
|
34
|
-
|
35
|
+
|
35
36
|
# This method should be called each time a user performs an
|
36
37
|
# action while authenticated with this session.
|
37
38
|
def touch!
|
@@ -40,8 +41,8 @@ module Authie
|
|
40
41
|
self.last_activity_path = controller.request.path
|
41
42
|
self.save!
|
42
43
|
end
|
43
|
-
|
44
|
-
# Sets the cookie on the associated controller.
|
44
|
+
|
45
|
+
# Sets the cookie on the associated controller.
|
45
46
|
def set_cookie!
|
46
47
|
cookies[:user_session] = {
|
47
48
|
:value => token,
|
@@ -50,7 +51,7 @@ module Authie
|
|
50
51
|
:expires => self.expires_at
|
51
52
|
}
|
52
53
|
end
|
53
|
-
|
54
|
+
|
54
55
|
# Check the security of the session to ensure it can be used.
|
55
56
|
def check_security!
|
56
57
|
if controller
|
@@ -58,12 +59,12 @@ module Authie
|
|
58
59
|
invalidate!
|
59
60
|
raise BrowserMismatch, "Browser ID mismatch"
|
60
61
|
end
|
61
|
-
|
62
|
+
|
62
63
|
unless self.active?
|
63
64
|
invalidate!
|
64
65
|
raise InactiveSession, "Session is no longer active"
|
65
66
|
end
|
66
|
-
|
67
|
+
|
67
68
|
if self.expires_at && self.expires_at < Time.now
|
68
69
|
invalidate!
|
69
70
|
raise ExpiredSession, "Persistent session has expired"
|
@@ -75,7 +76,7 @@ module Authie
|
|
75
76
|
end
|
76
77
|
end
|
77
78
|
end
|
78
|
-
|
79
|
+
|
79
80
|
# Allow this session to persist rather than expiring at the end of the
|
80
81
|
# current browser session
|
81
82
|
def persist!
|
@@ -83,12 +84,18 @@ module Authie
|
|
83
84
|
self.save!
|
84
85
|
set_cookie!
|
85
86
|
end
|
86
|
-
|
87
|
+
|
87
88
|
# Is this a persistent session?
|
88
89
|
def persistent?
|
89
90
|
!!expires_at
|
90
91
|
end
|
91
92
|
|
93
|
+
# Activate an old session
|
94
|
+
def activate!
|
95
|
+
self.active = true
|
96
|
+
self.save!
|
97
|
+
end
|
98
|
+
|
92
99
|
# Mark this session as invalid
|
93
100
|
def invalidate!
|
94
101
|
self.active = false
|
@@ -97,7 +104,7 @@ module Authie
|
|
97
104
|
cookies.delete(:user_session)
|
98
105
|
end
|
99
106
|
end
|
100
|
-
|
107
|
+
|
101
108
|
# Set some additional data in this session
|
102
109
|
def set(key, value)
|
103
110
|
self.data ||= {}
|
@@ -109,7 +116,7 @@ module Authie
|
|
109
116
|
def get(key)
|
110
117
|
(self.data ||= {})[key.to_s]
|
111
118
|
end
|
112
|
-
|
119
|
+
|
113
120
|
# Find a session from the database for the given controller instance.
|
114
121
|
# Returns a session object or :none if no session is found.
|
115
122
|
def self.get_session(controller)
|
@@ -122,7 +129,7 @@ module Authie
|
|
122
129
|
:none
|
123
130
|
end
|
124
131
|
end
|
125
|
-
|
132
|
+
|
126
133
|
# Create a new session and return the newly created session object.
|
127
134
|
# Any other sessions for the browser will be invalidated.
|
128
135
|
def self.start(controller, params = {})
|
@@ -136,18 +143,18 @@ module Authie
|
|
136
143
|
session.save
|
137
144
|
session
|
138
145
|
end
|
139
|
-
|
146
|
+
|
140
147
|
# Cleanup any old sessions.
|
141
148
|
def self.cleanup
|
142
149
|
self.active.where("expires_at IS NULL AND last_activity_at < ?", Authie.config.session_inactivity_timeout.ago).each(&:invalidate!)
|
143
150
|
end
|
144
|
-
|
151
|
+
|
145
152
|
private
|
146
|
-
|
153
|
+
|
147
154
|
# Return all cookies on the associated controller
|
148
155
|
def cookies
|
149
156
|
controller.send(:cookies)
|
150
157
|
end
|
151
|
-
|
158
|
+
|
152
159
|
end
|
153
160
|
end
|
data/lib/authie/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: authie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Cooke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A Rails library for storing user sessions in a backend database
|
14
14
|
email:
|
@@ -19,11 +19,13 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- db/migrate/20141012174250_create_authie_sessions.rb
|
21
21
|
- db/migrate/20141013115205_add_indexes_to_authie_sessions.rb
|
22
|
+
- db/migrate/20150109144120_add_parent_id_to_authie_sessions.rb
|
22
23
|
- lib/authie.rb
|
23
24
|
- lib/authie/config.rb
|
24
25
|
- lib/authie/controller_extension.rb
|
25
26
|
- lib/authie/engine.rb
|
26
27
|
- lib/authie/error.rb
|
28
|
+
- lib/authie/rack_controller.rb
|
27
29
|
- lib/authie/session.rb
|
28
30
|
- lib/authie/version.rb
|
29
31
|
homepage: https://github.com/adamcooke/authie
|