Beispiel #1
0
        public void TestSessionReq()
        {
            // formatLogin: {a: "login", logid: logId, pswd: tokenB64, iv: ivB64};
            AnsonMsg reqv11 = AnSessionReq.formatLogin(uid, tk64, iv64);

            MemoryStream stream = new MemoryStream();

            reqv11.ToBlock(stream);
            string json = Utils.ToString(stream);

            // json:
            // {type: io.odysz.anson.jprotocol.AnsonMsg,
            //  code: null, opts: null,
            //  port: "session",
            //  header: null, vestion: "1.0",
            //  body: [{type: io.odysz.anson.jprotocol.AnSessionReq,
            //          uid: "test-id",
            //          parent: "io.odysz.anson.jprotocol.AnsonMsg",
            //          a: "login", conn: null,
            //          iv: "iv: I'm base64", mds: null,
            //          token: "tk: I'm base64"}], seq: 909}
            AnsonMsg msg = (AnsonMsg)Anson.FromJson(json);

            Assert.AreEqual(reqv11.code, msg.code);
            Assert.AreEqual(reqv11.port.name, msg.port.name);
            Assert.AreEqual(((AnSessionReq)reqv11.Body(0)).iv, ((AnSessionReq)msg.Body(0)).iv);
            Assert.AreEqual(((AnSessionReq)reqv11.Body(0)).token, ((AnSessionReq)msg.Body(0)).token);
        }
Beispiel #2
0
        /// <summary>
        /// Login and return a client instance (with session managed by jserv).
        /// </summary>
        /// <param name="uid"></param>
        /// <paramref name="pswdPlain">password in plain</param>
        /// <param name="device"></param>
        /// <param name="onlogin"></param>
        /// <param name="err"></param>
        /// <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>
        /// <return>null if failed, a SessionClient instance if login succeed.</return>
        public static async Task <SessionClient> Login(string uid, string pswdPlain, string device,
                                                       OnLogin onlogin, OnError err = 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);
            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, device);

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

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

            MsgCode code = msg.code;

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

                if (AnClient.console)
                {
                    Console.WriteLine(msg.ToString());
                }
            }
            else if (err != null)
            {
                err.err(new MsgCode(code.code), ((AnsonResp)msg.Body(0)).Msg());
            }
            else
            {
                throw new SemanticException(
                          "loging failed\ncode: {0}\nerror: {1}",
                          code, ((AnsonResp)msg.Body()[0]).Msg());
            }
            return(inst[0]);
        }