Exemplo n.º 1
0
    private void RefreshIfNecessary(User user)
    {
        if (cache.ContainsKey(user.Username))
        {
            var entry = cache[user.Username];
            if (DateTime.Now.Subtract(entry.Timestamp).TotalSeconds < CacheValidity)
            {
                return;
            }
            else
            {
                cache.Remove(user.Username);
            }
        }

        var result   = realAuthorizator.ListPermissions(user);
        var newEntry = new CacheEntry <Permission[]>(user, DateTime.Now, result);

        cache.Add(user.Username, newEntry);
    }
Exemplo n.º 2
0
    private void Server_DataRecieved(string host, byte[] requestData)
    {
        // Match host session
        var session = sessions.GetSession(host);

        if (session == null)
        {
            if (UnknownSession != null)
            {
                UnknownSession(string.Format("Session not found for host: {0}.", host));
            }

            return;
        }

        // Commit request session phase
        Request request = null;

        if (session.CurrentPhase == SessionPhase.Hello)
        {
            request = MatchRequest <HelloRequest>(requestData);
            if (request == null)
            {
                return;
            }
        }
        else if (session.CurrentPhase == SessionPhase.Encryption)
        {
            var data = DecryptRequest(requestData, asymetricCrypter);
            if (data == null)
            {
                return;
            }

            request = MatchRequest <KeyExchangeRequest>(data);
        }
        else if (session.CurrentPhase == SessionPhase.Authentication)
        {
            var data = DecryptRequest(requestData, session.Crpter);
            if (data == null)
            {
                return;
            }

            request = MatchRequest <AuthenticateRequest>(data);
        }
        else
        {
            var data = DecryptRequest(requestData, session.Crpter);
            if (data == null)
            {
                return;
            }

            request = MatchRequest <Request>(data);
        }
        request.Session = session;

        Response response = null;

        // Authenitacate request user
        AuthenticationResult result = AuthenticationResult.Denied;

        if (request.RequiresAuthentication)
        {
            string password = session.Crpter.DecryptString(request.User.PasswordEnc);
            result = authenticator.Authenticate(request.User, password);
        }
        if (!request.RequiresAuthentication || result == AuthenticationResult.Granted)
        {
            // Check user permissions for request (authorize request user)
            Permission[] permissions = null;
            if (request.RequiresAuthorization)
            {
                permissions = authorizator.ListPermissions(request.User);
            }

            if (!request.RequiresAuthorization || request.MatchesPermissions(permissions))
            {
                try
                {
                    // EXECUTE REQUEST
                    ProgramOutput.Info("Executing request: {0}...", request.ToString());
                    response = request.Execute();
                }
                catch (Exception ex)
                {
                    response = new CannotExecuteResponse(ex);
                    if (ExecuteError != null)
                    {
                        ExecuteError(request, ex);
                    }
                }
            }
            else
            {
                response = new AccessDeniedResponse();
                if (AccessDenied != null)
                {
                    AccessDenied(request);
                }
            }
        }
        else if (result == AuthenticationResult.Denied)
        {
            response = new UnauthorizedResponse(request.User);
            if (Unauthorized != null)
            {
                Unauthorized(request);
            }
        }

        if (!(request is GoodbyeRequest))
        {
            // Serialize response
            byte[] responseData = null;
            try
            {
                responseData = formatter.Format(response);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (InvalidResponse != null)
                {
                    InvalidResponse(request);
                }
                return;
            }

            // Encrypt response
            if (session.CurrentPhase == SessionPhase.Authentication || session.CurrentPhase == SessionPhase.Established)
            {
                responseData = EncryptResponse(responseData, session.Crpter);
            }
            if (responseData == null)
            {
                return;
            }

            // Send response
            try
            {
                ProgramOutput.Info("Responding: {0}, status code: {1}", response, response.Code);
                server.SendMessage(host, responseData);
            }
            catch (SocketException)
            {
                if (NetProblem != null)
                {
                    NetProblem(request);
                }
                return;
            }

            session.NextPhase();
        }
    }