Пример #1
0
        public async Task <ActionResult> Authorize(MobileConnectAuthorizeModel model)
        {
            if (string.IsNullOrEmpty(model.PhoneNumber) ||
                string.IsNullOrEmpty(model.RedirectUrl) ||
                string.IsNullOrEmpty(model.NotificationUri) ||
                string.IsNullOrEmpty(model.DiscoveryUrl) ||
                string.IsNullOrEmpty(model.DiscoveryClientId) ||
                string.IsNullOrEmpty(model.DiscoveryPassword) ||
                string.IsNullOrEmpty(model.PrivateRsaKeyPath))
            {
                return(Content("Fill all fields"));
            }

            var authorizeSettings = new MobileConnectSiAuthorizeSettings
            {
                PhoneNumber       = model.PhoneNumber,
                RedirectUrl       = model.RedirectUrl,
                NotificationUri   = model.NotificationUri,
                DiscoveryUrl      = model.DiscoveryUrl,
                DiscoveryClientId = model.DiscoveryClientId,
                DiscoveryPassword = model.DiscoveryPassword,
                PrivateRsaKeyPath = model.PrivateRsaKeyPath
            };

            var authorizeResult = await _mobileConnectService.SiAuthorize(authorizeSettings);

            if (!authorizeResult.IsSucceeded)
            {
                MobileConnectAuthorizeLogger.Warn(
                    $"Authorize [authReqId: {authorizeResult.AuthReqId}, correlationId: {authorizeResult.CorrelationId}]. authorizeResult is not succeeded");
                MobileConnectAuthorizeLogger.Warn(authorizeResult.ToString());
            }

            var mobileConnectRequest = new MobileConnectAuthorizeRequest
            {
                PhoneNumber             = model.PhoneNumber,
                ClientNotificationToken = authorizeResult.ClientNotificationToken,
                AuthReqId            = authorizeResult.AuthReqId,
                CorrelationId        = authorizeResult.CorrelationId,
                Nonce                = authorizeResult.Nonce,
                IsResponseSucceeded  = authorizeResult.IsSucceeded,
                ResponseErrorMessage = authorizeResult.ErrorMessage,
                RequestedDateTime    = DateTime.Now
            };

            _repository.CreateMobileConnectAuthorizeRequest(mobileConnectRequest);

            return(PartialView("_MobileConnectAuthorizePartial", authorizeResult));
        }
Пример #2
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));
        }