From e21c42e8a2167c059b9a39e4ed66efdf5f9bcbfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Straubmeier?= Date: Fri, 9 Jun 2023 08:45:14 +0200 Subject: [PATCH] Add GetAuthTicketForWebApiAsync --- Facepunch.Steamworks.Test/UserTest.cs | 11 ++++ Facepunch.Steamworks/SteamUser.cs | 74 ++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/Facepunch.Steamworks.Test/UserTest.cs b/Facepunch.Steamworks.Test/UserTest.cs index d7d98db..2d0065b 100644 --- a/Facepunch.Steamworks.Test/UserTest.cs +++ b/Facepunch.Steamworks.Test/UserTest.cs @@ -86,6 +86,17 @@ namespace Steamworks Console.WriteLine( $"ticket.Data: { string.Join( "", ticket.Data.Select( x => x.ToString( "x" ) ) ) }" ); } + [TestMethod] + public async Task AuthTicketForWebApiAsync() + { + var ticket = await SteamUser.GetAuthTicketForWebApiAsync( "Test" ); + + Assert.AreNotEqual( 0, ticket.Handle ); + Assert.AreNotEqual( 0, ticket.Data.Length ); + Console.WriteLine( $"ticket.Handle: {ticket.Handle}" ); + Console.WriteLine( $"ticket.Data: { string.Join( "", ticket.Data.Select( x => x.ToString( "x" ) ) ) }" ); + } + [TestMethod] public void SteamLevel() { diff --git a/Facepunch.Steamworks/SteamUser.cs b/Facepunch.Steamworks/SteamUser.cs index d0af833..573959d 100644 --- a/Facepunch.Steamworks/SteamUser.cs +++ b/Facepunch.Steamworks/SteamUser.cs @@ -43,6 +43,7 @@ namespace Steamworks Dispatch.Install( x => OnMicroTxnAuthorizationResponse?.Invoke( x.AppID, x.OrderID, x.Authorized != 0 ) ); Dispatch.Install( x => OnGameWebCallback?.Invoke( x.URLUTF8() ) ); Dispatch.Install( x => OnGetAuthSessionTicketResponse?.Invoke( x ) ); + Dispatch.Install( x => OnGetTicketForWebApiResponse?.Invoke( x ) ); Dispatch.Install( x => OnDurationControl?.Invoke( new DurationControl { _inner = x } ) ); } @@ -89,10 +90,15 @@ namespace Steamworks public static event Action OnValidateAuthTicketResponse; /// - /// Used internally for . + /// Used internally for . /// internal static event Action OnGetAuthSessionTicketResponse; + /// + /// Used internally for . + /// + internal static event Action OnGetTicketForWebApiResponse; + /// /// Invoked when a user has responded to a microtransaction authorization request. /// ( appid, orderid, user authorized ) @@ -375,6 +381,72 @@ namespace Steamworks } } + /// + /// Retrieve an authentication ticket to be sent to the entity who wishes to authenticate you. + /// + private static unsafe AuthTicket GetAuthTicketForWebApi( string identity ) + { + uint ticket = Internal.GetAuthTicketForWebApi( identity ); + + if ( ticket == 0 ) + return null; + + return new AuthTicket() + { + Handle = ticket + }; + } + + /// + /// Retrieve a authentication ticket to be sent to the entity who wishes to authenticate you. + /// This waits for a positive response from the backend before returning the ticket. This means + /// the ticket is definitely ready to go as soon as it returns. Will return if the callback + /// times out or returns negatively. + /// + public static async Task GetAuthTicketForWebApiAsync( string identity, double timeoutSeconds = 10.0f ) + { + var result = Result.Pending; + AuthTicket ticket = null; + var stopwatch = Stopwatch.StartNew(); + + void f( GetTicketForWebApiResponse_t t ) + { + if ( t.AuthTicket != ticket.Handle ) return; + result = t.Result; + ticket.Data = t.GubTicket; + } + + OnGetTicketForWebApiResponse += f; + + try + { + ticket = GetAuthTicketForWebApi( identity ); + if ( ticket == null ) + return null; + + while ( result == Result.Pending ) + { + await Task.Delay( 10 ); + + if ( stopwatch.Elapsed.TotalSeconds > timeoutSeconds ) + { + ticket.Cancel(); + return null; + } + } + + if ( result == Result.OK ) + return ticket; + + ticket.Cancel(); + return null; + } + finally + { + OnGetTicketForWebApiResponse -= f; + } + } + public static unsafe BeginAuthResult BeginAuthSession( byte[] ticketData, SteamId steamid ) { fixed ( byte* ptr = ticketData )