'Authentication for Patreon' Documentation

Welcome to the setup guide for 'Authentication for Patreon'. This tool integrates OAuth services into Unity projects, specifically designed to work with Patreon's API version 2. This guide provides straightforward, step-by-step instructions for both initial setup and advanced configurations.

Table of Contents

  1. Extracting the Backend.zip
  2. Locating the Extracted Files
  3. Unity Configuration
  4. Following the Setup Instructions for the Backend
  5. Script References
  6. Support

1. Extracting the Backend.zip

  1. Open your Unity Project.
  2. Locate the Backend_Source.zip file in /Assets/MSCreativeTech/AuthenticationForPatreon/_Backend/ directory.
  3. Extract the contents of the Backend folder from Backend_Source.zip to your desktop.

To verify correct extraction, in Unity go to Tools > Authentication for Patreon > Setup Assistant and click "Verify Backend Folder".

2. Locating the Extracted Files

Find the extracted Backend folder on your desktop. Key files include:

3. Unity Configuration

  1. Locating the Patreon Integration Script: In the Demo scene, locate the PatreonLogin game object which has the Patreon Integration script attached.
  2. Configuring the Patreon Integration Script: Open the AES Encryption window in the Unity Editor by going to Authentication for Patreon > AES Encryption. In the editor window, perform the following steps:
  3. Setting Up Patreon Tier Configuration: Run Desktop/Backend/HelperScripts/CheckPatreonTiersGUI.exe to retrieve the Patreon tier IDs. Insert these IDs into the respective fields in the Patreon Tier Configuration list of the Patreon Integration script.
  4. Setting Up Network Address: Paste your Backend Server's IP Address into the Backend Server IP field of the Patreon Integration script. When you later use HTTPS, then check the Use HTTPS checkbox and paste the Backend Server's domain into the Server Domain field.
  5. Setting Up Debug Logs: Check the Enable Debug Logs checkbox in the Patreon Integration script, which will enable all the debug log messages of the script.
  6. Running the Demo Scene: Run the demo scene in Unity and test the Patreon login functionality by clicking the Login with Patreon button.
  7. Testing the Logout Functionality: Press the Logout button, which will reset the tier status and member ID, and will delete the saved player prefs, so you won't authenticate via timecode (if offline authentication is enabled). Note: You can also use the Delete Saved Offline Data option in the editor (bottom of the Patreon Integration script's component menu) to delete those saved player prefs.
Note: Offline Authentication

The Authentication for Patreon system supports offline authentication, allowing users to access the app for a specified number of days without an internet connection. This feature can be enabled and configured in the Patreon Integration script settings.

4. Following the Setup Instructions for the Backend

  1. Open the Instructions File: Navigate to the Backend folder on your desktop. Find and open the Instructions.html file, which contains detailed instructions on how to set up the backend script.
  2. Complete the Setup: Follow the steps provided in Instructions.html to complete the setup for Authentication for Patreon in your Unity project.

5. Script References

PatreonIntegration.cs

Namespace: MSCreativeTech.AuthenticationForPatreon

Description: Handles the integration between your Unity application and Patreon.

Properties:

- Instance: Singleton instance of the class.
- _isAuthenticated: (Private, Serialized) Flag indicating if the user is authenticated.
- IsAuthenticated: (Public) Property to get or privately set _isAuthenticated.
- MemberId: (Public) The ID of the authenticated Patreon member.
- patreonAuthCode: (Private) Authorization code received from Patreon.
- isAuthenticationInProgress: (Private) Flag for authentication in progress.
- encryptedClientID, encryptedClientIDAesKey, encryptedClientIDAesIV, patreonClientID: (Private, Serialized) Encryption and decryption details for the client ID.
- patreonTiers: (Private, Serialized) List of Patreon tiers.
- _tierIndex, tierIndex: (Private) Current Patreon tier index of the user.
- logoutScene: (Private, Serialized) Scene to load upon logout.
- _memberId: (Private) Member details.
- allowAccessWithNoTier: (Serialized) Allow access even if the user doesn't belong to any valid tier.
- enableOfflineAuthentication: (Serialized) Toggle to enable or disable offline authentication.
- isAuthenticatedViaTimecode: (Private) Flag indicating if the user is authenticated via offline timecode.
- enableRateLimiting: (Serialized) Enable or disable rate limiting for the authentication process.
- maxRequestsPerHour: (Serialized) The maximum number of authentication requests allowed per hour.
- enableDebugLogs: (Serialized) Toggle to enable or disable debug logs for the script.
- enableErrorCodes: (Serialized) Enable or disable error codes display.
- authenticationTimeout: (Serialized) The timeout value for the authentication process, in seconds.
- requestTimeout: (Serialized) The timeout value for web requests, in seconds.
- allowedOfflineDays: (Serialized) Number of days offline authentication is allowed.
- backendServerIP, serverDomain: (Serialized) Backend server configuration details.
- useHTTPS: (Serialized) Flag to toggle HTTPS communication.
- successfulAuthHTML: (Serialized) HTML content to display when authentication is successful.
- errorMessageText, statusText: (Serialized) References to UI elements for displaying messages.
- loginButton, logoutButton: (Serialized) References to UI elements for login/logout actions.
- onTierStatusUpdate: (Serialized) Event triggered on tier status update.

Methods:

- Awake(): Initializes the script, validates configurations, and sets up offline authentication.
- Start(): Sets up server details and offline data.
- Update(): Monitors main thread actions queue.
- OnOfflineAuthentication(): Handles actions after successful offline authentication.
- LogCurrentKeyValues(): Logs the current PlayerPrefs key-values.
- Logout(): Logs the user out and clears offline data.
- LoadOfflineData(): Loads saved token and other data.
- SaveOfflineData(string accessToken): Saves offline authentication data.
- GenerateHardwareId(): Generates a hardware ID for offline authentication.
- StartAuthenticationProcess(): Initiates the web-based Patreon authentication.
- StartWebServer(): Starts the built-in web server for authentication callback.
- GetTiers(): Initiates the process to fetch Patreon tiers.
- GetAccessTokenFromBackend(): Coroutine to fetch the access token.
- OnRequestReceived(IAsyncResult): Handles received HTTP request for authentication.
- FetchMembershipInfo(string): Fetches Patreon membership information.
- OnMembershipInfoReceived(AsyncOperation): Handles received membership info.
- HandleNoMatchingTiers(): Handles the scenario when no matching tiers are found.
- HandleNoValidTierData(): Handles the scenario when no valid tier data is found.
- OnUserAuthenticated(): Handles actions after successful online authentication.
- CloseWebServer(): Stops and closes the web server.
- GetNetworkTime(): Fetches the network time.
- SwapEndianness(ulong): Utility to swap endianness of a ulong value.
- IsValidBase64String(): Checks if a given string is a valid Base64 string.
- EncryptDate(string dateToEncrypt, string offlineEncryptionKey, string offlineEncryptionIV): Encrypts a date using AES encryption.
- DecryptDate(string encryptedDate, string offlineEncryptionKey, string offlineEncryptionIV): Decrypts an AES-encrypted date.
- DecryptClientID(string encryptedClientID): Decrypts an AES-encrypted client ID.
- Log(string message, int errorCode = 0): Logs messages and error codes.
- ResetErrorCodeText(): Resets the text for error messages.
- SetKeyAndIV(string newKey, string newIV): Sets new encryption key and IV for offline authentication.
- AuthenticationTimeoutHandler(): Coroutine for handling authentication timeout.

Data Classes:

- PatreonTokenResponse: A record for the response from Patreon when requesting a token.
- PatreonResponse: A record for the general response from Patreon.
- PatreonData: A record for Patreon data.
- PatreonRelationships: A record for Patreon relationships.
- PatreonMemberships: A record for Patreon memberships.
- PatreonMemberData: A record for Patreon member data.
- PatreonTier: A record for Patreon tiers.

Events:

onTierStatusUpdate(): Invoked when the user's tier status updates. In the DemoScene, this event calls CheckTier(int sentTier) in the TierCheck script, which triggers a function to load the EmptySceneToLoad.

TierCheck.cs

Namespace: MSCreativeTech.AuthenticationForPatreon

Description: Checks the Patreon tier of an authenticated user and performs actions accordingly.

Properties:

- patreonIntegration (Private): Reference to the PatreonIntegration script.
- errorMessageText (Private): Reference to a UI element for displaying error messages.
- sceneToLoad (Private): Name of the scene to load upon successful tier check.
- minimumRequiredTier (Private): Minimum required Patreon tier for access.
- enableDebugLogs: (Serialized) Toggle to enable or disable debug logs for the script.
- enableErrorCodes: (Serialized) Enable or disable error codes display.
- loadSceneOnTierReceive: (Serialized) Flag to load the specified scene when tier is received.

Methods:

- CheckTier(int sentTier): Checks the Patreon tier of the user.
- CheckMemberID(string sentMemberID): Checks the Patreon member ID of the user.
- LoadScene(): Loads the specified Unity scene.
- GetSavedTier(): Retrieves the saved Patreon tier from PlayerPrefs.
- Log(string message, int errorCode = 0): Logs messages and error codes.

Events:

onTierMatch(): Invoked when the Patreon tier of the user matches the required tier.

6. Support

For questions or issues, please contact mscreativetech@proton.me.