forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebPortalSession.mm
142 lines (110 loc) · 3.57 KB
/
WebPortalSession.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#import "WebPortalSession.h"
#import "DDData.h"
#import "NSData+N2.h"
NSString* const SessionCookieName = @"OSID";
NSString* const SessionUserIDKey = @"UserID"; // NSManagedObjectID
NSString* const SessionUsernameKey = @"Username"; // NSString
NSString* const SessionTokensDictKey = @"Tokens"; // NSMutableDictionary
NSString* const SessionChallengeKey = @"Challenge"; // NSString
NSString* const SessionLastActivityDateKey = @"LastActivityDate"; // NSDate
@implementation WebPortalSession
@synthesize sid, dict;
-(id)initWithId:(NSString*)isid {
self = [super init];
sid = [isid retain];
dictLock = [[NSLock alloc] init];
dict = [[NSMutableDictionary alloc] initWithCapacity:8];
return self;
}
-(void)dealloc {
[dict release];
[sid release];
[dictLock release];
[super dealloc];
}
-(void)setObject:(id)o forKey:(NSString*)k {
[dictLock lock];
if (o) [dict setObject:o forKey:k];
else [dict removeObjectForKey:k];
[dictLock unlock];
}
-(id)objectForKey:(NSString*)k {
[dictLock lock];
id value = [dict objectForKey:k];
[dictLock unlock];
return value;
}
-(id)valueForKey:(NSString*)key {
return [self objectForKey:key];
}
-(NSMutableDictionary*)tokensDictionary {
[dictLock lock];
NSMutableDictionary* tdict = [dict objectForKey:SessionTokensDictKey];
if (!tdict) [dict setObject: tdict = [NSMutableDictionary dictionary] forKey:SessionTokensDictKey];
[dictLock unlock];
return tdict;
}
-(NSString*)createToken {
NSMutableDictionary* tokensDictionary = [self tokensDictionary];
[dictLock lock];
NSString* token;
double tokend;
do { // is this a dumb way to generate tokens?
tokend = [NSDate timeIntervalSinceReferenceDate];
} while ([[tokensDictionary allKeys] containsObject: token = [[[NSData dataWithBytes:&tokend length:sizeof(double)] md5Digest] hex]]);
[tokensDictionary setObject:[NSDate date] forKey:token];
[dictLock unlock];
return token;
}
-(BOOL)containsToken:(NSString*)token {
NSMutableDictionary* tokensDictionary = [self tokensDictionary];
[dictLock lock];
NSDate *date = [tokensDictionary objectForKey: token];
BOOL ok = NO;
if( date)
{
if( [date timeIntervalSinceNow] > -30*60) // Token are valid for 30 min
ok = YES;
else
[tokensDictionary removeObjectForKey:token];
}
[dictLock unlock];
return ok;
}
-(BOOL)consumeToken:(NSString*)token {
NSMutableDictionary* tokensDictionary = [self tokensDictionary];
[dictLock lock];
NSDate *date = [tokensDictionary objectForKey: token];
BOOL ok = NO;
if( date)
{
if( [date timeIntervalSinceNow] > -30*60) // Token are valid for 30 min
ok = YES;
[tokensDictionary removeObjectForKey:token];
}
[dictLock unlock];
return ok;
}
-(NSString*)newChallenge {
double challenged = [NSDate timeIntervalSinceReferenceDate];
NSString* challenge = [[[NSData dataWithBytes:&challenged length:sizeof(double)] md5Digest] hex];
[dict setObject:challenge forKey:SessionChallengeKey];
return challenge;
}
-(NSString*)challenge {
return [self objectForKey:SessionChallengeKey];
}
-(void)deleteChallenge {
return [dict removeObjectForKey:SessionChallengeKey];
}
@end