private bool IsValidRequest( HttpRequestMessage req, string appId, string incomingBase64Signature, string nonce, string requestTimeStamp) { var apiKey = ApiKeyStore.GetApiKey(appId); // Check if the app ID provided returned an API key. if (apiKey == null) { return(false); } // Check if the request is a replay. if (IsReplayRequest(nonce, requestTimeStamp)) { return(false); } // Rebuild the base 64 signature. var rebuiltbase64Signature = HMACHelper.BuildBase64Signature( apiKey, appId, req.RequestUri, req.Method, req.Content, nonce, requestTimeStamp ); // Check if the signatures match. return(incomingBase64Signature.Equals(rebuiltbase64Signature, StringComparison.Ordinal)); }
public AuthController(IAuthRepository repo, IConfiguration config, IMapper mapper) { _mapper = mapper; _repo = repo; _config = config; _hmacHelper = new HMACHelper(); }
protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { // Calculate UNIX time. var epochStart = new DateTime(1970, 01, 01, 0, 0, 0, 0, DateTimeKind.Utc); var timeSpan = DateTime.UtcNow - epochStart; var requestTimeStamp = Convert.ToUInt64(timeSpan.TotalSeconds).ToString(); // Create random nonce for each request var nonce = HMACHelper.BuildNonce(); // Build the base 64 signature. var base64Signature = HMACHelper.BuildBase64Signature( _apiKey, _appId, request.RequestUri, request.Method, request.Content, nonce, requestTimeStamp ); // Setting the values in the Authorization header using custom scheme (amx) request.Headers.Authorization = new AuthenticationHeaderValue("amx", $"{_appId}:{base64Signature}:{nonce}:{requestTimeStamp}"); var response = base.SendAsync(request, cancellationToken); return(response); }
public SymantecRequest(Random random, DateTime utcTime) { timestamp = UnixTimestampHelper.ConvertToUnixTimeStamp(utcTime); var default_model = $"MacBookPro{random.Next(1, 12)},{random.Next(1, 4)}"; model = default_model; serial = RandomHelper.GenerateRandomStringOfLength(random, CharacterHelper.AllDigits + CharacterHelper.AllUpperCase, 12); client_id = $"Mac-{RandomHelper.GenerateRandomStringOfLength(random, CharacterHelper.HexCharacters, 16)}"; os = default_model; var data_before_hmac = $"{timestamp}{timestamp}{client_id_type}{client_id}{dist_channel}"; var data_before_hmac_utf8 = StringHelper.ToUtf8(data_before_hmac); var digesthmac = HMACHelper.HashHMACSHA256(HMAC_KEY, data_before_hmac_utf8); var base64hmac = Convert.ToBase64String(digesthmac); data = base64hmac; }
public void SeedUsers() { _context.Users.RemoveRange(_context.Users); _context.SaveChanges(); HMACHelper hMAC = new HMACHelper(); var userData = System.IO.File.ReadAllText("Models/Data/UserSeedData.json"); var users = JsonConvert.DeserializeObject <List <User> >(userData); foreach (var user in users) { byte[] passwordHash, passwordSalt; hMAC.CreatePasswordHash("password", out passwordHash, out passwordSalt); user.PasswordSalt = passwordSalt; user.PasswordHash = passwordHash; user.Name = user.Name.ToLower(); _context.Users.AddRange(user); } _context.SaveChanges(); }
public AuthRepository(DataContext context) { _context = context; _HMACHelper = new HMACHelper(); }