@@ -4,31 +4,34 @@ module Storage
4
4
class AbstractTicketStore
5
5
6
6
attr_accessor :log
7
- @log = CASClient ::LoggerWrapper . new
7
+ def log
8
+ @log ||= CASClient ::LoggerWrapper . new
9
+ end
8
10
9
- def process_single_sign_out ( si )
11
+ def process_single_sign_out ( st )
10
12
11
- session_id , session = get_session_for_service_ticket ( si )
13
+ session_id , session = get_session_for_service_ticket ( st )
12
14
if session
13
15
session . destroy
14
- log . debug ( "Destroyed #{ session . inspect } for session #{ session_id . inspect } corresponding to service ticket #{ si . inspect } ." )
16
+ log . debug ( "Destroyed #{ session . inspect } for session #{ session_id . inspect } corresponding to service ticket #{ st . inspect } ." )
15
17
else
16
18
log . debug ( "Data for session #{ session_id . inspect } was not found. It may have already been cleared by a local CAS logout request." )
17
19
end
18
20
19
21
if session_id
20
22
log . info ( "Single-sign-out for service ticket #{ session_id . inspect } completed successfuly." )
21
23
else
22
- log . debug ( "No session id found for CAS ticket #{ si } " )
24
+ log . debug ( "No session id found for CAS ticket #{ st } " )
23
25
end
24
26
end
25
27
26
28
def get_session_for_service_ticket ( st )
27
- session_id = read_service_session_lookup ( si )
28
- if session_id
29
- session = ActiveRecord ::SessionStore ::Session . find_by_session_id ( session_id )
29
+ session_id = read_service_session_lookup ( st )
30
+ unless session_id . nil?
31
+ # This feels a bit hackish, but there isn't really a better way to go about it that I am aware of yet
32
+ session = ActiveRecord ::SessionStore . session_class . find_by_session_id ( session_id )
30
33
else
31
- log . warn ( "Couldn't destroy session with SessionIndex #{ si } because no corresponding session id could be looked up ." )
34
+ log . warn ( "Couldn't destroy session service ticket #{ st } because no corresponding session id could be found ." )
32
35
end
33
36
[ session_id , session ]
34
37
end
@@ -53,6 +56,12 @@ def retrieve_pgt(pgt_iou)
53
56
def read_service_session_lookup ( st )
54
57
raise 'Implement this in a subclass!'
55
58
end
59
+
60
+ def session_id_from_controller ( controller )
61
+ session_id = controller . request . session_options [ :id ] || controller . session . session_id
62
+ raise CASClient ::CASException , "Failed to extract session_id from controller" if session_id . nil?
63
+ session_id
64
+ end
56
65
end
57
66
58
67
# A Ticket Store that keeps it's tickets in a directory on the local filesystem.
@@ -83,10 +92,10 @@ def initialize(config={})
83
92
# Rails session id.
84
93
# Returns the filename of the lookup file created.
85
94
def store_service_session_lookup ( st , controller )
86
- raise CASException , "No service_ticket specified." unless st
87
- raise CASException , "No controller specified." unless controller
95
+ raise CASException , "No service_ticket specified." if st . nil?
96
+ raise CASException , "No controller specified." if controller . nil?
88
97
89
- sid = controller . request . session_options [ :id ] || controller . session . session_id
98
+ sid = session_id_from_controller ( controller )
90
99
91
100
st = st . ticket if st . kind_of? ServiceTicket
92
101
f = File . new ( filename_of_service_session_lookup ( st ) , 'w' )
@@ -100,11 +109,11 @@ def store_service_session_lookup(st, controller)
100
109
# cas_sess.<session ticket> file created in a prior call to
101
110
# #store_service_session_lookup.
102
111
def read_service_session_lookup ( st )
103
- raise CASException , "No service_ticket specified." unless st
112
+ raise CASException , "No service_ticket specified." if st . nil?
104
113
105
114
st = st . ticket if st . kind_of? ServiceTicket
106
115
ssl_filename = filename_of_service_session_lookup ( st )
107
- return File . exists? ( ssl_filename ) && IO . read ( ssl_filename )
116
+ return IO . read ( ssl_filename ) if File . exists? ( ssl_filename )
108
117
end
109
118
110
119
# Removes a stored relationship between a ServiceTicket and a local
@@ -113,14 +122,17 @@ def read_service_session_lookup(st)
113
122
#
114
123
# See #store_service_session_lookup.
115
124
def cleanup_service_session_lookup ( st )
116
- raise CASException , "No service_ticket specified." unless st
125
+ raise CASException , "No service_ticket specified." if st . nil?
117
126
118
127
st = st . ticket if st . kind_of? ServiceTicket
119
128
ssl_filename = filename_of_service_session_lookup ( st )
120
129
File . delete ( ssl_filename ) if File . exists? ( ssl_filename )
121
130
end
122
131
123
132
def save_pgt_iou ( pgt_iou , pgt )
133
+ raise CASException , "Invalid pgt_iou" if pgt_iou . nil?
134
+ raise CASException , "Invalid pgt" if pgt . nil?
135
+
124
136
# TODO: pstore contents should probably be encrypted...
125
137
pstore = open_pstore
126
138
@@ -135,17 +147,14 @@ def retrieve_pgt(pgt_iou)
135
147
pstore = open_pstore
136
148
137
149
pgt = nil
150
+ # TODO: need to periodically clean the storage, otherwise it will just keep growing
138
151
pstore . transaction do
139
152
pgt = pstore [ pgt_iou ]
153
+ pstore . delete pgt_iou
140
154
end
141
155
142
156
raise CASException , "Invalid pgt_iou specified. Perhaps this pgt has already been retrieved?" unless pgt
143
157
144
- # TODO: need to periodically clean the storage, otherwise it will just keep growing
145
- pstore . transaction do
146
- pstore . delete pgt_iou
147
- end
148
-
149
158
pgt
150
159
end
151
160
0 commit comments