using System; using System.Collections.Generic; using Unity.Services.Authentication.Editor.Models; using Unity.Services.Authentication.Utilities; using UnityEngine; using ILogger = Unity.Services.Authentication.Utilities.ILogger; namespace Unity.Services.Authentication.Editor { interface IAuthenticationAdminNetworkClient { IWebRequest TokenExchange(string token); IWebRequest GetDefaultIdDomain(string token); IWebRequest CreateIdProvider(CreateIdProviderRequest body, string idDomain, string token); IWebRequest ListIdProvider(string idDomain, string token); IWebRequest UpdateIdProvider(UpdateIdProviderRequest body, string idDomain, string type, string token); IWebRequest EnableIdProvider(string idDomain, string type, string token); IWebRequest DisableIdProvider(string idDomain, string type, string token); IWebRequest DeleteIdProvider(string idDomain, string type, string token); } class AuthenticationAdminNetworkClient : IAuthenticationAdminNetworkClient { const string k_ServicesGatewayStem = "/api/player-identity/v1/organizations/"; const string k_GetDefaultIdDomainStem = "/iddomains/default"; const string k_TokenExchangeStem = "/api/auth/v1/genesis-token-exchange/unity"; readonly string m_ServicesGatewayHost; readonly string m_GetDefaultIdDomainUrl; readonly string m_TokenExchangeUrl; readonly string m_OrganizationId; readonly string m_ProjectId; readonly INetworkingUtilities m_NetworkClient; readonly Dictionary m_CommonPlayerIdentityHeaders; internal AuthenticationAdminNetworkClient(string servicesGatewayHost, string organizationId, string projectId, INetworkingUtilities networkClient, ILogger logger) { m_ServicesGatewayHost = servicesGatewayHost; m_OrganizationId = organizationId; m_ProjectId = projectId; m_GetDefaultIdDomainUrl = servicesGatewayHost + k_ServicesGatewayStem + organizationId + k_GetDefaultIdDomainStem; m_TokenExchangeUrl = servicesGatewayHost + k_TokenExchangeStem; m_NetworkClient = networkClient; m_CommonPlayerIdentityHeaders = new Dictionary { ["ProjectId"] = projectId, // The Error-Version header enables RFC7807HttpError error responses ["Error-Version"] = "v1" }; } public IWebRequest GetDefaultIdDomain(string token) { return m_NetworkClient.Get(m_GetDefaultIdDomainUrl, addTokenHeader(m_CommonPlayerIdentityHeaders, token)); } public IWebRequest TokenExchange(string token) { var body = new TokenExchangeRequest(); body.Token = token; return m_NetworkClient.PostJson(m_TokenExchangeUrl, body); } public IWebRequest CreateIdProvider(CreateIdProviderRequest body, string idDomain, string token) { return m_NetworkClient.PostJson(CreateIdProviderUrl(idDomain), body, addTokenHeader(m_CommonPlayerIdentityHeaders, token)); } public IWebRequest ListIdProvider(string idDomain, string token) { return m_NetworkClient.Get(ListIdProviderUrl(idDomain), addTokenHeader(m_CommonPlayerIdentityHeaders, token)); } public IWebRequest UpdateIdProvider(UpdateIdProviderRequest body, string idDomain, string type, string token) { return m_NetworkClient.Put(UpdateIdProviderUrl(idDomain, type), body, addTokenHeader(m_CommonPlayerIdentityHeaders, token)); } public IWebRequest EnableIdProvider(string idDomain, string type, string token) { return m_NetworkClient.Post(EnableIdProviderUrl(idDomain, type), addJsonHeader(addTokenHeader(m_CommonPlayerIdentityHeaders, token))); } public IWebRequest DisableIdProvider(string idDomain, string type, string token) { return m_NetworkClient.Post(DisableIdProviderUrl(idDomain, type), addJsonHeader(addTokenHeader(m_CommonPlayerIdentityHeaders, token))); } public IWebRequest DeleteIdProvider(string idDomain, string type, string token) { return m_NetworkClient.Delete(DeleteIdProviderUrl(idDomain, type), addTokenHeader(m_CommonPlayerIdentityHeaders, token)); } Dictionary addTokenHeader(Dictionary d, string token) { var headers = new Dictionary(d); headers.Add("Authorization", "Bearer " + token); return headers; } Dictionary addJsonHeader(Dictionary d) { var headers = new Dictionary(d); headers.Add("Content-Type", "application/json"); return headers; } string CreateIdProviderUrl(string idDomain) { return m_ServicesGatewayHost + k_ServicesGatewayStem + m_OrganizationId + "/iddomains/" + idDomain + "/idps"; } string ListIdProviderUrl(string idDomain) { return m_ServicesGatewayHost + k_ServicesGatewayStem + m_OrganizationId + "/iddomains/" + idDomain + "/idps"; } string UpdateIdProviderUrl(string idDomain, string type) { return m_ServicesGatewayHost + k_ServicesGatewayStem + m_OrganizationId + "/iddomains/" + idDomain + "/idps/" + type; } string DeleteIdProviderUrl(string idDomain, string type) { return m_ServicesGatewayHost + k_ServicesGatewayStem + m_OrganizationId + "/iddomains/" + idDomain + "/idps/" + type; } string EnableIdProviderUrl(string idDomain, string type) { return m_ServicesGatewayHost + k_ServicesGatewayStem + m_OrganizationId + "/iddomains/" + idDomain + "/idps/" + type + "/enable"; } string DisableIdProviderUrl(string idDomain, string type) { return m_ServicesGatewayHost + k_ServicesGatewayStem + m_OrganizationId + "/iddomains/" + idDomain + "/idps/" + type + "/disable"; } } }