Yandex - hippogamesunity/SimpleSignIn GitHub Wiki
Welcome to Simple Yandex Sign-In wiki!
The asset provides Yandex 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.
Notice
This asset is based on web OAuth, which means users are redirected to Yandex Oauth Endpoint with a default web browser. Benefits of this apprcoach are simple setup and minimalism. For native integrations, use LoginSDK avaliable for some platforms (it has much more complicated setup and doesn't support specific platforms).
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 for integration with other Yandex services
- More security for client-server apps (get an access token on a client, get all user data on a server to avoid tampering)
- JSON Web Tokens (JWT) validation
- 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 Yandex
Authorization Endpointusing a default web browser (embedded webviews are not allowed) - Users perform sign-in using their login and password
- Yandex
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 Yandex
-
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
-
Loopback flow for Editor and Windows (optional):
- This flow is optional for Windows (the generic workflow is used by default)
Redirect URIishttp://localhost:PORT/with a random unused port- The app listens to localhost using
System.Net.HttpListener - The app obtains
codeand asks a user to close the browser tab and to return to the app - Further workflow is the same (exchanging
codeforaccess token, requesting user data)
-
Middleware flow for WebGL (the platform doesn'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 - For Editor: Set
Allow downloads over HTTP = Always allowed(Unity 2022+)
Setup steps
- Visit OAuth Yandex - Яндекс
- Navigate to
Applicationsand create a new app, select WEB type (our asset is based on web OAuth, use Yandex SDK for native integrations) - Add
simple.oauth:/oauth2/yandex(USE YOUR custom URI scheme from Preconditions, but leave the same pathoauth2/yandex) toRedirect URI, optionally addhttp://localhost:53100/toRedirect URIfor debugging in Unity andhttps://hippogames.dev/api/oauth/redirectfor WebGL builds - Copy
Client IdandClient Secret(optionally) - Return to Unity and configure
Resources/YandexAuthSettings.asset- Set
Client IdandClient Secret(optionally, insecure) - Set
Client Uri Schemefrom Preconditions - Check
Access Scopes
- Set
Checklist
-
Custom URI schemeis picked, and it has a different value thansimple.oauth -
Custom URI schemeis set in 3 places: [1] OAuth Yandex (Redirect URI), [2] Resources/YandexAuthSettings.asset, [3] your application manifest (AndroidManifest.xml for Android,Supported URL schemesfor iOS,Protocolfor UWP) -
Client Idis copied to Resources/YandexAuthSettings.asset
Usage
- Check our
Examplescene and C# code ofExample.cs - Create an instance of
YandexAuth - Call
YandexAuth.SignInorYandexAuth.GetAccessToken - Create
OnSignInorOnGetAccessTokencallbacks - Build and test
- Write a review on the Asset Store :)
Best practices
- Call
YandextAuth.SignInwithcaching: trueto return cachedUserInfo - Call
YandextAuth.SignInwithcaching: falseto requestUserInfofrom Yandex - Call
YandexAuth.GetAccessTokeninstead ofYandexAuth.SignInif you need an access token only (and don't needUserInfo) - You can use
YandexAuth.SavedAuthto getTokenResponseorUserInfo(don't forget to check all values for null) - Call
YandexAuth.SignOutwhen 'Sign out` button is pressed - Don't store
Client SecretinYandexAuthSettings(leave it empty, and don't revoke access tokens) - If you want to allow changing user accounts without revoking access tokens, ask users to revoke access tokens manually from Yandex ID
Next steps (optional)
- You can add extra access scopes in
Resources/YandexAuthSettings.asset - For Editor and Windows (optional), you can modify
StandaloneTemplate.html(used by the loopback flow) to edit the message "Success! Please close the browser tab and return to the app." - For Windows, check Settings for Windows
- For WebGL, consider deploying your own
Authorization Middleware - You can use this asset with
asyncmethods, just refer toYandexAuth.csfor examples
Can I trust Authorization Middleware? Is it secure to use a 3rd party service?
Authorization Middlewarecan't exchangecodeforaccess tokenwithout knowing bothclient secretandcode verifier. 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
- Yandex docs are avaialble for RU and EN languages
- You can use
https://oauth.yandex.ruandhttps://oauth.yandex.comendpoints - 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
- For token revocation, it's required to specify
device_idanddevice_namewhen creating OAuth request (SystemInfo.deviceUniqueIdentifierandSystemInfo.deviceNameare unsupported for WebGL)
Known issues
- Please visit Common issues section