Ejemplo n.º 1
0
        public void TokenIsValid()
        {
            var          ticks  = SystemTime.UtcNow().Ticks;
            const string secKey = "DF25BCB5-35E3-41E4-980F-64D916D806FF";
            const string appId  = "87E9DAAB-2EA4-4A99-92BA-6C9DDB0F868C";
            TokenObject  token  = TokenObject.Create(
                TokenObject.Token(appId, ticks, secKey),
                appId,
                ticks);

            Assert.IsTrue(token.IsValid(secKey));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="publicKey"></param>
        /// <param name="ticks"></param>
        /// <param name="secretKey"></param>
        /// <returns></returns>
        public static CredentialData CreateToken(string publicKey, Int64 ticks, string secretKey)
        {
            var credential = new CredentialData
            {
                ClientType     = ClientType.Node.ToName(),
                CredentialType = CredentialType.Token.ToName(),
                ClientId       = publicKey,
                Ticks          = ticks,
                UserName       = "******"
            };

            credential.Password = TokenObject.Token(publicKey, ticks, secretKey);

            return(credential);
        }
Ejemplo n.º 3
0
        public void NotAuthorized()
        {
            var xm   = NewXM();
            var keys = new string[] {
                "XM", "ZZJGM"
            };
            var values = new string[] {
                xm, "11011421004"
            };
            var infoValue = new KeyValueBuilder(keys, values).ToArray();
            var client    = new JsonServiceClient(AcDomain.NodeHost.Nodes.ThisNode.Node.AnycmdApiAddress);
            var ticks     = DateTime.UtcNow.Ticks;
            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),
                Credential  = new CredentialData
                {
                    ClientType     = ClientType.Node.ToName(),
                    CredentialType = CredentialType.Token.ToName(),
                    ClientId       = "41e711c6-f215-4606-a0bf-9af11cce1d54",
                    Ticks          = ticks,
                    Password       = TokenObject.Token("41e711c6-f215-4606-a0bf-9af11cce1d54", ticks, "invalidSecretKey")
                },
                TimeStamp = DateTime.UtcNow.Ticks
            };
            var response = client.Get(request);

            Assert.IsTrue(Status.NotAuthorized.ToName() == response.Body.Event.ReasonPhrase, response.Body.Event.Description);
            request.Verb = "Get";
            request.JspxSignature();// 签名
            var result = request.RequestNode(AcDomain.NodeHost.Nodes.CenterNode);

            Assert.IsTrue((int)Status.NotExist == result.Body.Event.Status, result.Body.Event.Description);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="cmdDto"></param>
 /// <param name="toNode"></param>
 /// <returns></returns>
 public static IMessageDto RequestNode(this IMessageDto cmdDto, NodeDescriptor toNode)
 {
     if (cmdDto == null)
     {
         throw new ArgumentNullException("cmdDto");
     }
     if (toNode == null)
     {
         throw new ArgumentNullException("toNode");
     }
     if (cmdDto.Credential == null)
     {
         throw new GeneralException("非法状态的命令,没有设置证书");
     }
     if (string.IsNullOrEmpty(cmdDto.Credential.Password))
     {
         CredentialType credentialType;
         if (!cmdDto.Credential.CredentialType.TryParse(out credentialType))
         {
             throw new GeneralException("意外的证书类型" + cmdDto.Credential.CredentialType);
         }
         ClientType clientType;
         if (!cmdDto.Credential.ClientType.TryParse(out clientType))
         {
             throw new GeneralException("意外的客户端类型" + cmdDto.Credential.ClientType);
         }
         switch (clientType)
         {
             case ClientType.Undefined:
                 break;
             case ClientType.Node:
                 NodeDescriptor clientNode;
                 if (!toNode.AcDomain.NodeHost.Nodes.TryGetNodeByPublicKey(cmdDto.Credential.ClientId, out clientNode))
                 {
                     throw new GeneralException("意外的客户节点标识" + cmdDto.Credential.ClientId);
                 }
                 switch (credentialType)
                 {
                     case CredentialType.Undefined:
                         break;
                     case CredentialType.Token:
                         cmdDto.Credential.Password = TokenObject.Token(cmdDto.Credential.ClientId, cmdDto.Credential.Ticks, clientNode.Node.SecretKey);
                         break;
                     case CredentialType.Signature:
                         cmdDto.Credential.Password = Signature.Sign(cmdDto.ToOrignalString(cmdDto.Credential), clientNode.Node.SecretKey);
                         break;
                     case CredentialType.OAuth:
                         break;
                     default:
                         break;
                 }
                 break;
             case ClientType.App:
                 break;
             case ClientType.Monitor:
                 break;
             default:
                 break;
         }
     }
     return cmdDto.ToAnyCommand(toNode).Request();
 }
Ejemplo n.º 5
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));
        }