private async Task CreateFirstAdminUserDocumentAsync() { Console.WriteLine("Creating first administrator user"); Console.Write("Please enter the administrator user email: "); var email = Console.ReadLine(); if (!new EmailAddressAttribute().IsValid(email)) { throw new Exception($"Email '{email}' is invalid."); } var password = RandomGenerator.Generate(16); Console.WriteLine($"Administrator users password is: {password}"); var user = new User { UserId = Guid.NewGuid().ToString() }; await user.SetIdAsync(new User.IdKey { TenantName = settings.MasterTenant, TrackName = settings.MasterTrack, Email = email }); await secretHashLogic.AddSecretHashAsync(user, password); user.Claims = new List <ClaimAndValues> { new ClaimAndValues { Claim = JwtClaimTypes.Role, Values = adminUserRoles.ToList() } }; user.SetPartitionId(); await simpleTenantRepository.SaveAsync(user); Console.WriteLine($"Administrator user document created and saved in Cosmos DB"); }
protected async Task <ActionResult <Api.OAuthClientSecretResponse> > Post(Api.OAuthClientSecretRequest party) { try { if (!await ModelState.TryValidateObjectAsync(party)) { return(BadRequest(ModelState)); } var oauthDownParty = await tenantService.GetAsync <TParty>(await DownParty.IdFormat(RouteBinding, party.PartyName)); var secret = new OAuthClientSecret(); await secretHashLogic.AddSecretHashAsync(secret, party.Secret); if (oauthDownParty.Client.Secrets == null) { oauthDownParty.Client.Secrets = new List <OAuthClientSecret>(); } oauthDownParty.Client.Secrets.Add(secret); await tenantService.UpdateAsync(oauthDownParty); return(Created(mapper.Map <Api.OAuthClientSecretResponse>(secret).Set(s => s.Name = new[] { oauthDownParty.Name, s.Name }.ToDotList()))); } catch (CosmosDataException ex) { if (ex.StatusCode == HttpStatusCode.Conflict) { logger.Warning(ex, $"Create secret on client '{typeof(TParty).Name}' by name '{party.PartyName}'."); return(Conflict(typeof(TParty).Name, party.PartyName)); } throw; } }
protected async Task <ActionResult> Post(Api.OAuthClientSecretRequest secretRequest) { try { if (!await ModelState.TryValidateObjectAsync(secretRequest)) { return(BadRequest(ModelState)); } secretRequest.PartyName = secretRequest.PartyName?.ToLower(); var oauthDownParty = await tenantRepository.GetAsync <TParty>(await DownParty.IdFormatAsync(RouteBinding, secretRequest.PartyName)); foreach (var s in secretRequest.Secrets) { var secret = new OAuthClientSecret(); await secretHashLogic.AddSecretHashAsync(secret, s); if (oauthDownParty.Client.Secrets == null) { oauthDownParty.Client.Secrets = new List <OAuthClientSecret>(); } oauthDownParty.Client.Secrets.Add(secret); } secretRequest.Secrets = oauthDownParty.Client.Secrets.Select(s => s.Id).ToList(); if (!await ModelState.TryValidateObjectAsync(secretRequest)) { return(BadRequest(ModelState)); } await tenantRepository.UpdateAsync(oauthDownParty); return(Created(new Api.OAuthDownParty { Name = secretRequest.PartyName })); } catch (CosmosDataException ex) { if (ex.StatusCode == HttpStatusCode.Conflict) { logger.Warning(ex, $"Conflict, Create secret on client '{typeof(TParty).Name}' by name '{secretRequest.PartyName}'."); return(Conflict(typeof(TParty).Name, secretRequest.PartyName)); } throw; } }