Add docs for using builtin auth mechanism#10719
Add docs for using builtin auth mechanism#10719baccenfutter wants to merge 1 commit intogodotengine:masterfrom
Conversation
04ec47d to
bfa5410
Compare
bfa5410 to
b9c3c6e
Compare
|
In addition to discoverability of the authentication mechanism being low, there also appears to be misleading information in the relevant documentation. The I can only see the authentication handshake happen between a client peer and the server, and once the authentication with the server has completed, I'm not terribly familiar with the godot source code, but that also appears to be what is happening. Ideally the previously mentioned documentation can also be updated as part of this PR to make the entire mechanism more clear. |
| # signal to the MultiplayerAPI that the authentication was successful | ||
| multiplayer.complete_auth(peer_id) | ||
|
|
||
| As soon as both, the client's and the the server's :ref:`complete_auth() <class_SceneMultiplayer_method_complete_auth>`, have been called, the connection is considered to be established and the `connected_to_server` and `peer_connected` signals fire. |
There was a problem hiding this comment.
| As soon as both, the client's and the the server's :ref:`complete_auth() <class_SceneMultiplayer_method_complete_auth>`, have been called, the connection is considered to be established and the `connected_to_server` and `peer_connected` signals fire. | |
| As soon as both, the client's and the server's :ref:`complete_auth() <class_SceneMultiplayer_method_complete_auth>`, | |
| have been called, the connection is considered to be established and the | |
| `connected_to_server` and `peer_connected` signals fire. |
There was a problem hiding this comment.
Rather:
| As soon as both, the client's and the the server's :ref:`complete_auth() <class_SceneMultiplayer_method_complete_auth>`, have been called, the connection is considered to be established and the `connected_to_server` and `peer_connected` signals fire. | |
| As soon as both the client's and the server's :ref:`complete_auth() <class_SceneMultiplayer_method_complete_auth>` methods | |
| have been called, the connection is considered to be established and the | |
| `connected_to_server` and `peer_connected` signals fire. |
Too many commas
| Before hosting your game online to a public audience, you may want to consider adding authentication and protecting your RPCs against unauthenticated access. | ||
| For this you can use the :ref:`SceneMultiplayer <class_SceneMultiplayer>`'s builtin authentication mechanism. |
There was a problem hiding this comment.
| Before hosting your game online to a public audience, you may want to consider adding authentication and protecting your RPCs against unauthenticated access. | |
| For this you can use the :ref:`SceneMultiplayer <class_SceneMultiplayer>`'s builtin authentication mechanism. | |
| Before hosting your game online to a public audience, you may want to consider adding authentication and protecting your RPCs against unauthenticated access. | |
| You can use the :ref:`SceneMultiplayer <class_SceneMultiplayer>`'s builtin authentication mechanism for this. |
There was a problem hiding this comment.
We generally use built-in as well afaik
| .. tabs:: | ||
| .. code-tab:: gdscript GDScript | ||
|
|
||
| # after multiplayer.multiplayer_peer = peer |
There was a problem hiding this comment.
| # after multiplayer.multiplayer_peer = peer | |
| # This goes after `multiplayer.multiplayer_peer = peer`. |
| multiplayer.auth_timout = 3 | ||
| multiplayer.auth_callback = func(peer_id: int, payload: PackedByteArray): | ||
| var auth_data: Dictionary = JSON.parse_string(payload.get_string_from_utf8()) | ||
| # your authentication logic goes here... |
There was a problem hiding this comment.
| # your authentication logic goes here... | |
| # Your authentication logic (such as checking the supplied username/password against a database) | |
| # goes here. |
| var auth_data: Dictionary = JSON.parse_string(payload.get_string_from_utf8()) | ||
| # your authentication logic goes here... | ||
|
|
||
| # signal to the MultiplayerAPI that the authentication was successful |
There was a problem hiding this comment.
"Signal" may be confused with emitting a signal here.
| # signal to the MultiplayerAPI that the authentication was successful | |
| # Tell the MultiplayerAPI that the authentication was successful. |
| .. tabs:: | ||
| .. code-tab:: gdscript GDScript | ||
|
|
||
| # after multiplayer.multiplayer_peer = peer |
There was a problem hiding this comment.
| # after multiplayer.multiplayer_peer = peer | |
| # This goes after `multiplayer.multiplayer_peer = peer`. |
|
|
||
| # after multiplayer.multiplayer_peer = peer | ||
| multiplayer.auth_callback = func: | ||
| # we have to set this on the client in order for the peer_authenticating signal to fire |
There was a problem hiding this comment.
| # we have to set this on the client in order for the peer_authenticating signal to fire | |
| # We have to set this on the client for the `peer_authenticating` | |
| # signal to emit. |
| } | ||
| multiplayer.send_auth(1, JSON.stringify(auth_data).to_utf8_buffer()) | ||
|
|
||
| # signal to the MultiplayerAPI that the authentication was successful |
There was a problem hiding this comment.
| # signal to the MultiplayerAPI that the authentication was successful | |
| # Tell the MultiplayerAPI that the authentication was successful. |
|
Might I suggest not using lambda in your examples? It's a lot cleaner and easier to follow if the method being linked is distinct. |
| # signal to the MultiplayerAPI that the authentication was successful | ||
| multiplayer.complete_auth(peer_id) | ||
|
|
||
| As soon as both, the client's and the the server's :ref:`complete_auth() <class_SceneMultiplayer_method_complete_auth>`, have been called, the connection is considered to be established and the `connected_to_server` and `peer_connected` signals fire. |
There was a problem hiding this comment.
Rather:
| As soon as both, the client's and the the server's :ref:`complete_auth() <class_SceneMultiplayer_method_complete_auth>`, have been called, the connection is considered to be established and the `connected_to_server` and `peer_connected` signals fire. | |
| As soon as both the client's and the server's :ref:`complete_auth() <class_SceneMultiplayer_method_complete_auth>` methods | |
| have been called, the connection is considered to be established and the | |
| `connected_to_server` and `peer_connected` signals fire. |
Too many commas
| var auth_data = { | ||
| "username": "username", | ||
| "password": "password", | ||
| } | ||
| multiplayer.send_auth(1, JSON.stringify(auth_data).to_utf8_buffer()) | ||
|
|
||
| # signal to the MultiplayerAPI that the authentication was successful | ||
| multiplayer.complete_auth(peer_id) |
There was a problem hiding this comment.
| var auth_data = { | |
| "username": "username", | |
| "password": "password", | |
| } | |
| multiplayer.send_auth(1, JSON.stringify(auth_data).to_utf8_buffer()) | |
| # signal to the MultiplayerAPI that the authentication was successful | |
| multiplayer.complete_auth(peer_id) | |
| var auth_data = { | |
| "username": "username", | |
| "password": "password", | |
| } | |
| multiplayer.send_auth(1, JSON.stringify(auth_data).to_utf8_buffer()) | |
| # Tell the MultiplayerAPI that the authentication was successful. | |
| multiplayer.complete_auth(peer_id) |
Continuation for a method uses two indentations, see the style guide
| var auth_data: Dictionary = JSON.parse_string(payload.get_string_from_utf8()) | ||
| # your authentication logic goes here... | ||
|
|
||
| # signal to the MultiplayerAPI that the authentication was successful | ||
| if authentication_successful: | ||
| multiplayer.complete_auth(peer_id) |
There was a problem hiding this comment.
| var auth_data: Dictionary = JSON.parse_string(payload.get_string_from_utf8()) | |
| # your authentication logic goes here... | |
| # signal to the MultiplayerAPI that the authentication was successful | |
| if authentication_successful: | |
| multiplayer.complete_auth(peer_id) | |
| var auth_data: Dictionary = JSON.parse_string(payload.get_string_from_utf8()) | |
| # Your authentication logic (such as checking the supplied username/password against a database) | |
| # goes here. | |
| # Tell the MultiplayerAPI that the authentication was successful. | |
| if authentication_successful: | |
| multiplayer.complete_auth(peer_id) |
| Before hosting your game online to a public audience, you may want to consider adding authentication and protecting your RPCs against unauthenticated access. | ||
| For this you can use the :ref:`SceneMultiplayer <class_SceneMultiplayer>`'s builtin authentication mechanism. |
There was a problem hiding this comment.
We generally use built-in as well afaik
| # we have to set this on the client in order for the peer_authenticating signal to fire | ||
| pass |
There was a problem hiding this comment.
| # we have to set this on the client in order for the peer_authenticating signal to fire | |
| pass | |
| # We have to set this on the client for the `peer_authenticating` | |
| # signal to emit. | |
| pass |
Took a huge unnecessary detour, today. it was an upstream battle...
Since there was no mention of it whatsoever, I intuitively concluded that I would have to build my own authentication on top of
SceneMultiplayer. The authentication part was easy, but when trying to figure out how to hook into Godot's RPC call-stack to prevent unauthenticated RPC calls, I got stuck and was made aware ofSceneMultiplayer.auth_callbackby Discord.Getting the builtin auth mechanism to work with the available docs on
SceneMultiplayerwas an upstream battle.But the main issue to me, is the complete lack of mentioning the availability of the builtin auth mechanism, at all.
Wanted to share my results and prevent others from having to take the same detour.