public ClientID ValidateClientID(GameServerUserToken token, GameServerID gameserverId) { var name = ValidateAndGetNameImpl(token.Credential, gameserverId); return(new ClientID { ID = Guid.Parse(name) }); }
public GameServerUserToken CreatePlayerToken(ClientID clientID, GameServerID gameserverId) { var name = clientID.ID.ToString(); return(new GameServerUserToken { Credential = CreatePlayerTokenImpl(name, gameserverId) }); }
static void Main(string[] args) { Console.Title = "Backend.GameServer"; if (args.Length != 4) { Console.Error.WriteLine("Provide ip address (such as 127.0.0.1), TCP port and secret key (base64) as command line args"); return; } var argIpAddress = args[0]; var argPortNumber = args[1]; var argSecretKey = args[2]; var argGameServerId = args[3]; IPAddress address; if (!IPAddress.TryParse(argIpAddress, out address)) { Console.Error.WriteLine("\"{0}\" is not a valid IP address", args[0]); return; } int port; if (!int.TryParse(argPortNumber, out port)) { Console.Error.WriteLine("\"{0}\" is not a valid TCP port", args[1]); return; } var ipEndPoint = new IPEndPoint(address, port); byte[] secretKey = Convert.FromBase64String(argSecretKey); var gameServerID = new GameServerID { ID = Guid.Parse(argGameServerId) }; Console.WriteLine("Listen on {0}", ipEndPoint); var cts = new CancellationTokenSource(); var server = new AsyncServerHost(ipEndPoint); var gameServerImpl = new GameServerImpl(gameServerID, secretKey); Task t = server.Start(gameServerImpl, cts.Token); Console.WriteLine("Launched game server process on {0}", ipEndPoint); t.Wait(); }
private string ValidateAndGetNameImpl(string tokenString, GameServerID gameserverId) { var tokenHandler = new JwtSecurityTokenHandler(); var validationParameters = new TokenValidationParameters() { ValidAudience = ComposeAudienceUrl(gameserverId), IssuerSigningToken = new BinarySecretSecurityToken(this._synchroniedKey), ValidIssuer = _validIssuer }; try { ClaimsPrincipal principal = tokenHandler.ValidateToken(tokenString, validationParameters); return(principal.Claims.First(_ => _.Type == ClaimTypes.NameIdentifier).Value); } catch (Exception) { return(null); } }
private string CreatePlayerTokenImpl(string name, GameServerID gameserverId) { var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, name), new Claim(ClaimTypes.Role, "Player"), }), TokenIssuerName = _validIssuer, AppliesToAddress = ComposeAudienceUrl(gameserverId), Lifetime = new Lifetime( created: DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(1)), expires: DateTime.UtcNow.AddMinutes(60)), SigningCredentials = new SigningCredentials( new InMemorySymmetricSecurityKey(this._synchroniedKey), "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", "http://www.w3.org/2001/04/xmlenc#sha256") }; var tokenHandler = new JwtSecurityTokenHandler(); var token = tokenHandler.CreateToken(tokenDescriptor); return(tokenHandler.WriteToken(token)); }
public GameServerImpl(GameServerID gameserverID, byte[] secretKey) { this.GameServerID = gameserverID; this.PlayerAuthenticator = new PlayerAuthenticator(secretKey); }
public LoginToLobbyResponseMessage(IPEndPoint gameServer, int innergameServerPort, GameServerUserToken token, GameServerID gameServerID) { this.GameServer = gameServer; this.InnergameServerPort = innergameServerPort; this.Token = token; this.GameServerID = gameServerID; }
private string ComposeAudienceUrl(GameServerID gameserverId) { return(string.Format("game://{0}", gameserverId.ID.ToString())); }