public string Serialize() { if (string.IsNullOrEmpty(qry)) { throw new InvalidOperationException("qry parameter required in a response"); } var rv = GetType() .GetProperties() .Select(x => (key: x.Name, value: x.GetValue(this)?.ToString())) .Where(x => !string.IsNullOrEmpty(x.value)) .Select(x => $"{x.key}={x.value}\r\n") .Aggregate("", (r, v) => r + v); return(SQRL.ToBase64URL(rv.UTF8Bytes())); }
public async Task InvokeAsync(HttpContext ctx) { var options = ctx.RequestServices.GetService <SQRLOptions>() ?? new SQRLOptions(); if (ctx.Request.Path != options.LoginPath || ctx.Request.Method != HttpMethods.Post || !ctx.Request.IsHttps) { await _next(ctx); return; } //todo: validate nut var cache = ctx.RequestServices.GetService <IMemoryCache>(); var userRepo = ctx.RequestServices.GetService <IUserRepo>(); var auth = new SQRLVM { Client = ctx.Request.Form["client"], Ids = ctx.Request.Form["ids"], Server = ctx.Request.Form["server"] }; var req = SQRL.DecodeRequest(ctx.Request.Host.Value, RequestIP(), auth); var res = SQRL.ComoseResponse(req, userRepo.Get, userRepo.Update, (key, user) => { cache.Set(key, user); }); if (req.cmd != "query") { res.url = options.CPSPath(ctx, res.nut); } var rv = res.Serialize(); ctx.Response.StatusCode = 200; ctx.Response.ContentType = "application/x-www-form-urlencoded"; ctx.Response.ContentLength = rv.Length; await ctx.Response.WriteAsync(rv); string RequestIP() => ctx.Request.IsHttps ? ctx.Request.Host.Host == "localhost" ? "127.0.0.1" : ctx.Request.Host.Host : "0.0.0.0"; }