NetworkAuthenticator.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. namespace Mirror
  5. {
  6. [Serializable] public class UnityEventNetworkConnection : UnityEvent<NetworkConnection> {}
  7. /// <summary>Base class for implementing component-based authentication during the Connect phase</summary>
  8. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-authenticators")]
  9. public abstract class NetworkAuthenticator : MonoBehaviour
  10. {
  11. /// <summary>Notify subscribers on the server when a client is authenticated</summary>
  12. [Header("Event Listeners (optional)")]
  13. [Tooltip("Mirror has an internal subscriber to this event. You can add your own here.")]
  14. public UnityEventNetworkConnection OnServerAuthenticated = new UnityEventNetworkConnection();
  15. /// <summary>Notify subscribers on the client when the client is authenticated</summary>
  16. [Tooltip("Mirror has an internal subscriber to this event. You can add your own here.")]
  17. public UnityEventNetworkConnection OnClientAuthenticated = new UnityEventNetworkConnection();
  18. /// <summary>Called when server starts, used to register message handlers if needed.</summary>
  19. public virtual void OnStartServer() {}
  20. /// <summary>Called when server stops, used to unregister message handlers if needed.</summary>
  21. public virtual void OnStopServer() {}
  22. /// <summary>Called on server from OnServerAuthenticateInternal when a client needs to authenticate</summary>
  23. public abstract void OnServerAuthenticate(NetworkConnection conn);
  24. protected void ServerAccept(NetworkConnection conn)
  25. {
  26. OnServerAuthenticated.Invoke(conn);
  27. }
  28. protected void ServerReject(NetworkConnection conn)
  29. {
  30. conn.Disconnect();
  31. }
  32. /// <summary>Called when client starts, used to register message handlers if needed.</summary>
  33. public virtual void OnStartClient() {}
  34. /// <summary>Called when client stops, used to unregister message handlers if needed.</summary>
  35. public virtual void OnStopClient() {}
  36. // Deprecated 2021-03-13
  37. [Obsolete("Remove the NetworkConnection parameter from your override and use NetworkClient.connection instead")]
  38. public virtual void OnClientAuthenticate(NetworkConnection conn) => OnClientAuthenticate();
  39. /// <summary>Called on client from OnClientAuthenticateInternal when a client needs to authenticate</summary>
  40. public abstract void OnClientAuthenticate();
  41. // Deprecated 2021-03-13
  42. [Obsolete("Remove the NetworkConnection parameter from your override and use NetworkClient.connection instead")]
  43. protected void ClientAccept(NetworkConnection conn) => ClientAccept();
  44. protected void ClientAccept()
  45. {
  46. OnClientAuthenticated.Invoke(NetworkClient.connection);
  47. }
  48. // Deprecated 2021-03-13
  49. [Obsolete("Remove the NetworkConnection parameter from your override and use NetworkClient.connection instead")]
  50. protected void ClientReject(NetworkConnection conn) => ClientReject();
  51. protected void ClientReject()
  52. {
  53. // Set this on the client for local reference
  54. NetworkClient.connection.isAuthenticated = false;
  55. // disconnect the client
  56. NetworkClient.connection.Disconnect();
  57. }
  58. void OnValidate()
  59. {
  60. #if UNITY_EDITOR
  61. // automatically assign authenticator field if we add this to NetworkManager
  62. NetworkManager manager = GetComponent<NetworkManager>();
  63. if (manager != null && manager.authenticator == null)
  64. {
  65. manager.authenticator = this;
  66. UnityEditor.Undo.RecordObject(gameObject, "Assigned NetworkManager authenticator");
  67. }
  68. #endif
  69. }
  70. }
  71. }