public async Task <IActionResult> JoinGuildChat([FromServices] ICharacterSessionRepository characterSessionRepository, [FromServices] IFactoryCreatable <VivoxTokenClaims, VivoxTokenClaimsCreationContext> claimsFactory, [FromServices] IGuildCharacterMembershipRepository guildMembershipRepository, [FromServices] IVivoxTokenSignService signService) { int accountId = this.ClaimsReader.GetAccountIdInt(User); //If the user doesn't actually have a claimed session in the game //then we shouldn't log them into Vivox. if (!await characterSessionRepository.AccountHasActiveSession(accountId)) { return(BuildFailedResponseModel(VivoxLoginResponseCode.NoActiveCharacterSession)); } int characterId = await RetrieveSessionCharacterIdAsync(characterSessionRepository, accountId); if (!await guildMembershipRepository.ContainsAsync(characterId)) { return(BuildFailedResponseModel(VivoxLoginResponseCode.ChannelUnavailable)); } int guildId = (await guildMembershipRepository.RetrieveAsync(characterId)).GuildId; //TODO: Use a factory for channel name generation maybe? VivoxTokenClaims claims = claimsFactory.Create(new VivoxTokenClaimsCreationContext(characterId, VivoxAction.JoinChannel, new VivoxChannelData(false, $"Guild-{guildId}"))); //We don't send it back in a JSON form even though it's technically a JSON object //because the client just needs it as a raw string anyway to put through the Vivox client API. return(BuildSuccessfulResponseModel(new VivoxChannelJoinResponse(signService.CreateSignature(claims), claims.DestinationSIPURI))); }
public static async Task Test_Controller_Produces_AlreadyHasActiveSession_When_Session_Has() { //arrange IServiceProvider serviceProvider = ControllerTestsHelpers.BuildServiceProvider <CharacterSessionController>("Test", 1); CharacterSessionController controller = serviceProvider.GetService <CharacterSessionController>(); ICharacterRepository characterRepo = serviceProvider.GetService <ICharacterRepository>(); ICharacterSessionRepository sessionRepo = serviceProvider.GetService <ICharacterSessionRepository>(); await characterRepo.TryCreateAsync(new CharacterEntryModel(1, "Testing")); await sessionRepo.TryCreateAsync(new CharacterSessionModel(1, 0)); //We can't create the claimed session through this interface because it's a stored procedure. //Raw SQL can't execute. So we must interact directly with the DbSet //await sessionRepo.TryClaimUnclaimedSession(1, 1); CharacterDatabaseContext context = serviceProvider.GetService <CharacterDatabaseContext>(); await context.ClaimedSession.AddAsync(new ClaimedSessionsModel(1)); await context.SaveChangesAsync(); //act CharacterSessionEnterResponse response = await controller.EnterSession(1); //assert Assert.False(response.isSuccessful, $"Characters that already have "); Assert.AreEqual(CharacterSessionEnterResponseCode.AccountAlreadyHasCharacterSession, response.ResultCode); }
public async Task <IActionResult> JoinZoneProximityChat([FromServices] ICharacterSessionRepository characterSessionRepository, [FromServices] IFactoryCreatable <VivoxTokenClaims, VivoxTokenClaimsCreationContext> claimsFactory, [FromServices] IVivoxTokenSignService signService) { int accountId = this.ClaimsReader.GetAccountIdInt(User); //If the user doesn't actually have a claimed session in the game //then we shouldn't log them into Vivox. if (!await characterSessionRepository.AccountHasActiveSession(accountId)) { return(BuildFailedResponseModel(VivoxLoginResponseCode.NoActiveCharacterSession)); } int characterId = await RetrieveSessionCharacterIdAsync(characterSessionRepository, accountId); CharacterSessionModel session = await characterSessionRepository.RetrieveAsync(characterId); //Players in the same zone will all join the same proximity channel such as Prox-1. //They can use this for proximity text and voice chat. //TODO: Use a factory for channel name generation maybe? VivoxTokenClaims claims = claimsFactory.Create(new VivoxTokenClaimsCreationContext(characterId, VivoxAction.JoinChannel, new VivoxChannelData(true, $"Prox-{session.ZoneId}"))); //We don't send it back in a JSON form even though it's technically a JSON object //because the client just needs it as a raw string anyway to put through the Vivox client API. return(BuildSuccessfulResponseModel(new VivoxChannelJoinResponse(signService.CreateSignature(claims), claims.DestinationSIPURI))); }
public ZonePersistenceController(IClaimsPrincipalReader claimsReader, ILogger <AuthorizationReadyController> logger, [NotNull] ICharacterSessionRepository characterSessionRepository) : base(claimsReader, logger) { CharacterSessionRepository = characterSessionRepository ?? throw new ArgumentNullException(nameof(characterSessionRepository)); }
private static async Task <int> RetrieveSessionCharacterIdAsync(ICharacterSessionRepository characterSessionRepository, int accountId) { //TODO: Technically a race condition here. //Now let's actually get the character id of the session that the account has ClaimedSessionsModel session = await characterSessionRepository.RetrieveClaimedSessionByAccountId(accountId); int characterId = session.CharacterId; return(characterId); }
public static async Task Test_Controller_Produces_InvalidId_When_Wrong_AccountId() { //arrange IServiceProvider serviceProvider = ControllerTestsHelpers.BuildServiceProvider <CharacterSessionController>("Test", 2); CharacterSessionController controller = serviceProvider.GetService <CharacterSessionController>(); ICharacterRepository characterRepo = serviceProvider.GetService <ICharacterRepository>(); ICharacterSessionRepository sessionRepo = serviceProvider.GetService <ICharacterSessionRepository>(); await characterRepo.TryCreateAsync(new CharacterEntryModel(1, "Testing")); await sessionRepo.TryCreateAsync(new CharacterSessionModel(1, 0)); //act CharacterSessionEnterResponse response = await controller.EnterSession(1); //assert Assert.False(response.isSuccessful, $"Characters should not be able to create sessions when the accountid doesn't match."); Assert.AreEqual(response.ResultCode, CharacterSessionEnterResponseCode.InvalidCharacterIdError); }
public static async Task Test_Controller_Produces_SessionGranted_With_Zone_Id_If_UnclaimedSession_Exists(int accountId, int zoneid) { //arrange IServiceProvider serviceProvider = ControllerTestsHelpers.BuildServiceProvider <CharacterSessionController>("Test", accountId); CharacterSessionController controller = serviceProvider.GetService <CharacterSessionController>(); ICharacterRepository characterRepo = serviceProvider.GetService <ICharacterRepository>(); ICharacterSessionRepository sessionRepo = serviceProvider.GetService <ICharacterSessionRepository>(); await characterRepo.TryCreateAsync(new CharacterEntryModel(accountId, "Testing")); await sessionRepo.TryCreateAsync(new CharacterSessionModel(1, zoneid)); //act CharacterSessionEnterResponse response = await controller.EnterSession(1); //assert Assert.True(response.isSuccessful, $"Created sessions should be granted if no active account session or character session is claimed."); Assert.AreEqual(CharacterSessionEnterResponseCode.Success, response.ResultCode); Assert.AreEqual(zoneid, response.ZoneId, $"Provided zone id was not the same as the session."); }
public async Task <IActionResult> LoginVivox([FromServices] ICharacterSessionRepository characterSessionRepository, [FromServices] IFactoryCreatable <VivoxTokenClaims, VivoxTokenClaimsCreationContext> claimsFactory, [FromServices] IVivoxTokenSignService signService) { int accountId = this.ClaimsReader.GetAccountIdInt(User); //If the user doesn't actually have a claimed session in the game //then we shouldn't log them into Vivox. if (!await characterSessionRepository.AccountHasActiveSession(accountId)) { return(BuildFailedResponseModel(VivoxLoginResponseCode.NoActiveCharacterSession)); } int characterId = await RetrieveSessionCharacterIdAsync(characterSessionRepository, accountId); VivoxTokenClaims claims = claimsFactory.Create(new VivoxTokenClaimsCreationContext(characterId, VivoxAction.Login)); //We don't send it back in a JSON form even though it's technically a JSON object //because the client just needs it as a raw string anyway to put through the Vivox client API. return(BuildSuccessfulResponseModel(signService.CreateSignature(claims))); }
/// <inheritdoc /> public CharacterSessionController(IClaimsPrincipalReader claimsReader, ILogger <AuthorizationReadyController> logger, [FromServices] ICharacterRepository characterRepository, [FromServices] ICharacterSessionRepository characterSessionRepository) : base(claimsReader, logger) { CharacterRepository = characterRepository ?? throw new ArgumentNullException(nameof(characterRepository)); CharacterSessionRepository = characterSessionRepository ?? throw new ArgumentNullException(nameof(characterSessionRepository)); }