Exemplo n.º 1
0
        public void Entity_Event_InvalidStateCode()
        {
            var xm        = System.Guid.NewGuid().ToString();
            var infoValue = new KeyValueBuilder(new Dictionary <string, string> {
                { "XM", xm },
                { "ZZJGM", "11011421005" }
            }).ToArray();
            var request = new Message
            {
                MessageId   = System.Guid.NewGuid().ToString(),
                MessageType = "event",
                IsDumb      = true,
                Verb        = "Create",
                Ontology    = "JS",
                TimeStamp   = DateTime.UtcNow.Ticks,
                Version     = "v1",
                Body        = new BodyData(infoValue, infoValue)
                {
                    Event = new EventData
                    {
                        Subject    = "StateCodeChanged",
                        SourceType = "entity"
                    }
                },
            }.JspxToken();
            //var response = request.RequestNode(acDomain.NodeHost.Nodes.CenterNode);
            var response = AnyMessage.Create(HecpRequest.Create(AcDomain, request), AcDomain.NodeHost.Nodes.CenterNode).Response();

            Assert.IsTrue((int)Status.InvalidStatus == response.Body.Event.Status, response.Body.Event.Description);
            request.IsDumb = false;
            response       = request.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);
            Assert.IsTrue((int)Status.InvalidStatus == response.Body.Event.Status, response.Body.Event.Description);
        }
Exemplo n.º 2
0
        public void UpdateLoginName()
        {
            var localEntityId = Guid.NewGuid().ToString();
            var xm            = NewXM();
            var infoId        = new KeyValue[] {
                new KeyValue("ZZJGM", "11010621022"),
                new KeyValue("XM", xm)
            };
            var infoValue = infoId;
            var request   = new Message()
            {
                Version     = ApiVersion.V1.ToName(),
                IsDumb      = false,
                MessageType = MessageType.Action.ToName(),
                MessageId   = Guid.NewGuid().ToString(),
                Verb        = "create",
                Ontology    = "JS",
                TimeStamp   = DateTime.UtcNow.Ticks,
                Body        = new BodyData(infoValue, infoValue),
            }.UiaSignature();
            var response = AnyMessage.Create(HecpRequest.Create(AcDomain, request), AcDomain.NodeHost.Nodes.ThisNode).Response();

            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
            request.Body.InfoId    = response.Body.InfoValue;
            request.Verb           = "update";
            request.Body.InfoValue = new KeyValue[] { new KeyValue("LoginName", DateTime.Now.Ticks.ToString()) };
            response = AnyMessage.Create(HecpRequest.Create(AcDomain, request), AcDomain.NodeHost.Nodes.ThisNode).Response();
            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
            request.Verb = "delete";
            response     = AnyMessage.Create(HecpRequest.Create(AcDomain, request), AcDomain.NodeHost.Nodes.ThisNode).Response();
            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
        }
Exemplo n.º 3
0
        public void Command_Event_NotExist()
        {
            var xm        = System.Guid.NewGuid().ToString();
            var infoValue = new KeyValueBuilder(new Dictionary <string, string> {
                { "XM", xm },
                { "ZZJGM", "11011421005" }
            }).ToArray();
            var dto = new Message
            {
                MessageId   = System.Guid.NewGuid().ToString(),
                MessageType = "event",
                Verb        = "Create",
                Ontology    = "JS",
                IsDumb      = true,
                TimeStamp   = DateTime.UtcNow.Ticks,
                Version     = "v1",
                Body        = new BodyData(infoValue, infoValue)
                {
                    Event = new EventData
                    {
                        Status       = (int)Status.AuditApproved,
                        Subject      = "StateCodeChanged",
                        ReasonPhrase = Status.AuditApproved.ToName(),
                        SourceType   = EventSourceType.Command.ToName()
                    }
                },
            }.JspxToken();
            // var response = dto.RequestNode(acDomain.NodeHost.Nodes.CenterNode);
            var response = AnyMessage.Create(HecpRequest.Create(AcDomain, dto), AcDomain.NodeHost.Nodes.ThisNode).Response();

            Assert.IsTrue((int)Status.NotExist == response.Body.Event.Status, response.Body.Event.Description);
            dto.IsDumb = false;
            response   = dto.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);
            Assert.IsTrue((int)Status.NotExist == response.Body.Event.Status, response.Body.Event.Description);
        }
Exemplo n.º 4
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="cmdDto"></param>
 /// <param name="responseNode"></param>
 /// <returns></returns>
 public static AnyMessage ToAnyCommand(this IMessageDto cmdDto, NodeDescriptor responseNode)
 {
     if (cmdDto == null)
     {
         throw new ArgumentNullException("cmdDto");
     }
     if (responseNode == null)
     {
         throw new ArgumentNullException("responseNode");
     }
     return AnyMessage.Create(HecpRequest.Create(responseNode.AcDomain, cmdDto), responseNode);
 }
Exemplo n.º 5
0
        public void Command_Action_Update()
        {
            // 首先使用一个Create型命令准备测试数据。然后测试Command和Action,最后打扫现场删除测试数据。
            var xm        = NewXM();
            var infoValue = new KeyValueBuilder(new Dictionary <string, string> {
                { "XM", xm },
                { "ZZJGM", "11011421004" }
            }).ToArray();
            var request = new Message
            {
                MessageId   = System.Guid.NewGuid().ToString(),
                Version     = "v1",
                MessageType = "action",
                IsDumb      = false,
                Verb        = "Create",
                Ontology    = "JS",
                Body        = new BodyData(infoValue, infoValue),
                TimeStamp   = DateTime.UtcNow.Ticks
            }.JspxToken();
            var response = request.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);

            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
            // 从配置文件中读取或者从数据库表的列名读取
            KeyValue[] infoId = new KeyValueBuilder(new Dictionary <string, string> {
                { "XBM", (DateTime.Now.Ticks % 3).ToString() }
            }).ToArray();
            request.MessageId   = Guid.NewGuid().ToString();
            request.MessageType = "Command";
            request.Body        = new BodyData(infoValue, new KeyValueBuilder(request.Body.InfoValue).Append("XBM", "1").ToArray());
            request.TimeStamp   = DateTime.UtcNow.Ticks;
            request.Verb        = "Update";
            request.IsDumb      = true;
            //response = request.RequestNode(acDomain.NodeHost.Nodes.CenterNode);
            response = AnyMessage.Create(HecpRequest.Create(AcDomain, request), AcDomain.NodeHost.Nodes.CenterNode).Response();
            Assert.IsTrue((int)Status.ReceiveOk == response.Body.Event.Status, response.Body.Event.Description);
            request.MessageType = "action";
            response            = request.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);
            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);

            request.MessageType = "command";
            request.IsDumb      = false;
            response            = request.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);
            Assert.IsTrue((int)Status.ReceiveOk == response.Body.Event.Status, response.Body.Event.Description);
            request.MessageType = "action";
            response            = request.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);
            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
            request.Verb = "delete";
            response     = request.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);
            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
        }
Exemplo n.º 6
0
        public void Permission()
        {
            var infoId = new KeyValueBuilder(new Dictionary <string, string> {
                { "SFZJH", "320113198108242027" },
                { "SFZJLXM", "1" },
                { "GHHM", "85012345" }
            }).ToArray();
            var cmdDto = new Message
            {
                MessageId   = System.Guid.NewGuid().ToString(),
                Version     = "v1",
                Verb        = "Head",
                MessageType = "action",
                IsDumb      = true,
                Ontology    = "JS",
                Body        = new BodyData(infoId, null),
                TimeStamp   = DateTime.UtcNow.Ticks
            }.JspxToken();
            var response = cmdDto.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);

            //var response = AnyMessage.Create(HecpRequest.Create(request, Credential.Create(request))).Response();
            Assert.IsTrue((int)Status.NoPermission == response.Body.Event.Status, response.Body.Event.Description);
            cmdDto.IsDumb = false;
            //response = request.RequestNode(acDomain.NodeHost.Nodes.CenterNode);
            // 使用下面这行可以绕过网络传输从而易于调试,而上面那行需要网络传输
            response = AnyMessage.Create(HecpRequest.Create(AcDomain, cmdDto), AcDomain.NodeHost.Nodes.ThisNode).Response();
            Assert.IsTrue((int)Status.NoPermission == response.Body.Event.Status, response.Body.Event.Description);

            cmdDto.MessageType = "Command";
            response           = cmdDto.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);
            //var response = AnyMessage.Create(HecpRequest.Create(request, Credential.Create(request))).Response();
            Assert.IsTrue((int)Status.NoPermission == response.Body.Event.Status, response.Body.Event.Description);
            cmdDto.IsDumb = false;
            //response = request.RequestNode(acDomain.NodeHost.Nodes.CenterNode);
            // 使用下面这行可以绕过网络传输从而易于调试,而上面那行需要网络传输
            response = AnyMessage.Create(HecpRequest.Create(AcDomain, cmdDto), AcDomain.NodeHost.Nodes.ThisNode).Response();
            Assert.IsTrue((int)Status.NoPermission == response.Body.Event.Status, response.Body.Event.Description);
        }
Exemplo n.º 7
0
        public void Action_Create()
        {
            var xm        = NewXM();
            var infoValue = new KeyValueBuilder(new Dictionary <string, string> {
                { "XM", xm },
                { "ZZJGM", "11011421004" }
            }).ToArray();
            var request = new Message
            {
                MessageId   = System.Guid.NewGuid().ToString(),
                Version     = "v1",
                MessageType = "action",
                IsDumb      = true,
                Verb        = Verb.Create.Code,
                Ontology    = "JS",
                Body        = new BodyData(infoValue, infoValue)
                {
                    QueryList = new string[] { "Id" }
                },
                TimeStamp = DateTime.UtcNow.Ticks
            }.JspxToken();
            //var response = request.RequestNode(acDomain.NodeHost.Nodes.CenterNode);
            var response = AnyMessage.Create(HecpRequest.Create(AcDomain, request), AcDomain.NodeHost.Nodes.ThisNode).Response();

            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
            request.JspxSignature();// 签名
            response = request.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);
            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
            request.IsDumb = false;
            request.JspxSignature();// 命令对象有更改则需重新签名
            response = request.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);
            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
            request.Verb = "Delete";
            request.JspxSignature();// 命令对象有更改则需重新签名
            response = request.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);
            Assert.IsTrue((int)Status.ExecuteOk == response.Body.Event.Status, response.Body.Event.Description);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 处理任何命令
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns></returns>
        public Message Any(Message request)
        {
            try
            {
                var context = new HecpContext(acDomain, HecpRequest.Create(acDomain, request));
                acDomain.NodeHost.HecpHandler.Process(context);

                return(context.Response.ToMessage());
            }
            catch (Exception ex)
            {
                acDomain.LoggingService.Error(ex);
                var r = new Message
                {
                    MessageType = MessageType.Event.ToName(),
                    MessageId   = string.Empty
                };
                r.Body.Event.Description  = "服务器内部逻辑异常";
                r.Body.Event.Status       = 500;
                r.Body.Event.ReasonPhrase = Status.InternalServerError.ToString();// 不使用ToName扩展方法以避免造成新的异常

                return(r);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 创建给定的节点的命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="responseNode"></param>
        /// <returns></returns>
        public static AnyMessage Create(HecpRequest request, NodeDescriptor responseNode)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (responseNode == null)
            {
                throw new ArgumentNullException("responseNode");
            }
            var acDomain   = responseNode.AcDomain;
            var clientId   = string.Empty;
            var credential = request.Credential;

            if (credential == null)
            {
                throw new InvalidOperationException();
            }
            switch (credential.ClientType)
            {
            case ClientType.Undefined:
                break;

            case ClientType.Node:
                NodeDescriptor requester;
                if (acDomain.NodeHost.Nodes.TryGetNodeByPublicKey(credential.ClientId, out requester))
                {
                    clientId = requester.Id.ToString();
                }
                break;

            case ClientType.App:
                break;

            case ClientType.Monitor:
                break;

            default:
                break;
            }
            var dataTuple = DataItemsTuple.Create(
                acDomain,
                request.InfoId,
                request.InfoValue, request.QueryList, acDomain.Config.InfoFormat);
            MessageType requestType;

            request.MessageType.TryParse(out requestType);
            return(new AnyMessage(MessageTypeKind.AnyCommand, Guid.NewGuid(), dataTuple, responseNode)
            {
                Version = request.Version,
                IsDumb = request.IsDumb,
                Ontology = request.Ontology,
                ReceivedOn = DateTime.Now,
                CreateOn = DateTime.Now,
                MessageId = request.MessageId,
                ClientId = clientId,
                Verb = request.Verb,
                MessageType = requestType,
                ClientType = credential.ClientType,
                TimeStamp = SystemTime.ParseUtcTicksToLocalTime(request.TimeStamp),
                ReasonPhrase = request.EventReasonPhrase,
                Status = request.EventStatus,
                UserName = request.Credential.UserName,
                EventSourceType = request.EventSourceType,
                EventSubjectCode = request.EventSubject,
                LocalEntityId = null,
                Description = null,
                CatalogCode = null,
                From = request.From,
                RelatesTo = request.RelatesTo,
                To = request.To,
                SessionId = request.SessionId
            });
        }
Exemplo n.º 10
0
        public ActionResult AuditUnapproved(string ontologyCode, string id)
        {
            var response = new ResponseData {
                id = id, success = true
            };
            OntologyDescriptor ontology;

            if (!AcDomain.NodeHost.Ontologies.TryGetOntology(ontologyCode, out ontology))
            {
                throw new ValidationException("非法的本体码");
            }
            string[] ids           = id.Split(',');
            var      localEventIDs = new Guid[ids.Length];

            for (int i = 0; i < ids.Length; i++)
            {
                Guid tmp;
                if (Guid.TryParse(ids[i], out tmp))
                {
                    localEventIDs[i] = tmp;
                }
                else
                {
                    throw new ValidationException("意外的本地事件标识" + ids[i]);
                }
            }
            foreach (var localEventId in localEventIDs)
            {
                MessageEntity evnt = ontology.MessageProvider.GetCommand(MessageTypeKind.LocalEvent, ontology, localEventId);
                if (evnt != null)
                {
                    if (evnt.Status == (int)Status.ToAudit &&
                        evnt.EventSourceType.Equals("Command", StringComparison.OrdinalIgnoreCase))
                    {
                        var node  = AcDomain.NodeHost.Nodes.ThisNode;
                        var ticks = DateTime.UtcNow.Ticks;
                        var cmd   = new Message()
                        {
                            Version     = ApiVersion.V1.ToName(),
                            IsDumb      = false,
                            MessageType = MessageType.Event.ToName(),
                            Credential  = new CredentialData
                            {
                                ClientType     = ClientType.Node.ToName(),
                                UserType       = UserType.None.ToName(),
                                CredentialType = CredentialType.Token.ToName(),
                                ClientId       = node.Node.Id.ToString(),
                                UserName       = evnt.UserName,// UserName
                                Password       = TokenObject.Token(node.Node.Id.ToString(), ticks, node.Node.SecretKey),
                                Ticks          = ticks
                            },
                            TimeStamp = DateTime.UtcNow.Ticks,
                            MessageId = evnt.Id.ToString(),
                            Verb      = evnt.Verb.Code,
                            Ontology  = evnt.Ontology,
                            Body      = new BodyData(new KeyValue[] { new KeyValue("Id", evnt.LocalEntityId) }, evnt.DataTuple.ValueItems.Items.ToDto())
                            {
                                Event = new EventData
                                {
                                    Status       = (int)Status.AuditUnapproved,
                                    ReasonPhrase = Status.AuditUnapproved.ToName(),
                                    SourceType   = evnt.EventSourceType,
                                    Subject      = evnt.EventSubjectCode
                                }
                            }
                        };
                        var result = AnyMessage.Create(HecpRequest.Create(AcDomain, cmd), AcDomain.NodeHost.Nodes.ThisNode).Response();
                        if (result.Body.Event.Status == (int)Status.NotExist)
                        {
                            ontology.MessageProvider.DeleteCommand(MessageTypeKind.LocalEvent, ontology, evnt.Id, evnt.IsDumb);
                        }
                        else
                        {
                            if ((result.Body.Event.Status < 200 || result.Body.Event.Status >= 400) && result.Body.Event.Status != (int)Status.AuditUnapproved)
                            {
                                response.success = false;
                                response.msg     = result.Body.Event.Description;
                                response.Warning();
                            }
                        }
                    }
                }
            }

            return(this.JsonResult(response));
        }
Exemplo n.º 11
0
 /// <summary>
 /// 创建给定的节点的命令
 /// </summary>
 /// <param name="request"></param>
 /// <param name="responseNode"></param>
 /// <returns></returns>
 public static AnyMessage Create(HecpRequest request, NodeDescriptor responseNode)
 {
     if (request == null)
     {
         throw new ArgumentNullException("request");
     }
     if (responseNode == null)
     {
         throw new ArgumentNullException("responseNode");
     }
     var acDomain = responseNode.AcDomain;
     var clientId = string.Empty;
     var credential = request.Credential;
     if (credential == null)
     {
         throw new InvalidOperationException();
     }
     switch (credential.ClientType)
     {
         case ClientType.Undefined:
             break;
         case ClientType.Node:
             NodeDescriptor requester;
             if (acDomain.NodeHost.Nodes.TryGetNodeByPublicKey(credential.ClientId, out requester))
             {
                 clientId = requester.Id.ToString();
             }
             break;
         case ClientType.App:
             break;
         case ClientType.Monitor:
             break;
         default:
             break;
     }
     var dataTuple = DataItemsTuple.Create(
         acDomain,
         request.InfoId,
         request.InfoValue, request.QueryList, acDomain.Config.InfoFormat);
     MessageType requestType;
     request.MessageType.TryParse(out requestType);
     return new AnyMessage(MessageTypeKind.AnyCommand, Guid.NewGuid(), dataTuple, responseNode)
     {
         Version = request.Version,
         IsDumb = request.IsDumb,
         Ontology = request.Ontology,
         ReceivedOn = DateTime.Now,
         CreateOn = DateTime.Now,
         MessageId = request.MessageId,
         ClientId = clientId,
         Verb = request.Verb,
         MessageType = requestType,
         ClientType = credential.ClientType,
         TimeStamp = SystemTime.ParseUtcTicksToLocalTime(request.TimeStamp),
         ReasonPhrase = request.EventReasonPhrase,
         Status = request.EventStatus,
         UserName = request.Credential.UserName,
         EventSourceType = request.EventSourceType,
         EventSubjectCode = request.EventSubject,
         LocalEntityId = null,
         Description = null,
         CatalogCode = null,
         From = request.From,
         RelatesTo = request.RelatesTo,
         To = request.To,
         SessionId = request.SessionId
     };
 }
Exemplo n.º 12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ProcessResult Auth(HecpRequest request)
        {
            if (request == null)
            {
                return(new ProcessResult(false, Status.NoneCommand, "空请求"));
            }
            CredentialObject credential = request.Credential;

            if (credential == null)
            {
                return(new ProcessResult(false, Status.NoneCredential, "未传入证书对象"));
            }
            else if (credential.CredentialType == CredentialType.Undefined)
            {
                return(new ProcessResult(false, Status.InvalidCredentialType, "未定义的证书类型"));
            }
            else if (credential.Ticks < SystemTime.UtcNow().AddYears(-1).Ticks ||
                     credential.Ticks > SystemTime.UtcNow().AddYears(1).Ticks)
            {
                return(new ProcessResult(false, Status.InvalidTicks, "非法的时间戳:" + credential.Ticks));
            }
            var t = new DateTime(credential.Ticks, DateTimeKind.Utc);

            if (t.AddSeconds(request.Host.Config.TicksTimeout) < SystemTime.UtcNow() ||
                t.AddSeconds(-request.Host.Config.TicksTimeout) > SystemTime.UtcNow())
            {
                return(new ProcessResult(false, Status.NotAuthorized, "时间戳超时:" + credential.Ticks));
            }
            else
            {
                switch (credential.ClientType)
                {
                case ClientType.Undefined:
                    return(new ProcessResult(false, Status.InvalidClientType, "非法的客户端类型"));

                case ClientType.Node:
                {
                    // 向后兼容uia的实名认证在使用的token证书类型。如果ClientID为空则从UserName字段提取ClientID
                    string         clientId = credential.ClientId;
                    NodeDescriptor node;
                    if (!request.Host.NodeHost.Nodes.TryGetNodeByPublicKey(clientId, out node))
                    {
                        return(new ProcessResult(false, Status.InvalidClientId, "未知的节点"));
                    }
                    else if (node.Node.IsEnabled != 1)
                    {
                        return(new ProcessResult(false, Status.NodeIsDisabled, "节点已被禁用"));
                    }
                    else if (!node.Node.IsReceiveEnabled)
                    {
                        return(new ProcessResult(false, Status.ReceiveIsDisabled, "来自本节点的请求被禁止接收"));
                    }
                    if (string.IsNullOrEmpty(credential.Password))
                    {
                        return(new ProcessResult(false, Status.NotAuthorized, "签名不能为空"));
                    }
                    switch (credential.CredentialType)
                    {
                    case CredentialType.Undefined:
                        return(new ProcessResult(false, Status.InvalidCredentialType, "未定义的证书类型"));

                    case CredentialType.Token:            // 证书类型是令牌
                        var token = TokenObject.Create(credential.Password, clientId, credential.Ticks);
                        if (!token.IsValid(node.Node.SecretKey))
                        {
                            return(new ProcessResult(false, Status.NotAuthorized, "节点身份未验证通过"));
                        }
                        break;

                    case CredentialType.Signature:            // 证书类型是签名
                        if (credential.SignatureMethod == SignatureMethod.Undefined)
                        {
                            return(new ProcessResult(false, Status.NotAuthorized, "未指定签名算法,签名算法如:" + SignatureMethod.HMAC_SHA1.ToName()));
                        }
                        if (!CredentialObject.Valid(request, node.Node.SecretKey, credential.SignatureMethod))
                        {
                            return(new ProcessResult(false, Status.NotAuthorized, "签名可能被篡改,数据传输对象状态有变化时需重新签名。"));
                        }
                        break;

                    case CredentialType.OAuth:
                        if (credential.SignatureMethod == SignatureMethod.Undefined)
                        {
                            return(new ProcessResult(false, Status.NotAuthorized, "未指定签名算法,签名算法如:" + SignatureMethod.HMAC_SHA1.ToName()));
                        }
                        return(new ProcessResult(false, Status.NotAuthorized, "暂不支持开放授权"));

                    default:
                        return(new ProcessResult(false, Status.NotAuthorized, "暂不支持" + credential.CredentialType.ToName() + "证书类型"));
                    }
                    break;
                }

                case ClientType.App:
                    return(new ProcessResult(false, Status.InvalidClientType, "暂不支持"));

                case ClientType.Monitor:
                    return(new ProcessResult(false, Status.InvalidClientType, "暂不支持"));

                default:
                    break;
                }
            }

            return(new ProcessResult(true, Status.Ok, "身份认证通过"));;
        }
Exemplo n.º 13
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ProcessResult Auth(HecpRequest request)
        {
            if (request == null)
            {
                return new ProcessResult(false, Status.NoneCommand, "空请求");
            }
            CredentialObject credential = request.Credential;
            if (credential == null)
            {
                return new ProcessResult(false, Status.NoneCredential, "未传入证书对象");
            }
            else if (credential.CredentialType == CredentialType.Undefined)
            {
                return new ProcessResult(false, Status.InvalidCredentialType, "未定义的证书类型");
            }
            else if (credential.Ticks < SystemTime.UtcNow().AddYears(-1).Ticks
                || credential.Ticks > SystemTime.UtcNow().AddYears(1).Ticks)
            {
                return new ProcessResult(false, Status.InvalidTicks, "非法的时间戳:" + credential.Ticks);
            }
            var t = new DateTime(credential.Ticks, DateTimeKind.Utc);
            if (t.AddSeconds(request.Host.Config.TicksTimeout) < SystemTime.UtcNow()
                || t.AddSeconds(-request.Host.Config.TicksTimeout) > SystemTime.UtcNow())
            {
                return new ProcessResult(false, Status.NotAuthorized, "时间戳超时:" + credential.Ticks);
            }
            else
            {
                switch (credential.ClientType)
                {
                    case ClientType.Undefined:
                        return new ProcessResult(false, Status.InvalidClientType, "非法的客户端类型");
                    case ClientType.Node:
                        {
                            // 向后兼容uia的实名认证在使用的token证书类型。如果ClientID为空则从UserName字段提取ClientID
                            string clientId = credential.ClientId;
                            NodeDescriptor node;
                            if (!request.Host.NodeHost.Nodes.TryGetNodeByPublicKey(clientId, out node))
                            {
                                return new ProcessResult(false, Status.InvalidClientId, "未知的节点");
                            }
                            else if (node.Node.IsEnabled != 1)
                            {
                                return new ProcessResult(false, Status.NodeIsDisabled, "节点已被禁用");
                            }
                            else if (!node.Node.IsReceiveEnabled)
                            {
                                return new ProcessResult(false, Status.ReceiveIsDisabled, "来自本节点的请求被禁止接收");
                            }
                            if (string.IsNullOrEmpty(credential.Password))
                            {
                                return new ProcessResult(false, Status.NotAuthorized, "签名不能为空");
                            }
                            switch (credential.CredentialType)
                            {
                                case CredentialType.Undefined:
                                    return new ProcessResult(false, Status.InvalidCredentialType, "未定义的证书类型");
                                case CredentialType.Token:// 证书类型是令牌
                                    var token = TokenObject.Create(credential.Password, clientId, credential.Ticks);
                                    if (!token.IsValid(node.Node.SecretKey))
                                    {
                                        return new ProcessResult(false, Status.NotAuthorized, "节点身份未验证通过");
                                    }
                                    break;
                                case CredentialType.Signature:// 证书类型是签名
                                    if (credential.SignatureMethod == SignatureMethod.Undefined)
                                    {
                                        return new ProcessResult(false, Status.NotAuthorized, "未指定签名算法,签名算法如:" + SignatureMethod.HMAC_SHA1.ToName());
                                    }
                                    if (!CredentialObject.Valid(request, node.Node.SecretKey, credential.SignatureMethod))
                                    {
                                        return new ProcessResult(false, Status.NotAuthorized, "签名可能被篡改,数据传输对象状态有变化时需重新签名。");
                                    }
                                    break;
                                case CredentialType.OAuth:
                                    if (credential.SignatureMethod == SignatureMethod.Undefined)
                                    {
                                        return new ProcessResult(false, Status.NotAuthorized, "未指定签名算法,签名算法如:" + SignatureMethod.HMAC_SHA1.ToName());
                                    }
                                    return new ProcessResult(false, Status.NotAuthorized, "暂不支持开放授权");
                                default:
                                    return new ProcessResult(false, Status.NotAuthorized, "暂不支持" + credential.CredentialType.ToName() + "证书类型");
                            }
                            break;
                        }
                    case ClientType.App:
                        return new ProcessResult(false, Status.InvalidClientType, "暂不支持");
                    case ClientType.Monitor:
                        return new ProcessResult(false, Status.InvalidClientType, "暂不支持");
                    default:
                        break;
                }
            }

            return new ProcessResult(true, Status.Ok, "身份认证通过"); ;
        }