Ejemplo n.º 1
0
        public static SQRLReponse ComoseResponse(SQRLRequest req, Func <string, User> getUser, Action <User> saveUser, Action <string, User> loggedIn)
        {
            var resp = new SQRLReponse
            {
                nut = MakeNut(req.RequestIP),
            };

            if (!req.IsValid)
            {
                resp.tif = SQRLReponse.TIF.command_failed.ToHex();
                return(resp);
            }

            var user = getUser(req.idk);

            resp.tif = (user == null ? SQRLReponse.TIF.ips_match : SQRLReponse.TIF.ips_match | SQRLReponse.TIF.id_match).ToHex();
            resp.qry = "/sqrl/auth?nut=" + resp.nut;

            switch (req.cmd)
            {
            case "query":
                return(resp);

            case "ident":
                if (user == null)
                {
                    user = new User
                    {
                        idk       = req.idk,
                        suk       = req.suk,
                        vuk       = req.vuk,
                        CreatedOn = DateTime.Now,
                    };
                }
                else
                {
                    user.LoginCount++;
                    user.LastLoggedIn = DateTime.Now;
                    resp.suk          = user.suk;
                }
                saveUser(user);
                loggedIn("CPS" + resp.nut, user);

                resp.tif = (SQRLReponse.TIF.id_match | SQRLReponse.TIF.ips_match).ToHex();

                return(resp);

            default:
                throw new NotImplementedException();
            }
        }
Ejemplo n.º 2
0
        public static SQRLRequest DecodeRequest(string host, string ipAddress, SQRLVM vm)
        {
            var dict = Unpack(FromBase64URL(vm.Client).UTF8String());

            var rv = new SQRLRequest
            {
                idk = dict.GetOrDefault("idk"),
                suk = dict.GetOrDefault("suk"),
                vuk = dict.GetOrDefault("vuk"),
                cmd = dict.GetOrDefault("cmd"),

                Signature = FromBase64URL(vm.Ids),
            };

            rv.PublicKey = FromBase64URL(rv.idk);
            rv.IsValid   = ValidateSQRLPost(rv.Signature, vm.Client, vm.Server, rv.PublicKey);
            rv.RequestIP = ipAddress;
            rv.Host      = host;

            return(rv);
        }