Exemplo n.º 1
0
        public ActionResult Notify()
        {
            var notifyGuid = Guid.NewGuid().ToString().Substring(0, 13);

            var notifyModel = ValidateNotifyRequestAndGetMobileConnectNotifyModel(notifyGuid, out var validationErrorMessage);

            if (!string.IsNullOrEmpty(validationErrorMessage))
            {
                MobileConnectNotifyLogger.Warn(
                    $"Notify [#: {notifyGuid}]. {validationErrorMessage}");

                return(BadRequestResult(validationErrorMessage));
            }

            var authReqId     = notifyModel.AuthReqId;
            var correlationId = notifyModel.CorrelationId;

            var mobileConnectRequest =
                _repository.GetMobileConnectAuthorizeRequest(authReqId, correlationId);

            if (mobileConnectRequest == null)
            {
                var errorMessage = "mobile connect request not found";
                MobileConnectNotifyLogger.Warn(
                    $"Notify [#: {notifyGuid}, authReqId: {authReqId}, correlationId: {correlationId}]. {errorMessage}");

                return(BadRequestResult(errorMessage));
            }

            ValidateNotifyRequestAuthorization(mobileConnectRequest.ClientNotificationToken, out var authErrorMessage);

            if (!string.IsNullOrEmpty(authErrorMessage))
            {
                MobileConnectNotifyLogger.Warn(
                    $"Notify [#: {notifyGuid}, authReqId: {authReqId}, correlationId: {correlationId}]. {authErrorMessage}");

                return(BadRequestResult(authErrorMessage));
            }

            if (mobileConnectRequest.IsAuthorized == true)
            {
                MobileConnectNotifyLogger.Info(
                    $"Notify [#: {notifyGuid}, authReqId: {authReqId}, correlationId: {correlationId}]. Already Authorized at {mobileConnectRequest.AuthorizedDateTime}.");

                return(new HttpStatusCodeResult(HttpStatusCode.NoContent));
            }

            mobileConnectRequest.IsNotificationReceived = true;

            if (!string.IsNullOrEmpty(notifyModel.Error))
            {
                var errorMessage = $"{notifyModel.Error} - {notifyModel.ErrorDescription}";

                MobileConnectNotifyLogger.Warn(
                    $"Notify [#: {notifyGuid}, authReqId: {authReqId}, correlationId: {correlationId}]. {errorMessage}");

                return(BadRequestResult(errorMessage));
            }
            else
            {
                ValidateTokens(notifyModel, mobileConnectRequest, out string errorMessage);

                if (!string.IsNullOrEmpty(errorMessage))
                {
                    MobileConnectNotifyLogger.Warn(
                        $"Notify [#: {notifyGuid}, authReqId: {authReqId}, correlationId: {correlationId}]. {errorMessage}");

                    return(BadRequestResult(errorMessage));
                }
            }

            mobileConnectRequest.IsAuthorized       = true;
            mobileConnectRequest.AuthorizedDateTime = DateTime.Now;

            MobileConnectNotifyLogger.Info(
                $"Notify [#: {notifyGuid}, authReqId: {authReqId}, correlationId: {correlationId}]. Authorized.");

            return(new HttpStatusCodeResult(HttpStatusCode.NoContent));
        }