public IActionResult SetGlobal( string?sendthread, string?administrators, string?proxy, string?proxyenable, string?githubClientID, string?githubClientSecret, string?githubEnable, string?barkKeyId, string?barkTeamId, string?barkPrivateKey) { SendCacheStore.SetSystemValue("sendthread", sendthread); SendCacheStore.SetSystemValue("administrators", administrators); SendCacheStore.SetSystemValue("proxy", proxy); SendCacheStore.SetSystemValue("proxyenable", proxyenable); SendCacheStore.SetSystemValue("githubClientID", githubClientID); SendCacheStore.SetSystemValue("githubClientSecret", githubClientSecret); SendCacheStore.SetSystemValue("githubEnable", githubEnable); SendCacheStore.SetSystemValue("barkKeyId", barkKeyId); SendCacheStore.SetSystemValue("barkTeamId", barkTeamId); SendCacheStore.SetSystemValue("barkPrivateKey", barkPrivateKey); SendTaskManager.Instance.Stop(); SendTaskManager.Instance.Run(); return(OK()); }
private string CreateAcessToken() { var key = Auth.Corpid + Auth.AgentID + Auth.Corpsecret; var toekn = SendCacheStore.Get(key); if (toekn == null) { var url = string.Format(@"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}", Auth.Corpid, Auth.Corpsecret); WebRequest request = WebRequest.Create(url); request.Credentials = CredentialCache.DefaultCredentials; using WebResponse response = request.GetResponse(); using Stream streamResponse = response.GetResponseStream(); StreamReader reader = new StreamReader(streamResponse); string responseFromServer = reader.ReadToEnd(); if (!string.IsNullOrEmpty(responseFromServer)) { if (JsonConvert.DeserializeObject(responseFromServer) is JObject res) { if (res.TryGetValue("access_token", out JToken? jtoken)) { toekn = jtoken.ToString(); } } } reader.Close(); } if (toekn != null) { SendCacheStore.Set(key, toekn, DateTimeOffset.Now.AddHours(2)); } return(toekn); }
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddMemoryCache(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ClockSkew = TimeSpan.FromSeconds(DBManager.Instance.JWT.ClockSkew), ValidAudience = DBManager.Instance.JWT.Audience, ValidIssuer = DBManager.Instance.JWT.Issuer, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(DBManager.Instance.JWT.IssuerSigningKey)) }; options.Events = new JwtBearerEvents { OnAuthenticationFailed = context => { var payload = JsonConvert.SerializeObject(new { message = "ÈÏ֤ʧ°Ü", code = 403 }); context.Response.ContentType = "application/json"; context.Response.StatusCode = StatusCodes.Status200OK; context.Response.WriteAsync(payload); return(Task.FromResult(1)); }, OnForbidden = context => { var payload = JsonConvert.SerializeObject(new { message = "δ¾ÊÚȨ", code = 405 }); context.Response.ContentType = "application/json"; context.Response.StatusCode = StatusCodes.Status200OK; context.Response.WriteAsync(payload); return(Task.FromResult(1)); } }; }); services.AddAuthorization(options => { options.AddPolicy(Policys.Users, policy => policy.RequireRole(Roles.User).Build()); options.AddPolicy(Policys.Systems, policy => policy.RequireRole(Roles.System).Build()); options.AddPolicy(Policys.SystemOrUsers, policy => policy.RequireRole(Roles.User, Roles.System).Build()); options.AddPolicy(Policys.All, policy => policy.RequireRole(Roles.User, Roles.System).Build()); }); services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddGitHubLogin(p => { p.ClientId = SendCacheStore.GetSystemValue("githubClientID"); p.ClientSecret = SendCacheStore.GetSystemValue("githubClientSecret"); }); services.AddControllersWithViews().AddJsonOptions(options => { options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); }); }
public GitHubLogin(IHttpContextAccessor contextAccessor) : base( contextAccessor) { Credential = new CredentialSetting() { ClientId = SendCacheStore.GetSystemValue("githubClientID"), ClientSecret = SendCacheStore.GetSystemValue("githubClientSecret") }; _authorizeUrl = "https://github.com/login/oauth/authorize?client_id=" + Credential.ClientId; }
public IActionResult GetGlobal() { var proxyenable = SendCacheStore.GetSystemValue("proxyenable"); var githubEnable = SendCacheStore.GetSystemValue("githubEnable"); return(OK(new { sendthread = SendCacheStore.GetSystemValue("sendthread"), proxy = SendCacheStore.GetSystemValue("proxy"), proxyenable = proxyenable != "" && bool.Parse(proxyenable), administrators = SendCacheStore.GetSystemValue("administrators"), githubClientID = SendCacheStore.GetSystemValue("githubClientID"), githubClientSecret = SendCacheStore.GetSystemValue("githubClientSecret"), githubEnable = githubEnable != "" && bool.Parse(githubEnable), })); }
private string GetRole(string userName) { string role = "user"; var admins = SendCacheStore.GetSystemValue("administrators"); if (admins != null) { var adminNames = admins.Split(","); if (adminNames.Contains(userName)) { role = "system"; } } return(role); }
protected HttpClient GetHttpClientProxy() { if (SendCacheStore.GetSystemValue("proxyenable") == "true") { var proxyurl = SendCacheStore.GetSystemValue("proxy"); if (proxyurl != null) { WebProxy proxy = new WebProxy { Address = new Uri(proxyurl) }; HttpClientHandler handler = new HttpClientHandler { Proxy = proxy }; HttpClient httpClient = new HttpClient(handler); return(httpClient); } } return(new HttpClient()); }
public override bool SendMessage(SendMessage message) { if (apnSender == null) { var keyID = SendCacheStore.GetSystemValue("barkKeyId"); var teamID = SendCacheStore.GetSystemValue("barkTeamId"); var privateKey = SendCacheStore.GetSystemValue("barkPrivateKey"); var privateKeyContent = privateKey.Split('\n')[1]; var apnSettings = new ApnSettings() { TeamId = teamID, AppBundleIdentifier = "me.fin.bark", P8PrivateKey = privateKeyContent, ServerType = ApnServerType.Production, P8PrivateKeyId = keyID, }; apnSender = new ApnSender(apnSettings, new HttpClient()); } var payload = new AppleNotification( Guid.NewGuid(), message.Data, message.Title) { IsArchive = Auth.IsArchive, AutoMaticallyCopy = Auth.AutoMaticallyCopy, }; payload.Aps.Sound = Auth.Sound; var response = apnSender.Send(payload, Auth.DeviceToken); if (response.IsSuccess) { return(true); } return(false); }
public JsonResult GitHubLogin(string?code) { if (SendCacheStore.GetSystemValue("githubEnable") != "true") { return(Fail(401, "未启用GITHUB登陆")); } if (UserName != null && Token != null) { var direct = string.Format("/#login?token={0}", Token); HttpContext.Response.Redirect(direct, true); return(OK()); } else { if (string.IsNullOrEmpty(code)) { return(OK(m_gitHubLogin.GetOauthUrl())); } else { var res = m_gitHubLogin.Authorize(); if (res != null && res.Result != null && res.Code == Code.Success) { string?githubUserName = null; string?avtar = null; string email = ""; if (res.Result.TryGetValue("login", out JToken? jToken)) { githubUserName = jToken.ToString(); } if (res.Result.TryGetValue("avatar_url", out jToken)) { avtar = jToken.ToString(); } if (res.Result.TryGetValue("email", out jToken)) { email = jToken.ToString(); } if (githubUserName != null && avtar != null) { SendUserInfo user; if (DBManager.Instance.IsUser(githubUserName)) { user = DBManager.Instance.DBase.Query <SendUserInfo>().FirstOrDefault(e => e.UserName == githubUserName); user.Avatar = avtar; DBManager.Instance.DBase.Update(user); if (!user.Active) { return(Fail(401, "用户被禁用")); } } else { user = new SendUserInfo() { UserName = githubUserName, Avatar = avtar, Password = "******".ToMd5(), Email = email, Active = true, Token = Guid.NewGuid().ToString("N").ToUpper(), CreateTime = DateTime.Now }; DBManager.Instance.DBase.Insert(user); } var token = GenToken(user.UserName); var direct = string.Format("/#login?token={0}", token); HttpContext.Response.Redirect(direct, true); return(OK(user)); } } } } return(Fail(401, "Github登陆失败")); }
public JsonResult GithubEnable() { return(OK(SendCacheStore.GetSystemValue("githubEnable") == "true")); }