private bool CheckIfCodeIsValid(string clientPublicId, string scope, string codeValue, IContext context, out string userName) { bool IsValid(Code code) { if (code == null) { return(false); } if (!code.IsValid) { return(false); } if (code.ExpirationTimeStamp < new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()) { return(false); } if (!String.IsNullOrWhiteSpace(code.Scope) && !String.IsNullOrWhiteSpace(scope)) { IList <string> scopes = scope.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(c => c.ToUpper()).ToList(); IList <string> codeScopes = code.Scope.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(c => c.ToUpper()).ToList(); foreach (var s in scopes) { if (!codeScopes.Contains(s)) { return(false); } } } return(code.UserClient.Client.PublicId.Equals(clientPublicId, StringComparison.Ordinal)); } userName = String.Empty; var toReturn = true; var codeRepo = RepositoriesFactory.GetCodeRepository(context); var myCode = codeRepo.GetByCode(codeValue); if (myCode == null) { return(false); } if (!IsValid(myCode)) { toReturn = false; } else { myCode.IsValid = false; codeRepo.Update(myCode); } userName = myCode.UserClient.User.UserName; return(toReturn); }
private string GenerateAndSaveCode(string clientPublicId, string userName, string scope) { var codeValue = RandomService.GenerateRandomString(CodeLenght); using (var context = RepositoriesFactory.CreateContext()) { var userClientRepo = RepositoriesFactory.GetUserClientRepository(context); var codeRepo = RepositoriesFactory.GetCodeRepository(context); var uc = userClientRepo.GetUserClientByClientPublicIdAndUserName(clientPublicId, userName); codeRepo.Add(new Domain.Code() { CodeValue = codeValue, ExpirationTimeStamp = new DateTimeOffset(DateTime.Now.AddSeconds(Configuration.CodeDurationInSeconds)).ToUnixTimeSeconds(), IsValid = true, Scope = scope, UserClientId = uc.Id }); context.Commit(); } return(codeValue); }