-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement NpTicket Game Authentication (#141)
- Loading branch information
1 parent
0a85293
commit e12faea
Showing
31 changed files
with
377 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
SoundShapesServer/Database/GameDatabaseContext.GameIps.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using SoundShapesServer.Helpers; | ||
using SoundShapesServer.Types; | ||
using SoundShapesServer.Types.Sessions; | ||
using SoundShapesServer.Types.Users; | ||
|
||
namespace SoundShapesServer.Database; | ||
|
||
public partial class GameDatabaseContext | ||
{ | ||
private GameIp CreateIpAddress(GameUser user, string ipAddress) | ||
{ | ||
GameIp gameIp = new(ipAddress, user); | ||
|
||
_realm.Write(() => | ||
{ | ||
_realm.Add(gameIp); | ||
}); | ||
|
||
return gameIp; | ||
} | ||
|
||
public GameIp GetIpFromAddress(GameUser user, string ipAddress) | ||
{ | ||
_realm.Refresh(); | ||
|
||
GameIp? ip = user.IpAddresses.FirstOrDefault(i => i.IpAddress == ipAddress); | ||
return ip ?? CreateIpAddress(user, ipAddress); | ||
} | ||
public bool AuthorizeIpAddress(GameIp gameIp, bool oneTime) | ||
{ | ||
if (gameIp.Authorized) return false; | ||
|
||
_realm.Write(() => | ||
{ | ||
gameIp.Authorized = true; | ||
gameIp.OneTimeUse = oneTime; | ||
gameIp.ModificationDate = DateTimeOffset.UtcNow; | ||
foreach (GameSession session in gameIp.Sessions.Where(s=>s._SessionType == (int)SessionType.GameUnAuthorized)) | ||
{ | ||
session.SessionType = SessionType.Game; | ||
} | ||
}); | ||
|
||
_realm.Refresh(); | ||
|
||
return true; | ||
} | ||
|
||
public void RemoveIpAddress(GameIp gameIp) | ||
{ | ||
_realm.Write(() => | ||
{ | ||
// Remove all sessions with ip address | ||
foreach (GameSession session in gameIp.Sessions) | ||
{ | ||
_realm.Remove(session); | ||
} | ||
_realm.Remove(gameIp); | ||
}); | ||
|
||
_realm.Refresh(); | ||
} | ||
|
||
public void UseOneTimeIpAddress(GameIp gameIp) | ||
{ | ||
_realm.Write(() => | ||
{ | ||
gameIp.Authorized = false; | ||
gameIp.OneTimeUse = false; | ||
gameIp.ModificationDate = DateTimeOffset.UtcNow; | ||
}); | ||
} | ||
|
||
public (GameIp[], int) GetPaginatedIps(GameUser user, bool? authorized, int from, int count) | ||
{ | ||
IQueryable<GameIp> filteredAddresses = GetIps(user, authorized); | ||
GameIp[] paginatedAddresses = PaginationHelper.PaginateIpAddresses(filteredAddresses, from, count); | ||
|
||
return (paginatedAddresses, filteredAddresses.Count()); | ||
} | ||
|
||
private IQueryable<GameIp> GetIps(GameUser user, bool? authorized) | ||
{ | ||
IQueryable<GameIp> addresses = _realm.All<GameIp>().Where(i => i.User == user); | ||
IQueryable<GameIp> filteredAddresses = FilterIpAddresses(addresses, authorized); | ||
|
||
return filteredAddresses; | ||
} | ||
|
||
private static IQueryable<GameIp> FilterIpAddresses(IQueryable<GameIp> addresses, | ||
bool? authorized) | ||
{ | ||
IQueryable<GameIp> response = addresses; | ||
|
||
if (authorized != null) | ||
{ | ||
response = response.Where(i => i.Authorized == authorized); | ||
} | ||
|
||
return response; | ||
} | ||
} |
106 changes: 0 additions & 106 deletions
106
SoundShapesServer/Database/GameDatabaseContext.IpAuthorizations.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.