-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathSessionExtensions.cs
More file actions
392 lines (320 loc) · 14.4 KB
/
SessionExtensions.cs
File metadata and controls
392 lines (320 loc) · 14.4 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using ServiceStack.Auth;
using ServiceStack.Caching;
using ServiceStack.Host;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack
{
public class SessionOptions
{
public const string Temporary = "temp";
public const string Permanent = "perm";
}
/// <summary>
/// Configure ServiceStack to have ISession support
/// </summary>
public static class SessionExtensions
{
public static string GetOrCreateSessionId(this IRequest httpReq)
{
var sessionId = httpReq.GetSessionId();
return sessionId ?? SessionFeature.CreateSessionIds(httpReq);
}
public static void SetSessionId(this IRequest req, string sessionId)
{
if (string.IsNullOrEmpty(sessionId))
return;
var sessionKey = req.IsPermanentSession()
? SessionFeature.PermanentSessionId
: SessionFeature.SessionId;
req.Items[sessionKey] = sessionId;
}
public static string GetSessionId(this IRequest req)
{
if (req == null)
req = HostContext.GetCurrentRequest();
return req.IsPermanentSession()
? req.GetPermanentSessionId()
: req.GetTemporarySessionId();
}
public static string GetPermanentSessionId(this IRequest httpReq)
{
return httpReq.GetSessionParam(SessionFeature.PermanentSessionId);
}
public static string GetTemporarySessionId(this IRequest httpReq)
{
return httpReq.GetSessionParam(SessionFeature.SessionId);
}
public static string GetSessionParam(this IRequest httpReq, string sessionKey)
{
return httpReq.GetItem(sessionKey) as string
?? httpReq.GetHeader("X-" + sessionKey)
?? (HostContext.Config.AllowSessionIdsInHttpParams
? (httpReq.QueryString[sessionKey] ?? httpReq.FormData[sessionKey])
: null)
?? httpReq.GetCookieValue(sessionKey);
}
/// <summary>
/// Create the active Session or Permanent Session Id cookie.
/// </summary>
/// <returns></returns>
public static string CreateSessionId(this IResponse res, IRequest req)
{
return req.IsPermanentSession()
? res.CreatePermanentSessionId(req)
: res.CreateTemporarySessionId(req);
}
/// <summary>
/// Create both Permanent and Session Id cookies and return the active sessionId
/// </summary>
/// <returns></returns>
public static string CreateSessionIds(this IResponse res, IRequest req)
{
var permId = res.CreatePermanentSessionId(req);
var tempId = res.CreateTemporarySessionId(req);
return req.IsPermanentSession()
? permId
: tempId;
}
static readonly RandomNumberGenerator randgen = RandomNumberGenerator.Create();
[ThreadStatic] static byte[] SessionBytesCache;
public static string CreateRandomSessionId()
{
if (SessionBytesCache == null)
SessionBytesCache = new byte[15];
string base64Id;
do
{
PopulateWithSecureRandomBytes(SessionBytesCache);
base64Id = Convert.ToBase64String(SessionBytesCache);
} while (Base64StringContainsUrlUnfriendlyChars(base64Id));
return base64Id;
}
public static void PopulateWithSecureRandomBytes(byte[] bytes)
{
randgen.GetBytes(bytes);
}
public static string CreateRandomBase64Id(int size = 15)
{
var data = new byte[size];
randgen.GetBytes(data);
return Convert.ToBase64String(data);
}
public static string CreateRandomBase62Id(int size)
{
var bytes = new byte[size];
string base64Id;
do
{
PopulateWithSecureRandomBytes(bytes);
base64Id = Convert.ToBase64String(bytes);
} while (Base64StringContainsUrlUnfriendlyChars(base64Id));
return base64Id;
}
static readonly char[] UrlUnsafeBase64Chars = { '+', '/' };
public static bool Base64StringContainsUrlUnfriendlyChars(string base64)
{
return base64.IndexOfAny(UrlUnsafeBase64Chars) >= 0;
}
public static string CreatePermanentSessionId(this IResponse res, IRequest req) =>
CreateSessionId(req, res, SessionFeature.PermanentSessionId, CreateRandomSessionId());
public static string CreateTemporarySessionId(this IResponse res, IRequest req) =>
CreateSessionId(req, res, SessionFeature.SessionId, CreateRandomSessionId());
public static string CreateSessionId(this IRequest req, IResponse res, string sessionKey, string sessionId)
{
if (sessionKey == SessionFeature.SessionId)
{
(res as IHttpResponse)?.Cookies.AddSessionCookie(sessionKey, sessionId,
HostContext.Config.UseSecureCookies && req.IsSecureConnection);
}
else
{
(res as IHttpResponse)?.Cookies.AddPermanentCookie(sessionKey, sessionId,
HostContext.Config.UseSecureCookies && req.IsSecureConnection);
}
req.Items[sessionKey] = sessionId;
return sessionId;
}
public static string CreateTemporarySessionId(this IRequest req, string sessionId) =>
CreateSessionId(req, req.Response, SessionFeature.SessionId, sessionId);
public static string CreatePermanentSessionId(this IRequest req, string sessionId) =>
CreateSessionId(req, req.Response, SessionFeature.PermanentSessionId, sessionId);
public static bool IsPermanentSession(this IRequest req)
{
return req != null && GetSessionOptions(req).Contains(SessionOptions.Permanent);
}
public static HashSet<string> GetSessionOptions(this IRequest httpReq)
{
var sessionOptions = httpReq.GetSessionParam(SessionFeature.SessionOptionsKey);
return sessionOptions.IsNullOrEmpty()
? new HashSet<string>()
: sessionOptions.Split(',').ToSet();
}
public static string GetUserAuthName(this IAuthSession session) => session == null ? null
: session.UserAuthName ?? session.UserName ?? session.Email;
public static string GetUserAuthIdOrName(this IAuthSession session) => session == null ? null
: session.UserAuthId ?? session.UserAuthName ?? session.UserName ?? session.Email;
public static void UpdateSession(this IAuthSession session, IUserAuth userAuth)
{
if (userAuth == null || session == null) return;
if (session.Roles == null)
session.Roles = new List<string>();
if (session.Permissions == null)
session.Permissions = new List<string>();
userAuth.Roles?.ForEach(x => session.Roles.AddIfNotExists(x));
userAuth.Permissions?.ForEach(x => session.Permissions.AddIfNotExists(x));
}
public static HashSet<string> AddSessionOptions(this IRequest req, params string[] options)
{
if (req == null || options.Length == 0)
return new HashSet<string>();
var existingOptions = req.GetSessionOptions();
foreach (var option in options)
{
if (option.IsNullOrEmpty()) continue;
if (option == SessionOptions.Permanent)
existingOptions.Remove(SessionOptions.Temporary);
else if (option == SessionOptions.Temporary)
existingOptions.Remove(SessionOptions.Permanent);
existingOptions.Add(option);
}
var strOptions = string.Join(",", existingOptions.ToArray());
var httpRes = req.Response as IHttpResponse;
httpRes?.Cookies.AddPermanentCookie(SessionFeature.SessionOptionsKey, strOptions);
req.Items[SessionFeature.SessionOptionsKey] = strOptions;
return existingOptions;
}
public static string GetSessionKey(IRequest httpReq = null)
{
var sessionId = httpReq.GetSessionId();
return sessionId == null ? null : SessionFeature.GetSessionKey(sessionId);
}
public static TUserSession SessionAs<TUserSession>(this ICacheClient cache,
IRequest httpReq = null, IResponse httpRes = null)
{
return SessionFeature.GetOrCreateSession<TUserSession>(cache, httpReq, httpRes);
}
public static Task<TUserSession> SessionAsAsync<TUserSession>(this ICacheClientAsync cache,
IRequest httpReq = null, IResponse httpRes = null, CancellationToken token=default)
{
return SessionFeature.GetOrCreateSessionAsync<TUserSession>(cache, httpReq, httpRes, token);
}
public static IAuthSession GetUntypedSession(this ICacheClient cache,
IRequest httpReq = null, IResponse httpRes = null)
{
var sessionKey = GetSessionKey(httpReq);
if (sessionKey != null)
{
var userSession = cache.Get<IAuthSession>(sessionKey);
if (!Equals(userSession, default(AuthUserSession)))
return userSession;
}
if (sessionKey == null)
SessionFeature.CreateSessionIds(httpReq, httpRes);
var unAuthorizedSession = (IAuthSession)typeof(AuthUserSession).CreateInstance();
return unAuthorizedSession;
}
public static async Task<IAuthSession> GetUntypedSessionAsync(this ICacheClientAsync cache,
IRequest httpReq = null, IResponse httpRes = null, CancellationToken token=default)
{
var sessionKey = GetSessionKey(httpReq);
if (sessionKey != null)
{
var userSession = await cache.GetAsync<IAuthSession>(sessionKey, token);
if (!Equals(userSession, default(AuthUserSession)))
return userSession;
}
if (sessionKey == null)
SessionFeature.CreateSessionIds(httpReq, httpRes);
var unAuthorizedSession = (IAuthSession)typeof(AuthUserSession).CreateInstance();
return unAuthorizedSession;
}
public static void ClearSession(this ICacheClient cache, IRequest httpReq = null)
{
cache.Remove(GetSessionKey(httpReq));
}
public static Task ClearSessionAsync(this ICacheClientAsync cache, IRequest httpReq = null, CancellationToken token=default)
{
return cache.RemoveAsync(GetSessionKey(httpReq), token);
}
public static ISession GetSessionBag(this IRequest request)
{
var factory = request.TryResolve<ISessionFactory>() ?? new SessionFactory(request.GetCacheClient());
return factory.GetOrCreateSession(request, request.Response);
}
public static ISessionAsync GetSessionBagAsync(this IRequest request, CancellationToken token=default)
{
var factory = request.TryResolve<ISessionFactory>() ?? new SessionFactory(request.GetCacheClient(), request.GetCacheClientAsync());
return factory.GetOrCreateSessionAsync(request, request.Response);
}
public static ISession GetSessionBag(this IServiceBase service)
{
return service.Request.GetSessionBag();
}
public static ISessionAsync GetSessionBagAsync(this IServiceBase service, CancellationToken token=default)
{
return service.Request.GetSessionBagAsync();
}
public static T Get<T>(this ISession session)
{
return session.Get<T>(typeof(T).Name);
}
public static Task<T> GetAsync<T>(this ISessionAsync session, CancellationToken token=default)
{
return session.GetAsync<T>(typeof(T).Name, token);
}
public static void Set<T>(this ISession session, T value)
{
session.Set(typeof(T).Name, value);
}
public static Task SetAsync<T>(this ISessionAsync session, T value, CancellationToken token=default)
{
return session.SetAsync(typeof(T).Name, value, token);
}
public static void Remove<T>(this ISession session)
{
session.Remove(typeof(T).Name);
}
public static Task RemoveAsync<T>(this ISessionAsync session, CancellationToken token=default)
{
return session.RemoveAsync(typeof(T).Name, token);
}
public static void DeleteSessionCookies(this IResponse response)
{
if (!(response is IHttpResponse httpRes)) return;
httpRes.Cookies.DeleteCookie(Keywords.SessionId);
httpRes.Cookies.DeleteCookie(Keywords.PermanentSessionId);
httpRes.Cookies.DeleteCookie(HttpHeaders.XUserAuthId);
}
public static void DeleteJwtCookie(this IResponse response)
{
var httpRes = response as IHttpResponse;
httpRes?.Cookies.DeleteCookie(Keywords.TokenCookie);
httpRes?.Cookies.DeleteCookie(Keywords.RefreshTokenCookie);
}
public static async Task GenerateNewSessionCookiesAsync(this IRequest req, IAuthSession session, CancellationToken token=default)
{
if (!(req.Response is IHttpResponse httpRes))
return;
var sessionId = req.GetSessionId();
if (sessionId != null)
await req.RemoveSessionAsync(sessionId, token);
req.Response.ClearCookies();
var tempId = req.Response.CreateTemporarySessionId(req);
var permId = req.Response.CreatePermanentSessionId(req);
var isPerm = req.IsPermanentSession();
req.AddSessionOptions(isPerm ? SessionOptions.Permanent : SessionOptions.Temporary);
session.Id = isPerm
? permId
: tempId;
req.Items[Keywords.Session] = session;
}
}
}