123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System;
- using System.Collections;
- using UnityEngine;
- namespace Mirror.Authenticators
- {
- [AddComponentMenu("Network/Authenticators/BasicAuthenticator")]
- public class BasicAuthenticator : NetworkAuthenticator
- {
- [Header("Custom Properties")]
- // set these in the inspector
- public string username;
- public string password;
- #region Messages
- public struct AuthRequestMessage : NetworkMessage
- {
- // use whatever credentials make sense for your game
- // for example, you might want to pass the accessToken if using oauth
- public string authUsername;
- public string authPassword;
- }
- public struct AuthResponseMessage : NetworkMessage
- {
- public byte code;
- public string message;
- }
- #endregion
- #region Server
- /// <summary>
- /// Called on server from StartServer to initialize the Authenticator
- /// <para>Server message handlers should be registered in this method.</para>
- /// </summary>
- public override void OnStartServer()
- {
- // register a handler for the authentication request we expect from client
- NetworkServer.RegisterHandler<AuthRequestMessage>(OnAuthRequestMessage, false);
- }
- /// <summary>
- /// Called on server from StopServer to reset the Authenticator
- /// <para>Server message handlers should be registered in this method.</para>
- /// </summary>
- public override void OnStopServer()
- {
- // unregister the handler for the authentication request
- NetworkServer.UnregisterHandler<AuthRequestMessage>();
- }
- /// <summary>
- /// Called on server from OnServerAuthenticateInternal when a client needs to authenticate
- /// </summary>
- /// <param name="conn">Connection to client.</param>
- public override void OnServerAuthenticate(NetworkConnection conn)
- {
- // do nothing...wait for AuthRequestMessage from client
- }
- /// <summary>
- /// Called on server when the client's AuthRequestMessage arrives
- /// </summary>
- /// <param name="conn">Connection to client.</param>
- /// <param name="msg">The message payload</param>
- public void OnAuthRequestMessage(NetworkConnection conn, AuthRequestMessage msg)
- {
- // Debug.LogFormat(LogType.Log, "Authentication Request: {0} {1}", msg.authUsername, msg.authPassword);
- // check the credentials by calling your web server, database table, playfab api, or any method appropriate.
- if (msg.authUsername == username && msg.authPassword == password)
- {
- // create and send msg to client so it knows to proceed
- AuthResponseMessage authResponseMessage = new AuthResponseMessage
- {
- code = 100,
- message = "Success"
- };
- conn.Send(authResponseMessage);
- // Accept the successful authentication
- ServerAccept(conn);
- }
- else
- {
- // create and send msg to client so it knows to disconnect
- AuthResponseMessage authResponseMessage = new AuthResponseMessage
- {
- code = 200,
- message = "Invalid Credentials"
- };
- conn.Send(authResponseMessage);
- // must set NetworkConnection isAuthenticated = false
- conn.isAuthenticated = false;
- // disconnect the client after 1 second so that response message gets delivered
- StartCoroutine(DelayedDisconnect(conn, 1));
- }
- }
- IEnumerator DelayedDisconnect(NetworkConnection conn, float waitTime)
- {
- yield return new WaitForSeconds(waitTime);
- // Reject the unsuccessful authentication
- ServerReject(conn);
- }
- #endregion
- #region Client
- /// <summary>
- /// Called on client from StartClient to initialize the Authenticator
- /// <para>Client message handlers should be registered in this method.</para>
- /// </summary>
- public override void OnStartClient()
- {
- // register a handler for the authentication response we expect from server
- NetworkClient.RegisterHandler<AuthResponseMessage>((Action<AuthResponseMessage>)OnAuthResponseMessage, false);
- }
- /// <summary>
- /// Called on client from StopClient to reset the Authenticator
- /// <para>Client message handlers should be unregistered in this method.</para>
- /// </summary>
- public override void OnStopClient()
- {
- // unregister the handler for the authentication response
- NetworkClient.UnregisterHandler<AuthResponseMessage>();
- }
- /// <summary>
- /// Called on client from OnClientAuthenticateInternal when a client needs to authenticate
- /// </summary>
- public override void OnClientAuthenticate()
- {
- AuthRequestMessage authRequestMessage = new AuthRequestMessage
- {
- authUsername = username,
- authPassword = password
- };
- NetworkClient.connection.Send(authRequestMessage);
- }
- // Deprecated 2021-04-29
- [Obsolete("Call OnAuthResponseMessage without the NetworkConnection parameter. It always points to NetworkClient.connection anyway.")]
- public void OnAuthResponseMessage(NetworkConnection conn, AuthResponseMessage msg) => OnAuthResponseMessage(msg);
- /// <summary>
- /// Called on client when the server's AuthResponseMessage arrives
- /// </summary>
- /// <param name="msg">The message payload</param>
- public void OnAuthResponseMessage(AuthResponseMessage msg)
- {
- if (msg.code == 100)
- {
- // Debug.LogFormat(LogType.Log, "Authentication Response: {0}", msg.message);
- // Authentication has been accepted
- ClientAccept();
- }
- else
- {
- Debug.LogError($"Authentication Response: {msg.message}");
- // Authentication has been rejected
- ClientReject();
- }
- }
- #endregion
- }
- }
|