This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPlayFabAuthImpl.cs
76 lines (69 loc) · 2.87 KB
/
PlayFabAuthImpl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using Improbable.OnlineServices.Proto.Auth.PlayFab;
using Grpc.Core;
using PlayFab;
using PlayFab.ServerModels;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using Improbable.OnlineServices.Common.Analytics;
using Serilog;
using Improbable.SpatialOS.PlayerAuth.V2Alpha1;
namespace PlayFabAuth
{
public class PlayFabAuthImpl : AuthService.AuthServiceBase
{
private readonly string _project;
private readonly PlayerAuthServiceClient _authServiceClient;
private readonly AnalyticsSenderClassWrapper _analytics;
public PlayFabAuthImpl(string project, PlayerAuthServiceClient authServiceClient, IAnalyticsSender analytics = null)
{
_project = project;
_authServiceClient = authServiceClient;
_analytics = (analytics ?? new NullAnalyticsSender()).WithEventClass("authentication");
}
public override Task<ExchangePlayFabTokenResponse> ExchangePlayFabToken(ExchangePlayFabTokenRequest request,
ServerCallContext context)
{
UserAccountInfo userInfo;
try
{
var authenticateTask = PlayFabServerAPI.AuthenticateSessionTicketAsync(
new AuthenticateSessionTicketRequest
{
SessionTicket = request.PlayfabToken,
});
authenticateTask.Wait();
userInfo = authenticateTask.GetAwaiter().GetResult().Result.UserInfo;
}
catch (Exception e)
{
Log.Error(e, "Failed to authenticate PlayFab ticket");
throw new RpcException(new Status(StatusCode.InvalidArgument, "Failed to authenticate PlayFab ticket"));
}
try
{
var playerIdentityToken = _authServiceClient.CreatePlayerIdentityToken(
new CreatePlayerIdentityTokenRequest
{
PlayerIdentifier = userInfo.PlayFabId,
Provider = "playfab",
ProjectName = _project
}
);
_analytics.Send("player_token_exchanged", new Dictionary<string, string>
{
{ "provider", "PlayFab" },
{ "spatialProjectId", _project }
}, userInfo.PlayFabId);
return Task.FromResult(new ExchangePlayFabTokenResponse
{ PlayerIdentityToken = playerIdentityToken.PlayerIdentityToken });
}
catch (Exception e)
{
Log.Error(e, $"Failed to create player identity token for {userInfo.PlayFabId}");
throw new RpcException(new Status(StatusCode.Internal,
$"Failed to create player identity token for {userInfo.PlayFabId}"));
}
}
}
}