Created
May 16, 2019 22:53
-
-
Save judah4/8d80d7ed339e9fe95d92244eff922b9b to your computer and use it in GitHub Desktop.
Example Deployments endpoint for SpatialOS game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IdentityModel.Tokens.Jwt; | |
using System.Linq; | |
using System.Net; | |
using System.Security.Claims; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Google.Protobuf.WellKnownTypes; | |
using Improbable.SpatialOS.Deployment.V1Alpha1; | |
using Improbable.SpatialOS.Platform.Common; | |
using Improbable.SpatialOS.PlayerAuth.V2Alpha1; | |
using Microsoft.AspNetCore.Identity; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Logging; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
namespace GameWebsite.Controllers | |
{ | |
[Route("api/[controller]")] | |
public class DeploymentsController : Controller | |
{ | |
private ILogger _log; | |
private IConfiguration _configuration; | |
static PlatformRefreshTokenCredential credentialsWithProvidedToken = new PlatformRefreshTokenCredential("<api-key>"); | |
private readonly DeploymentServiceClient _deploymentServiceClient = DeploymentServiceClient.Create(credentials: credentialsWithProvidedToken); | |
private readonly PlayerAuthServiceClient _playerAuthServiceClient = PlayerAuthServiceClient.Create(credentials: credentialsWithProvidedToken); | |
public DeploymentsController(ILogger<DeploymentsController> log, IConfiguration configuration) | |
{ | |
_log = log; | |
_configuration = configuration; | |
} | |
[HttpGet] | |
public async Task<JContainer> Get() | |
{ | |
try | |
{ | |
StringValues authValue; | |
Request.Headers.TryGetValue("Authorization", out authValue); | |
if (authValue.Count < 1) | |
{ | |
Response.StatusCode = (int) HttpStatusCode.InternalServerError; | |
return new JObject() {{"error", "Not Authorized"}}; | |
} | |
var creds = ParseAuthorizationHeader(authValue); | |
//validate credentials here | |
var deployCreds = new JArray(); | |
var demoDeployments = _deploymentServiceClient.ListDeployments(new ListDeploymentsRequest | |
{ | |
ProjectName = "<project-name>", | |
}).Where(d =>d.Status == Deployment.Types.Status.Running).ToList(); | |
foreach (var deployment in demoDeployments) | |
{ | |
var createLoginTokenResponse = _playerAuthServiceClient.CreateLoginToken( | |
new CreateLoginTokenRequest | |
{ | |
PlayerIdentityToken = creds, | |
DeploymentId = deployment.Id, | |
LifetimeDuration = Duration.FromTimeSpan(new TimeSpan(0, 0, 30, 0)), | |
WorkerType = "UnityClient" //We use unity, replace if using unreal or another engine. | |
}); | |
deployCreds.Add(JObject.FromObject(new | |
{ | |
DeploymentName =deployment.Name, | |
LoginToken = createLoginTokenResponse.LoginToken, | |
Tags = string.Join(",", deployment.Tag), | |
})); | |
} | |
JObject content = new JObject(); | |
content.Add("SpatialDeployments", deployCreds); | |
return content; | |
} | |
catch (Exception e) | |
{ | |
_log.LogError(LoggingEvents.BAD_CREDENTIALS, e, e.Message); | |
//log error | |
Response.StatusCode = (int)HttpStatusCode.BadRequest; | |
return new JObject(); | |
} | |
} | |
private string ParseAuthorizationHeader(StringValues authValue) | |
{ | |
var authHeader = authValue[0].Split(' ').Last(); | |
//bearer, get last id | |
return authHeader; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment