X - hippogamesunity/SimpleSignIn GitHub Wiki
Welcome to Simple X Sign-In wiki!
The asset provides X (Twitter) sign-in with OAuth 2.0 for Android, iOS, Windows, macOS, Universal Windows Platform (UWP) and WebGL apps made with Unity. You can also get access tokens to make REST API calls.
Benefits
- Cross-platform user auth for cross-platform games and apps
- No plugins, no 3rd party libs, no dependencies
- No impact to build size
- Get access tokens to make X API calls
- More security for client-server apps (get an access token on a client, get all user data on a server to avoid tampering)
- SFSafariViewController is used on iOS (required by App Store review)
- Deep linking for Windows (UNITY_STANDALONE_WIN)
Terminology
- Please visit Terminology section
Understanding how it works
-
Generic workflow (for platforms that support deep linking):
- Your app navigates users to X
Authorization Endpointusing a default web browser (embedded webviews are not allowed) - Users perform sign-in using their login and password
- X
Authorization Endpointredirects users toRedirect URI(this can be a deep link when possible) and provides an authorizationcodeto the app (as URI parameters) - The app is activated and obtains
code - The app exchanges
codeforaccess token - The app requests user data with
access token(ID, name, email and other data according access scope defined)
- Your app navigates users to X
-
For Android, iOS, macOS, Windows and Universal Windows Platform (platforms that support deep linking):
Redirect URIis a deep link which activates the app and providescodein URI parameters
-
Middleware flow for WebGL and Editor (platforms don't support deep linking and loopback):
OAuth RedirecttoAuthorization Middlewareis used to temporary savecode- The app obtains
codefromAuthorization Middlewarewith a POST request - Further workflow is the same (exchanging
codeforaccess token, requesting user data)
Preconditions
- For Android, iOS, macOS, Windows and UWP (platforms that support deep linking): COME UP WITH your
Custom URI scheme(orProtocol). It MUST contain the period symbol.and small alphanumeric symbols only (no spaces, no undercores). In my example it issimple.oauth, but it can bejelly.bean(note thatCustom URI schemeis not the same as your actual package name or bundle id). - For Android, iOS, UWP: enable deep linking as described in Unity documentation or as described below.
- For Android: create
AndroidManifest.xmlinsideAssets/Plugins/Android/, SET yourCustom URI schemeinside, like<data android:scheme="simple.oauth" />. You can useAndroidManifestExample.xmlfrom the asset as an example, just copy, rename and edit. AGAIN, DON'T FORGET TO REPLACEsimple.oauthwith yourCustom URI scheme! - For iOS and macOS: navigate to
Player Settings > Other > Configurationand add yourCustom URI schemetoSupported URL schemes. In Xcode, make sure that the URL scheme is added (Register your URL scheme). - For Universal Windows Platform: navigate to
Player Settings > Publishing Settingsand setProtocol(it MUST contain a period symbol, for examplesimple.oauth), then enableInternetClientinCapabilities. - For Windows: navigate to
Player Settingsand enableResolution and Presentation > Force Single Instanceand setOther Settings > Api Compatibility Level = .NET Framework
Setup steps
- Visit X Developer Portal and register
- Create a new app if needed (there may be a default app already, just rename it)
- Go to
Keys and tokens > OAuth 2.0 Client ID and Client Secret, copy and save them - Navigate to
User authentication settings- Set
App permissions(readis recommended) - Set
Type of App(Native Appis recommended) - Set
App info > Callback URI / Redirect URL(addhttps://hippogames.dev/api/oauth/redirectand[Custom URI scheme]://oauth2/x(two slashes, replace the scheme by yours) - Add Website URL (put yours, create a one pager if needed)
- Set
Organization name,Terms of serviceandPrivacy policy(optional, but recommended) - Press
Save
- Set
- Navigate to Products section and enable
X API v2 - Return to Unity and configure
Resources/XAuthSettings.asset- Set
Client IdandClient Secretobtained in step 3 - Set
Custom Uri Scheme(refer toPreconditions) - Make sure
tweet.readandusers.readadded toAccess Scopes
- Set
Checklist
-
Custom URI schemeis picked, and it has a different value thansimple.oauth -
Custom URI schemeis set in 3 places: [1] X Developer Portal (Callback URI / Redirect URL), [2] Resources/XAuthSettings.asset, [3] your application manifest (AndroidManifest.xml for Android,Supported URL schemesfor iOS,Protocolfor UWP) -
Resources/XAuthSettings.assetcontains valid settings
Usage
- Check our
Examplescene and C# code ofExample.cs - Create a new instance of
XAuth, passXAuthSettingsto the constructor (optional) - Call
XAuth.SignInorXAuth.GetAccessToken - Create callbacks
OnSignInorOnGetAccessToken - Build and test
- Write a review on the Asset Store :)
Best practices
- Call
XAuth.SignInwithcaching: trueto return cachedUserInfo - Call
XAuth.SignInwithcaching: falseto requestUserInfofrom X - Call
X.GetAccessTokeninstead ofX.SignInif you need an access token only (if you need it for further API calls) - You can use
X.SavedAuthto getTokenResponseorUserInfo(don't forget to check all values for null) - Call
X.SignOutwhen 'Sign out` button is pressed (optional) - Disable debug logs for production by setting
X.DebugLog = false
Next steps (optional)
- You can add extra access scopes in
Resources/XAuthSettings.asset - If you have a backend (server), send
TokenResponseto it (to avoid tapmering user data when sending from clients to your server) - For Windows, check Settings for Windows
- For WebGL, consider deploying your own
Authorization Middleware - You can use this asset with
asyncmethods, just refer toXAuthAsync.csfor examples
Can I trust Authorization Middleware? Is it secure to use a 3rd party service?
Authorization Middlewarecan't exchangecodeforaccess tokenwithout knowingClient Secret. Only the app itself can exchangecodeforaccess token.- It's recommended to deploy your own trusted
Authorization Middlewareto handle sensitive data. Please refer to Authorization Middleware article.
Notes
- Please refer to User data disclosure
- Don't use default credentials that come with the asset in production, they are for test purposes only and can be disabled/blocked
- Check Manual cancellation if needed
Known issues
- Please visit Common issues section
- X may temporary block API calls if you make too many of them in a short period of time.
- Refreshing access tokens doesn't work at the moment (community). Also note that
offline.accessscope is required to receive refresh tokens. - JWT are not supported by X.
- You can't get user emails with X API v2 (community), even if
Request email from usersis set inApp permissions.