public override void Run(IList <string> args) { var store = new MongoMetaStore(); var appMetadata = store.GetAppById(args[0]); if (appMetadata == null) { appMetadata = new AppMetadata { AppId = args[0], Platforms = new Dictionary <string, AppSecret>() } } ; if (string.IsNullOrEmpty(appMetadata.ApiKey)) { appMetadata.ApiKey = GenerateApiKey(); } if (!string.IsNullOrWhiteSpace(_apnsCertFile)) { appMetadata.Platforms["apns"] = new AppSecret { Certificate = File.ReadAllBytes(_apnsCertFile) }; } if (_mpnsCertFile != null) { appMetadata.Platforms["mpns"] = new AppSecret { Certificate = string.IsNullOrWhiteSpace(_mpnsCertFile) ? null : File.ReadAllBytes(_mpnsCertFile) }; } if (!string.IsNullOrWhiteSpace(_gcmKey)) { appMetadata.Platforms["gcm"] = new AppSecret { Key = _gcmKey }; } if (!string.IsNullOrEmpty(_wnsKey)) { var parts = _wnsKey.Split('?'); Console.WriteLine(parts.Length); if (parts.Length != 2 || string.IsNullOrWhiteSpace(parts[0]) || string.IsNullOrWhiteSpace(parts[1])) { throw new ArgumentException("WNS credentials must be in the format SID?secret"); } appMetadata.Platforms["wns"] = new AppSecret { AppId = parts[0], Key = parts[1] }; } store.Save(appMetadata); Console.WriteLine("API Key: " + appMetadata.ApiKey); }
private static bool IsAuthValid(HttpRequestMessage request, out string appId, out string apiKey) { IEnumerable <string> authValues; appId = null; apiKey = null; if (!request.Headers.TryGetValues("Authorization", out authValues)) { return(false); } foreach (var authStr in authValues) { if (string.IsNullOrWhiteSpace(authStr)) { continue; } var partsSpace = authStr.Split(null, 2); if (string.Compare("APIKEY", partsSpace[0], StringComparison.OrdinalIgnoreCase) == 0) { foreach (var pair in partsSpace[1].Split(',').Select(p => p.Trim())) { var partsEquals = pair.Split(new char[] { '=' }, 2).Select(p => p.Trim()).ToArray(); if (partsEquals.Length == 2) { if (string.Compare("appId", partsEquals[0], StringComparison.OrdinalIgnoreCase) == 0) { appId = partsEquals[1].Trim(); } else if (string.Compare("apiKey", partsEquals[0], StringComparison.OrdinalIgnoreCase) == 0) { apiKey = partsEquals[1].Trim().Replace("\"", ""); } } } } } if (string.IsNullOrWhiteSpace(appId) || string.IsNullOrWhiteSpace(apiKey)) { return(false); } var store = new MongoMetaStore(); var appMetadata = store.GetAppById(appId); return(appMetadata != null && appMetadata.ApiKey == apiKey); }