Authentication
Access the Steam authentication system with Heathen's Steam API

Like what your seeing?

Consider supporting us as a GitHub Sponsor and get instant access to all our Unity assets, exclusive tools and assets, escalated support and issue tracking and our gratitude. These articles are made possible by our GitHub Sponsors ... become a sponsor today!

Introduction

using API = HeathenEngineering.SteamworksIntegration.API;
public static class API.Authentication
The Authentication has both client and server interfaces that are identical. Heathen's Steam API wrap's these in a single call which will call the appropriate client or server interface for you based on the build type in Unity.

What can it do?

The Authentication interface can be used to generate and validate session tickets. This is most commonly used with Steam Game Server but can also be used to for inventory verification or other similar verified processes.
Authentication Session
Heathen KB
Authentication Ticket
Heathen KB

Fields and Attributes

Active Tickets

Tickets this player has sent out.
public static List<AuthenticationTicket> ActiveTickets;

Active Sessions

Sessions the player has started.
public static List<AuthenticationSession> ActiveSessions;

Methods

IsAuthTicketValid

public static bool IsAuthTicketValid(AuthenticationTicket ticket);
Determines if the provided ticket handle is valid

EncodedAuthTicket

public static string EncodedAuthTicket(AuthenticationTicket ticket);
Encodes a ticket to hex string format
This is most commonly used with web calls such as https://partner.steamgames.com/doc/webapi/ISteamUserAuth#AuthenticateUserTicket

GetAuthSessionTicket

public static void GetAuthSessionTicket(
Action<AuthenticationTicket, bool> callback);
The callback delegate should be in the form of
void CallbackHandler(AuthenticationTicket result, bool IOError);
Requests a new Auth Session Ticket

CancelAuthTicket

public static void CancelAuthTicket(AuthenticationTicket ticket);
Cancels the auth ticket rather its client or server based.

BeginAuthSession

public static EBeginAuthSessionResult BeginAuthSession(byte[] authTicket,
CSteamID user,
Action<AuthenticationSession> callback);
The callback deligate should be in the form of
void CallbackHandler(AuthenticationSession result);
Starts an authorization session with the indicated user given the applied auth ticket

EndAuthSession

public static void EndAuthSession(CSteamID user);
Ends the auth session with the indicated user if any

UserHasLicenseForApp

public static EUserHasLicenseForAppResult UserHasLicenseForApp(CSteamID user,
AppId_t appId);
Checks if the user owns a specific piece of Downloadable Content (DLC).

EndAllSessions

public static void EndAllSessions();
Ends all tracked sessions

CancelAllTickets

public static void CancelAllTickets();
Cancels all tracked tickets

How To

Get a new ticket

To authenticate the user who needs to be authenticated will first get a ticket and then send that data to the entity that will be doing the authentication ... so typically a client gets a ticket and sends it to a server.
API.Authentication.GetAuthSessionTicket((result, IOError) =>
{
if(!IOError)
{
//result.Data is your ticket data, use it well
}
});
The act of sending your ticket data to your server or the other client that wishes to authenticate you is up to your networking solution. Rather that is Mirror, MLAPI, Forge, etc. you will need to send the ticket.Data to that user.

Begin a session

When ticket data is recieved you need to begin the auth session with that user and confirm the status of that session.
var result = API.Authentication.BeginAuthSession(ticket, user, (responce) =>
{
// responce is an AuthenticationSession
// You can use this value to understand the state of the session
});
if(result != EBeginAuthSessionResult.k_EBeginAuthSessionResultOK)
{
// The ticket is not valid,
// result is and enum of type EBeginAuthSessionResult
// its value indicates what is wrong
}
You can find details on the possible values for the EBeginAuthSessionResult result here:
steam_api.h (Steamworks Documentation)

Ending it

Rather from the authenticated user or the authenticating system a session should be ended when its no longer needed.
To cancel a ticket you sent out
API.Authentication.CencelAuthTicket(ticket);
To cancel all tickets you sent out
API.Authentication.CancelAllTickets();
To end a session you started
API.Authentication.EndAuthSession(user);
To end all sessions you started
API.Authentication.EndAllSessions();

Review open tickets and sessions

Your systems should maintain track of its tickets and sessions however the Authenticaitton API does keep a record of all active sessions and tickets.
Do not add or remove tickets or sessions manually
the API.Authentication.ActiveTickets and API.Authenticaiton.ActiveSessions lists can be used to iterate over the active tickets and sessions as required.