Esempio n. 1
0
 public void TestLogin()
 {
     Clients.Login(uid, pswd,
                   (code, resp) =>
     {
         client = new AnsonClient(resp.ssInf);
         Assert.AreEqual(uid, resp.ssInf.uid);
     });
 }
Esempio n. 2
0
        /// <summary>Login and return a client instance (with session managed by jserv).
        /// </summary>
        /// <paramref name="uid"/>
        /// <paramref name="pswdPlain">password in plain</param>
        /// <return>null if failed, a SessionClient instance if login succeed.</return>
        /// <throws>SQLException the request makes server generate wrong SQL.</throws>
        /// <throws>SemanticException Request can not parsed correctly</throws>
        /// <throws>GeneralSecurityException  other error</throws>
        /// <throws>Exception, most likely the network failed</throws>
        public static async Task <AnsonClient> Login(string uid, string pswdPlain, Action <int, AnSessionResp> onlogin = null, Action <int, AnsonResp> onerror = null)
        {
            byte[] iv   = AESHelper.getRandom();
            string iv64 = AESHelper.Encode64(iv);

            if (uid == null || pswdPlain == null)
            {
                throw new SemanticException("user id and password can not be null.");
            }

            string tk64 = AESHelper.Encrypt("-----------" + uid, pswdPlain, iv);

            // formatLogin: {a: "login", logid: logId, pswd: tokenB64, iv: ivB64};
            // AnsonMsg<? extends AnsonBody> reqv11 = new AnsonMsg<AnQueryReq>(Port.session);;
            AnsonMsg reqv11 = AnSessionReq.formatLogin(uid, tk64, iv64);

            string         url        = ServUrl(new Port(Port.session));
            HttpServClient httpClient = new HttpServClient();

            AnsonClient[] inst = new AnsonClient[1];
            AnsonMsg      msg  = await httpClient.Post(url, reqv11);

            MsgCode code = msg.code;

            if (code != null && MsgCode.ok == code.code)
            {
                // create a logged in client
                inst[0] = new AnsonClient(((AnSessionResp)msg.Body()[0]).ssInf);
                if (onlogin != null)
                {
                    onlogin(code.code, (AnSessionResp)msg.Body()[0]);
                }

                if (Clients.console)
                {
                    Console.WriteLine(msg.ToString());
                }
            }
            else if (onerror != null)
            {
                onerror(code.code, (AnsonResp)msg.Body()[0]);
            }
            else
            {
                throw new SemanticException(
                          "loging failed\ncode: {0}\nerror: {1}",
                          code, ((AnsonResp)msg.Body()[0]).Msg());
            }
            return(inst[0]);
        }
Esempio n. 3
0
 public void TestUpload()
 {
     Clients.Login(uid, pswd,
                   (code, resp) =>
     {
         client = new AnsonClient(resp.ssInf);
         Assert.AreEqual(uid, resp.ssInf.uid);
         UploadTransaction(client, "Sun Yet-sen.jpg");
     },
                   (code, resp) =>
     {
         Assert.Fail(string.Format(@"code: {0}\nerror:\n{1}", code, resp.Msg()));
     });
 }
Esempio n. 4
0
        static void UploadTransaction(CancellationTokenSource waker, AnsonClient client, string p)
        {
            // string p = Path.get(filename);
            byte[] f   = File.ReadAllBytes(p);
            string b64 = AESHelper.Encode64(f);

            AnsonMsg    jmsg = client.Update(null, "a_users");
            AnUpdateReq upd  = (AnUpdateReq)jmsg.Body(0);

            upd.Nv("nationId", "CN")
            .WhereEq("userId", "admin")
            // .post(((UpdateReq) new UpdateReq(null, "a_attach")
            .Post(AnUpdateReq.formatDelReq(null, null, "a_attaches")
                  .WhereEq("busiTbl", "a_users")
                  .WhereEq("busiId", "admin")
                  .Post((AnInsertReq.formatInsertReq(null, null, "a_attaches")
                         .Cols("attName", "busiId", "busiTbl", "uri")
                         .Nv("attName", "-Anclient.cs Test")
                         // The parent pk can't be resulved, we must provide the value.
                         // See https://odys-z.github.io/notes/semantics/best-practices.html#fk-ins-cate
                         .Nv("busiId", "admin")
                         .Nv("busiTbl", "a_users")
                         .Nv("uri", b64))));

            jmsg.Header(client.Header());

            client.Console(jmsg);

            client.Commit(jmsg,
                          (code, data) => {
                if (MsgCode.ok == code.code)
                {
                    Utils.Logi(code.ToString());
                }
                else
                {
                    Utils.Warn(data.ToString());
                }
            },
                          onErr: (c, err) => {
                Assert.Fail(string.Format(@"code: {0}, error: {1}", c, err.Msg()));
            },
                          waker);
        }
Esempio n. 5
0
        internal async Task Login(Action <AnsonClient, AnsonResp> onLogin = null)
        {
            bool succeed = false;
            await Clients.Login(uid, pswd,
                                (code, resp) =>
            {
                succeed = true;
                client  = new AnsonClient(resp.ssInf);
                Assert.AreEqual(uid, resp.ssInf.uid);
                Assert.IsNotNull(resp.ssInf.ssid);
                if (onLogin != null)
                {
                    onLogin(client, resp);
                }
            });

            if (!succeed)
            {
                Assert.Fail("onOk not called.");
            }
            Assert.IsNotNull(client);
        }