Using Vivox Access Tokens together with UAS
Follow this workflow to use Vivox Access Tokens together with Unity Authentication Services.
Read time 4 minutesLast updated 2 days ago
To have full control over which player can access which channels you can have your own back end mint Vivox Access Tokens (VATs).
Prerequisites:
- Set up a Unity Cloud project
- Make sure you have your project ID, environment ID, and environment name.
- Enable the UAS custom ID provider
- Make sure you have your Service Account credentials handy from this setup.
Creating VATs
- The Vivox SDK will request a VAT from the back end whenever one is needed using a custom token provider implementation.
- Add the to the request to the back end when requesting VATs within the
accessTokenmethod.GetTokenAsync - Validate and extract the needed information from the token in the back end and create the Vivox access token to send it back to the client.
VAT creation flow
This diagram covers the flow of creating VATs while using UAS:- The game client sends UAS access tokens to the game back end
- The game back end either gets token validation from the Unity Authentication Service with a JWKS refresh or validates the token itself using UAS player ID and environment ID.
- Once validated the game back end then returns a VAT to the game client.
Client-side setup
In your game client, create a class that implements the Vivox SDK’sIVivoxTokenProviderVivoxService.Instance.SetTokenProviderIVivoxTokenProvider.GetTokenAsyncIVivoxTokenProvider.GetTokenAsyncIVivoxTokenProvider.GetTokenAsyncTo subscribe
Before logging in your player, you must register yourIVivoxTokenProviderVivoxService.Instance.SetTokenProvider(new CustomTokenProvider());
Fetch the token
Create the payload with all the parameters provided in the overridden method, even if some are empty, and send it to your secure server to generate your Vivox Access Token. Best practice is to send all the parameters; only what’s needed for the payload will be returned. Use the payload as input in theGetTokenAsyncaccessTokentargetUserUrichannelUripublic class VoiceManager : MonoBehaviour{ async void Start() { // Must be done before any other Vivox action otherwise tokens will not be generated properly. VivoxService.Instance.SetTokenProvider(new VivoxTokenProvider()); await UnityServices.InitializeAsync(); await VivoxService.Instance.InitializeAsync(); }}class VivoxTokenProvider : IVivoxTokenProvider{ public Task<string> GetTokenAsync(string issuer = null, TimeSpan? expiration = null, string targetUserUri = null, string action = null, string channelUri = null, string fromUserUri = null, string realm = null) { if (!AuthenticationService.Instance.SessionTokenExists) { // Player not logged in! } var accessToken = AuthenticationService.Instance.AccessToken // Implement token fetching logic here. // The method parameters together with the accessToken from the AuthenticationService contain the necessary information for crafting the request payload. // This will be called whenever a token is needed for a Vivox action }}
Server-side setup
Your back end service needs to expose an API that the client can invoke within theGetTokenAsyncThesip:.issuer.unity_player_id.unity_environment_id.@domain.vivox.com
issuerunity_player_idunity_environment_idThesip:confctl-g-issuer.channel_name.unity_environment_id@domain.vivox.com
ge, g, dissuerchannel_nameunity_environment_idunity_player_idunity_environment_idGetTokenAsyncaccessTokensubaudaudWith that you can create the VAT and return it to the client.{ "header": { "alg": "RS256", "kid": "<id>", "typ": "JWT" }, "payload": { "aud": [ "upid:{{unity_project_id}}", "envName:{{environment_name}}", "envId:{{unity_environment_id}}" // unity environment id ], "exp": 1617677595, // expires at "iat": 1617673872, // issued at "nbf": 1617673872, // not valid before "sub": "{{unity_player_id}}", // UAS player ID "project_id": "{{unity_project_id}}" // unity project id }}