예제 #1
0
 public AuthProblemDetails(AuthException authException)
 {
     Title  = "Code Error";
     Status = StatusCodes.Status409Conflict;
     Detail = authException.Code;
     Type   = "Unexpected error";
 }
예제 #2
0
        private TcpMessage HandleUniqueItemQuery(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.UniqueItemQuery))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ParamTcpMessage(message);

            var response = new ObjectTcpMessage <DbItem>(TcpRequestType.List);

            if (request.HasParam("id"))
            {
                var id = request.GetParamAsNullableInt("id");
                if (id != null)
                {
                    var item = _dataService.GetItem(id.Value);
                    response.Add(item);
                    return(response);
                }
            }

            return(TcpMessage.Error("Missing or badly formatted query parameters"));
        }
예제 #3
0
        private TcpMessage HandleMultiRelationQuery(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.MultiRelationQuery))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ParamTcpMessage(message);

            var response = new ObjectTcpMessage <DbRelation>(TcpRequestType.List);

            if (request.HasParam("from_id"))
            {
                var fromId = request.GetParamAsNullableInt("from_id");
                var items  = _dataService.GetRelations(fromId);
                response.Add(items);
                return(response);
            }

            return(TcpMessage.Error("Missing or badly formatted query parameters"));
        }
예제 #4
0
        private TcpMessage HandleInsertItem(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.InsertItem))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ObjectTcpMessage <DbItem>(message);

            var response = new ObjectTcpMessage <DbItem>(TcpRequestType.List);
            var item     = request.Item;

            if (item != null)
            {
                _dataService.Insert(item);
                response.Add(item); // Send the item back to report assigned Id
            }

            return(response);
        }
예제 #5
0
        public override async Task OnExceptionAsync(ExceptionContext context)
        {
            var errorMessage = context.Exception.Message;

            var(statusCode, errorCode) = (HttpStatusCode.InternalServerError, ErrorCodes.ServerError);

            (statusCode, errorCode) = context.Exception switch
            {
                ServerException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                CrudException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                ResetPasswordException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                TokenException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                ProfileUpdateException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                ChangePasswordException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                UploadFileException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                DeleteFileException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                AdminActionException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),
                HubConnectionException _ => (HttpStatusCode.InternalServerError, GetErrorCode(context.Exception)),

                AuthException _ => (HttpStatusCode.Unauthorized, GetErrorCode(context.Exception)),
                InvalidCredentialsException _ => (HttpStatusCode.Unauthorized, GetErrorCode(context.Exception)),
                AccountNotConfirmedException _ => (HttpStatusCode.Unauthorized, GetErrorCode(context.Exception)),

                EntityNotFoundException _ => (HttpStatusCode.NotFound, GetErrorCode(context.Exception)),

                ServiceException _ => (HttpStatusCode.ServiceUnavailable, GetErrorCode(context.Exception)),

                NoPermissionsException _ => (HttpStatusCode.Forbidden, GetErrorCode(context.Exception)),
                AccountBlockedException _ => (HttpStatusCode.Forbidden, GetErrorCode(context.Exception)),

                DuplicateException _ => (HttpStatusCode.Conflict, GetErrorCode(context.Exception)),

                OldPasswordInvalidException _ => (HttpStatusCode.BadRequest, GetErrorCode(context.Exception)),

                _ => (HttpStatusCode.InternalServerError, ErrorCodes.ServerError)
            };

            var jsonResponse = (new BaseResponse(Error.Build(errorCode, errorMessage, statusCode: statusCode))).ToJSON();

            context.HttpContext.Response.ContentType   = "application/json";
            context.HttpContext.Response.StatusCode    = (int)statusCode;
            context.HttpContext.Response.ContentLength = Encoding.UTF8.GetBytes(jsonResponse).Length;

            context.HttpContext.Response.AddApplicationError(errorMessage);

            await context.HttpContext.Response.WriteAsync(jsonResponse);

            var logger = context.HttpContext.RequestServices.GetService <INLogger>();
            logger.Error(errorMessage, context.Exception);

            await base.OnExceptionAsync(context);
        }
        public override void OnException(ExceptionContext context)
        {
            string        message       = context.Exception.Message;
            AuthException authException = context.Exception as AuthException;

            if (authException == null)
            {
                LogUtils.LogError(context.Exception, "GlobaExceptionFilterAttribute", message);
            }
            ApiResult apiResult = ApiResult.Error(HttpStatusCode.ERROR, message);

            context.Result           = new JsonResult(apiResult);
            context.ExceptionHandled = true;
        }
예제 #7
0
        public static void GoToErrorPageForAuthErrorType(UIViewController parent, AuthErrorType authErrorType)
        {
            AuthException authException = new AuthException(authErrorType.ToString());

            switch (authErrorType)
            {
            case AuthErrorType.MaxTriesExceeded:
                GoToManyTriesError(parent, LogSeverity.WARNING, authException, "Max number of tries was exceeded");
                break;

            case AuthErrorType.NotInfected:
                GoToNotInfectedError(parent, LogSeverity.WARNING, authException, "User is not infected");
                break;

            case AuthErrorType.Unknown:
                GoToTechnicalError(parent, LogSeverity.WARNING, authException, "User sees Technical error page after ID Porten login: Unknown auth error or user press backbtn");
                break;
            }
        }
            private static Task HandleExceptionAsync(HttpContext context, Exception ex)
            {
                var code = HttpStatusCode.InternalServerError;

                string msg = null;

                if (ex is ValidationException)
                {
                    code = HttpStatusCode.Forbidden;

                    ValidationException vex = (ValidationException)ex;
                    msg = vex.ClientMessage;
                }

                else if (ex is AuthException)
                {
                    code = HttpStatusCode.Unauthorized;
                    AuthException bex = (AuthException)ex;
                    msg = bex.ClientMessage;
                }

                else if (ex is BusinessException)
                {
                    code = HttpStatusCode.Forbidden;
                    BusinessException bex = (BusinessException)ex;
                    msg = bex.ClientMessage;
                }
                else
                {
                    code = HttpStatusCode.InternalServerError;
                    msg  = "Internal Server Error. Please call customer support.";
                }

                context.Response.ContentType = "application/json";
                context.Response.StatusCode  = (int)code;

                var result = JsonConvert.SerializeObject(new ServerResponse <string> {
                    Result = msg
                });

                return(context.Response.WriteAsync(result));
            }
예제 #9
0
        private TcpMessage HandleDeleteItem(TcpConnectedHost host, TcpMessage message)
        {
            if (!message.HasType(TcpRequestType.DeleteItem))
            {
                return(null);
            }

            if (!IsAuthenticated(host))
            {
                throw AuthException.NotLoggedIn();
            }

            var request = new ObjectTcpMessage <DbItem>(message);

            var item = request.Item;

            if (item != null)
            {
                _dataService.Delete(item);
            }

            return(new TcpMessage(TcpRequestType.Ok));
        }
예제 #10
0
 public virtual Task OnUnauthorizedAsync(AuthException exception)
 {
     throw exception;
 }
 public AuthenticationException(AuthException ex) : base(ex)
 {
 }
예제 #12
0
 public override Task OnUnauthorizedAsync(AuthException ex)
 {
     return(base.OnUnauthorizedAsync(ex));
 }
예제 #13
0
 /// <summary>
 /// Throw an AuthenticationException with an AuthException code
 /// </summary>
 /// <param name="code">AuthException.CODE</param>
 /// <returns></returns>
 private AuthenticationException ThrowWithCode(AuthException code) => throw new AuthenticationException(code);