using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Facepunch.Steamworks { public partial class Client : IDisposable { Auth _auth; public Auth Auth { get { if ( _auth == null ) _auth = new Auth(this); return _auth; } } } /// /// Steam authentication statuses /// public enum ClientAuthStatus : int { OK = 0, UserNotConnectedToSteam = 1, NoLicenseOrExpired = 2, VACBanned = 3, LoggedInElseWhere = 4, VACCheckTimedOut = 5, AuthTicketCanceled = 6, AuthTicketInvalidAlreadyUsed = 7, AuthTicketInvalid = 8, PublisherIssuedBan = 9, } public enum ClientStartAuthSessionResult : int { OK = 0, InvalidTicket = 1, DuplicateRequest = 2, InvalidVersion = 3, GameMismatch = 4, ExpiredTicket = 5, ServerNotConnectedToSteam = 6, } public class Auth { public Auth(Client c) { client = c; client.RegisterCallback(OnAuthTicketValidate); } void OnAuthTicketValidate(SteamNative.ValidateAuthTicketResponse_t data) { if (OnAuthChange != null) OnAuthChange(data.SteamID, data.OwnerSteamID, (ClientAuthStatus)data.AuthSessionResponse); } internal Client client; public Action OnAuthChange; public class Ticket : IDisposable { internal Client client; public byte[] Data; public uint Handle; /// /// Cancels a ticket. /// You should cancel your ticket when you close the game or leave a server. /// public void Cancel() { if ( client.IsValid && Handle != 0 ) { client.native.user.CancelAuthTicket( Handle ); Handle = 0; Data = null; } } public void Dispose() { Cancel(); } } /// /// Creates an auth ticket. /// Which you can send to a server to authenticate that you are who you say you are. /// public unsafe Ticket GetAuthSessionTicket() { var data = new byte[1024]; fixed ( byte* b = data ) { uint ticketLength = 0; uint ticket = client.native.user.GetAuthSessionTicket( (IntPtr) b, data.Length, out ticketLength ); if ( ticket == 0 ) return null; return new Ticket() { client = client, Data = data.Take( (int)ticketLength ).ToArray(), Handle = ticket }; } } /// /// Start authorizing a ticket. This user isn't authorized yet. Wait for a call to OnAuthChange. /// public unsafe ClientStartAuthSessionResult StartSession(byte[] data, ulong steamid) { fixed (byte* p = data) { var result = client.native.user.BeginAuthSession((IntPtr)p, data.Length, steamid); return (ClientStartAuthSessionResult)result; } } /// /// Forget this guy. They're no longer in the game. /// public void EndSession(ulong steamid) { client.native.user.EndAuthSession(steamid); } } }