Пример #1
0
 public static async Task <IAuthSession> AssertAuthenticatedSessionAsync(this IRequest req, bool reload = false, CancellationToken token = default)
 => HostContext.AppHost.HasValidAuthSecret(req)
         ? HostContext.GetAuthSecretSession()
         : HostContext.AppHost.AssertAuthenticated(await req.GetSessionAsync(token: token).ConfigAwait(), req);
Пример #2
0
        internal static async Task <IAuthSession> GetSessionInternalAsync(this IRequest httpReq, bool reload, bool async, CancellationToken token = default)
        {
            if (httpReq == null)
            {
                return(null);
            }

            if (HostContext.TestMode)
            {
                var mockSession = httpReq.TryResolve <IAuthSession>(); //testing
                if (mockSession != null)
                {
                    return(mockSession);
                }
            }

            httpReq.Items.TryGetValue(Keywords.Session, out var oSession);
            if (reload && (oSession as IAuthSession)?.FromToken != true) // can't reload FromToken sessions from cache
            {
                oSession = null;
            }

            var appHost = HostContext.AppHost;

            if (oSession == null && !httpReq.Items.ContainsKey(Keywords.HasPreAuthenticated))
            {
                try
                {
                    await appHost.ApplyPreAuthenticateFiltersAsync(httpReq, httpReq.Response).ConfigAwait();

                    httpReq.Items.TryGetValue(Keywords.Session, out oSession);
                }
                catch (Exception ex)
                {
                    Log.Error("Error in GetSession() when ApplyPreAuthenticateFilters", ex);
                    /*treat errors as non-existing session*/
                }
            }

            var sessionId = httpReq.GetSessionId();
            var session   = oSession as IAuthSession;

            if (session != null)
            {
                session = appHost.OnSessionFilter(httpReq, session, sessionId);
            }
            if (session != null)
            {
                return(session);
            }

            if (appHost.HasValidAuthSecret(httpReq))
            {
                session = HostContext.GetAuthSecretSession();
                if (session != null)
                {
                    return(session);
                }
            }

            var sessionKey = SessionFeature.GetSessionKey(sessionId);

            if (sessionKey != null)
            {
                session = async
                    ? await httpReq.GetCacheClientAsync().GetAsync <IAuthSession>(sessionKey, token).ConfigAwait()
                    : httpReq.GetCacheClient().Get <IAuthSession>(sessionKey);

                if (session != null)
                {
                    session = appHost.OnSessionFilter(httpReq, session, sessionId);
                }
            }

            if (session == null)
            {
                var newSession = SessionFeature.CreateNewSession(httpReq, sessionId);
                session = appHost.OnSessionFilter(httpReq, newSession, sessionId) ?? newSession;
            }

            httpReq.Items[Keywords.Session] = session;
            return(session);
        }
Пример #3
0
 public static IAuthSession AssertAuthenticatedSession(this IRequest req, bool reload = false)
 => HostContext.AppHost.HasValidAuthSecret(req)
         ? HostContext.GetAuthSecretSession()
         : HostContext.AppHost.AssertAuthenticated(req.GetSession(), req);