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

Understanding how it works

  • Generic workflow (for platforms that support deep linking):

    • Your app navigates users to Yandex Authorization Endpoint using a default web browser (embedded webviews are not allowed)
    • Users perform sign-in using their login and password
    • Yandex Authorization Endpoint redirects users to Redirect URI (this can be a deep link when possible) and provides an authorization code to the app (as URI parameters)
    • The app is activated and obtains code
    • The app exchanges code for access token
    • The app requests user data with access token (ID, name, email and other data according access scope defined)
  • For Android, iOS, macOS, Windows and Universal Windows Platform (platforms that support deep linking):

    • Redirect URI is a deep link which activates the app and provides code in URI parameters
  • Loopback flow for Editor and Windows (optional):

    • This flow is optional for Windows (the generic workflow is used by default)
    • Redirect URI is http://localhost:PORT/ with a random unused port
    • The app listens to localhost using System.Net.HttpListener
    • The app obtains code and asks a user to close the browser tab and to return to the app
    • Further workflow is the same (exchanging code for access token, requesting user data)
  • Middleware flow for WebGL (the platform doesn't support deep linking and loopback):

    • OAuth Redirect to Authorization Middleware is used to temporary save code
    • The app obtains code from Authorization Middleware with a POST request
    • Further workflow is the same (exchanging code for access token, requesting user data)

Preconditions

  • For Android, iOS, macOS, Windows and UWP (platforms that support deep linking): COME UP WITH your Custom URI scheme (or Protocol). It MUST contain the period symbol . and small alphanumeric symbols only (no spaces, no undercores). In my example it is simple.oauth, but it can be jelly.bean (note that Custom URI scheme is 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.xml inside Assets/Plugins/Android/, SET your Custom URI scheme inside, like <data android:scheme="simple.oauth" />. You can use AndroidManifestExample.xml from the asset as an example, just copy, rename and edit. AGAIN, DON'T FORGET TO REPLACE simple.oauth with your Custom URI scheme!
  • For iOS and macOS: navigate to Player Settings > Other > Configuration and add your Custom URI scheme to Supported 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 Settings and set Protocol (it MUST contain a period symbol, for example simple.oauth), then enable InternetClient in Capabilities.
  • For Windows: navigate to Player Settings and enable Resolution and Presentation > Force Single Instance and set Other Settings > Api Compatibility Level = .NET Framework
  • For Editor: Set Allow downloads over HTTP = Always allowed (Unity 2022+)

Setup steps

  1. Visit OAuth Yandex - Яндекс
  2. Navigate to Applications and create a new app, select WEB type (our asset is based on web OAuth, use Yandex SDK for native integrations)
  3. Add simple.oauth:/oauth2/yandex (USE YOUR custom URI scheme from Preconditions, but leave the same path oauth2/yandex) to Redirect URI, optionally add http://localhost:53100/ to Redirect URI for debugging in Unity and https://hippogames.dev/api/oauth/redirect for WebGL builds
  4. Copy Client Id and Client Secret (optionally)
  5. Return to Unity and configure Resources/YandexAuthSettings.asset
    • Set Client Id and Client Secret (optionally, insecure)
    • Set Client Uri Scheme from Preconditions
    • Check Access Scopes

Checklist

  • Custom URI scheme is picked, and it has a different value than simple.oauth
  • Custom URI scheme is set in 3 places: [1] OAuth Yandex (Redirect URI), [2] Resources/YandexAuthSettings.asset, [3] your application manifest (AndroidManifest.xml for Android, Supported URL schemes for iOS, Protocol for UWP)
  • Client Id is copied to Resources/YandexAuthSettings.asset

Usage

  1. Check our Example scene and C# code of Example.cs
  2. Create an instance of YandexAuth
  3. Call YandexAuth.SignIn or YandexAuth.GetAccessToken
  4. Create OnSignIn or OnGetAccessToken callbacks
  5. Build and test
  6. Write a review on the Asset Store :)

Best practices

  • Call YandextAuth.SignIn with caching: true to return cached UserInfo
  • Call YandextAuth.SignIn with caching: false to request UserInfo from Yandex
  • Call YandexAuth.GetAccessToken instead of YandexAuth.SignIn if you need an access token only (and don't need UserInfo)
  • You can use YandexAuth.SavedAuth to get TokenResponse or UserInfo (don't forget to check all values for null)
  • Call YandexAuth.SignOut when 'Sign out` button is pressed
  • Don't store Client Secret in YandexAuthSettings (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 async methods, just refer to YandexAuth.cs for examples

Can I trust Authorization Middleware? Is it secure to use a 3rd party service?

  • Authorization Middleware can't exchange code for access token without knowing both client secret and code verifier. Only the app itself can exchange code for access token.
  • It's recommended to deploy your own trusted Authorization Middleware to 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.ru and https://oauth.yandex.com endpoints
  • 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_id and device_name when creating OAuth request (SystemInfo.deviceUniqueIdentifier and SystemInfo.deviceName are unsupported for WebGL)

Known issues

Support

Links