DeviceAuthenticator.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using UnityEngine;
  3. namespace Mirror.Authenticators
  4. {
  5. /// <summary>
  6. /// An authenticator that identifies the user by their device.
  7. /// <para>A GUID is used as a fallback when the platform doesn't support SystemInfo.deviceUniqueIdentifier.</para>
  8. /// <para>Note: deviceUniqueIdentifier can be spoofed, so security is not guaranteed.</para>
  9. /// <para>See https://docs.unity3d.com/ScriptReference/SystemInfo-deviceUniqueIdentifier.html for details.</para>
  10. /// </summary>
  11. [AddComponentMenu("Network/ Authenticators/Device Authenticator")]
  12. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-authenticators/device-authenticator")]
  13. public class DeviceAuthenticator : NetworkAuthenticator
  14. {
  15. #region Messages
  16. public struct AuthRequestMessage : NetworkMessage
  17. {
  18. public string clientDeviceID;
  19. }
  20. public struct AuthResponseMessage : NetworkMessage { }
  21. #endregion
  22. #region Server
  23. /// <summary>
  24. /// Called on server from StartServer to initialize the Authenticator
  25. /// <para>Server message handlers should be registered in this method.</para>
  26. /// </summary>
  27. public override void OnStartServer()
  28. {
  29. // register a handler for the authentication request we expect from client
  30. NetworkServer.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage, false);
  31. }
  32. /// <summary>
  33. /// Called on server from StopServer to reset the Authenticator
  34. /// <para>Server message handlers should be registered in this method.</para>
  35. /// </summary>
  36. public override void OnStopServer()
  37. {
  38. // unregister the handler for the authentication request
  39. NetworkServer.UnregisterHandler<AuthRequestMessage>();
  40. }
  41. /// <summary>
  42. /// Called on server from OnServerConnectInternal when a client needs to authenticate
  43. /// </summary>
  44. /// <param name="conn">Connection to client.</param>
  45. public override void OnServerAuthenticate(NetworkConnectionToClient conn)
  46. {
  47. // do nothing, wait for client to send his id
  48. }
  49. void OnAuthRequestMessage(NetworkConnectionToClient conn, AuthRequestMessage msg)
  50. {
  51. Debug.Log($"connection {conn.connectionId} authenticated with id {msg.clientDeviceID}");
  52. // Store the device id for later reference, e.g. when spawning the player
  53. conn.authenticationData = msg.clientDeviceID;
  54. // Send a response to client telling it to proceed as authenticated
  55. conn.Send(new AuthResponseMessage());
  56. // Accept the successful authentication
  57. ServerAccept(conn);
  58. }
  59. #endregion
  60. #region Client
  61. /// <summary>
  62. /// Called on client from StartClient to initialize the Authenticator
  63. /// <para>Client message handlers should be registered in this method.</para>
  64. /// </summary>
  65. public override void OnStartClient()
  66. {
  67. // register a handler for the authentication response we expect from server
  68. NetworkClient.RegisterHandler<AuthResponseMessage>(OnAuthResponseMessage, false);
  69. }
  70. /// <summary>
  71. /// Called on client from StopClient to reset the Authenticator
  72. /// <para>Client message handlers should be unregistered in this method.</para>
  73. /// </summary>
  74. public override void OnStopClient()
  75. {
  76. // unregister the handler for the authentication response
  77. NetworkClient.UnregisterHandler<AuthResponseMessage>();
  78. }
  79. /// <summary>
  80. /// Called on client from OnClientConnectInternal when a client needs to authenticate
  81. /// </summary>
  82. public override void OnClientAuthenticate()
  83. {
  84. string deviceUniqueIdentifier = SystemInfo.deviceUniqueIdentifier;
  85. // Not all platforms support this, so we use a GUID instead
  86. if (deviceUniqueIdentifier == SystemInfo.unsupportedIdentifier)
  87. {
  88. // Get the value from PlayerPrefs if it exists, new GUID if it doesn't
  89. deviceUniqueIdentifier = PlayerPrefs.GetString("deviceUniqueIdentifier", Guid.NewGuid().ToString());
  90. // Store the deviceUniqueIdentifier to PlayerPrefs (in case we just made a new GUID)
  91. PlayerPrefs.SetString("deviceUniqueIdentifier", deviceUniqueIdentifier);
  92. }
  93. // send the deviceUniqueIdentifier to the server
  94. NetworkClient.Send(new AuthRequestMessage { clientDeviceID = deviceUniqueIdentifier } );
  95. }
  96. /// <summary>
  97. /// Called on client when the server's AuthResponseMessage arrives
  98. /// </summary>
  99. /// <param name="msg">The message payload</param>
  100. public void OnAuthResponseMessage(AuthResponseMessage msg)
  101. {
  102. Debug.Log("Authentication Success");
  103. ClientAccept();
  104. }
  105. #endregion
  106. }
  107. }