Example #1
0
        [Authorize]     // we can live without it, because ControllerCommon.CheckAuthorizedGoogleEmail() will redirect to /login anyway, but it is quicker that this automatically redirects without clicking another URL link.
#endif
        public async Task <ActionResult> UserInfo()
        {
            var userAuthCheck = WsUtils.CheckAuthorizedGoogleEmail(this.HttpContext);

            if (userAuthCheck != UserAuthCheckResult.UserKnownAuthOK)
            {
                return(Content(@"<HTML><body>Google Authorization Is Required. You are not logged in or your your Google account is not accepted.<br>" +
                               @"<A href=""/logout"">logout this Google user </a> and login with another one." +
                               "</body></HTML>", "text/html"));
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("<html><body>");
            sb.Append("Hello " + (User.Identity?.Name ?? "anonymous") + "<br>");
            sb.Append("Request.Path '" + (Request.Path.ToString() ?? "Empty") + "'<br>");
            foreach (var claim in User.Claims)
            {
                sb.Append(claim.Type + ": " + claim.Value + "<br>");
            }

            sb.Append("Tokens:<br>");
            sb.Append("Access Token: " + await HttpContext.GetTokenAsync("access_token") + "<br>");
            sb.Append("Refresh Token: " + await HttpContext.GetTokenAsync("refresh_token") + "<br>");
            sb.Append("Token Type: " + await HttpContext.GetTokenAsync("token_type") + "<br>");
            sb.Append("expires_at: " + await HttpContext.GetTokenAsync("expires_at") + "<br>");
            sb.Append("<a href=\"/logout\">Logout</a><br>");
            sb.Append("</body></html>");

            return(Content(sb.ToString(), "text/html"));
        }
Example #2
0
        public async Task <ActionResult> Index()
        {
            string jsLogMessage = string.Empty;

            using (var reader = new StreamReader(Request.Body))
            {
                // example: '{"message":"A simple error() test message to NGXLogger","additional":[],"level":5,"timestamp":"2020-01-18T00:46:47.740Z","fileName":"ExampleJsClientGet.js","lineNumber":"52"}'
                jsLogMessage = await reader.ReadToEndAsync();
            }

            // 1. just log the event to our file log
            var clientIP        = WsUtils.GetRequestIPv6(this.HttpContext);
            var clientUserEmail = WsUtils.GetRequestUser(this.HttpContext);

            if (clientUserEmail == null)
            {
                clientUserEmail = "*****@*****.**";
            }

            string jsLogMsgWithOrigin = $"Javascript Logger /JsLogController was called by '{clientUserEmail}' from '{clientIP}'. Received JS log: '{jsLogMessage}'";

            Utils.Logger.Info(jsLogMsgWithOrigin);

            // 2. interpret the log and if it is an error, notify HealthMonitor
            try
            {
                var jsLogObj = JsonSerializer.Deserialize <NGXLogInterface>(jsLogMessage);
                if (jsLogObj == null || jsLogObj.level == NgxLoggerLevel.ERROR || jsLogObj.level == NgxLoggerLevel.FATAL)
                {   // notify HealthMonitor to send an email
                    await HealthMonitorMessage.SendAsync(jsLogMsgWithOrigin, HealthMonitorMessageID.SqCoreWebJsError);
                }
            }
            catch (Exception e)
            {
                Utils.Logger.Error(e, "JsLogController(). Probably serialization problem. JsLogMessage: " + jsLogMessage);
                throw;  // if we don't rethrow it, Kestrel will not send HealthMonitor message. Although we should fix this error.
            }

            return(NoContent()); // The common use case is to return 204 (NoContent) as a result of a PUT request, updating a resource
        }
Example #3
0
        public async Task <HttpResponseMessage> Connect(SignInSession session, string seedString, string rememberMe, int reconnectDelay = 0)
        {
            if (_connected)
            {
                throw new WebSocketError(WsAlreadyConnected, session.Username);
            }

            var timeout = false;
            var timeoutToOpenWebSocket = SetTimeout(
                () =>
            {
                if (!_connected && !Reconnecting)
                {
                    timeout = true;
                    throw new WebSocketError("timeout", "");
                }
            },
                10000
                );

            var url = $"{WsUtils.GetWsUrl(_config.Endpoint)}api?appId={_config.AppId}&sessionId={session.SessionId}&clientId={_clientId}";

            // TODO: handle timeouts
            var webSocket = new WebSocket(url);

            webSocket.Opened          += (sender, e) => OnOpened(sender, e, timeout, timeoutToOpenWebSocket);
            webSocket.DataReceived    += OnDataReceived;
            webSocket.MessageReceived += async(sender, args) => await OnMessageReceived(sender, args, webSocket, seedString, session);

            webSocket.Closed += async(sender, args) => await OnClosed(sender, args, session, seedString, rememberMe, reconnectDelay, timeout);

            webSocket.Error += OnError;
            var result = await webSocket.OpenAsync();

            return(result
                ? new HttpResponseMessage(HttpStatusCode.OK)
                : new HttpResponseMessage(HttpStatusCode.InternalServerError));
        }