<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using ServiceStack.CacheAccess;
using ServiceStack.Common;
using ServiceStack.Configuration;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.Auth;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;
using ServiceStack.WebHost.Endpoints.Extensions;

namespace ServiceStack.ServiceInterface
{
    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 GetSessionId(this IHttpRequest httpReq)
        {
            var sessionOptions = GetSessionOptions(httpReq);

            return sessionOptions.Contains(SessionOptions.Permanent)
                ? httpReq.GetItemOrCookie(SessionFeature.PermanentSessionId)
                : httpReq.GetItemOrCookie(SessionFeature.SessionId);
        }

        public static string GetPermanentSessionId(this IHttpRequest httpReq)
        {
            return httpReq.GetItemOrCookie(SessionFeature.PermanentSessionId);
        }

        public static string GetTemporarySessionId(this IHttpRequest httpReq)
        {
            return httpReq.GetItemOrCookie(SessionFeature.SessionId);
        }

        /// <summary>
        /// Create the active Session or Permanent Session Id cookie.
        /// </summary>
        /// <returns></returns>
        public static string CreateSessionId(this IHttpResponse res, IHttpRequest req)
        {
            var sessionOptions = GetSessionOptions(req);
            return sessionOptions.Contains(SessionOptions.Permanent)
                ? 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 IHttpResponse res, IHttpRequest req)
        {
            var sessionOptions = GetSessionOptions(req);
            var permId = res.CreatePermanentSessionId(req);
            var tempId = res.CreateTemporarySessionId(req);
            return sessionOptions.Contains(SessionOptions.Permanent)
                ? permId
                : tempId;
        }

        public static string CreatePermanentSessionId(this IHttpResponse res, IHttpRequest req)
        {
            var sessionId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
            res.Cookies.AddPermanentCookie(SessionFeature.PermanentSessionId, sessionId);
            req.Items[SessionFeature.PermanentSessionId] = sessionId;
            return sessionId;
        }

        public static string CreateTemporarySessionId(this IHttpResponse res, IHttpRequest req)
        {
            var sessionId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
            res.Cookies.AddSessionCookie(SessionFeature.SessionId, sessionId,
                (EndpointHost.Config != null && EndpointHost.Config.OnlySendSessionCookiesSecurely && req.IsSecureConnection));
            req.Items[SessionFeature.SessionId] = sessionId;<commit_msg>Use RNGCryptoServiceProvider to create true random session ids<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Web;
using ServiceStack.CacheAccess;
using ServiceStack.Common;
using ServiceStack.Configuration;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.Auth;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;
using ServiceStack.WebHost.Endpoints.Extensions;

namespace ServiceStack.ServiceInterface
{
    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 GetSessionId(this IHttpRequest httpReq)
        {
            var sessionOptions = GetSessionOptions(httpReq);

            return sessionOptions.Contains(SessionOptions.Permanent)
                ? httpReq.GetItemOrCookie(SessionFeature.PermanentSessionId)
                : httpReq.GetItemOrCookie(SessionFeature.SessionId);
        }

        public static string GetPermanentSessionId(this IHttpRequest httpReq)
        {
            return httpReq.GetItemOrCookie(SessionFeature.PermanentSessionId);
        }

        public static string GetTemporarySessionId(this IHttpRequest httpReq)
        {
            return httpReq.GetItemOrCookie(SessionFeature.SessionId);
        }

        /// <summary>
        /// Create the active Session or Permanent Session Id cookie.
        /// </summary>
        /// <returns></returns>
        public static string CreateSessionId(this IHttpResponse res, IHttpRequest req)
        {
            var sessionOptions = GetSessionOptions(req);
            return sessionOptions.Contains(SessionOptions.Permanent)
                ? 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 IHttpResponse res, IHttpRequest req)
        {
            var sessionOptions = GetSessionOptions(req);
            var permId = res.CreatePermanentSessionId(req);
            var tempId = res.CreateTemporarySessionId(req);
            return sessionOptions.Contains(SessionOptions.Permanent)
                ? permId
                : tempId;
        }

        static readonly RandomNumberGenerator randgen = new RNGCryptoServiceProvider();
        internal static string CreateRandomSessionId()
        {
            var data = new byte[15];
            randgen.GetBytes(data);
            return Convert.ToBase64String(data);
        }

        public static string CreatePermanentSessionId(this IHttpResponse res, IHttpRequest req)
        {
            var sessionId = CreateRandomSessionId();
            res.Cookies.AddPermanentCookie(SessionFeature.PermanentSessionId, sessionId);
            req.Items[SessionFeature.PermanentSessionId] = sessionId;
            return sessionId;
        }

        public static string CreateTemporarySessionId(this IHttpResponse res, IHttpRequest req)
        {
            var sessionId = CreateRandomSessionId();
            res.Cookies.AddSessionCookie(SessionFeature.SessionId, sessionId,
                (EndpointHost.Config != null && EndpointHost.Config.OnlySendSessionCookiesSecurely && req.IsSecureConnection));
            req.Items[SessionFeature.SessionId] = sessionId;
{'fraction_non_alphanumeric': 0.07694495804295264, 'fraction_numerical': 0.0011378182335371925, 'mean_word_length': 2.5950920245398774, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6716198', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1692, 'n_words': 387}