VK - hippogamesunity/SimpleSignIn GitHub Wiki
Welcome to Simple VK Sign-In wiki!
The asset provides VK (Vkontakte) sign-in with OAuth 2.0 for Android, iOS, Windows, Mac, Universal Windows Platform (UWP) and WebGL apps made with Unity.
Benefits
- Cross-platform user auth for cross-platform games and apps
- No plugins, no 3rd party libs, no dependencies
- No impact to build size
- More security for client-server apps (get an access token on a client, get all user data on a server to avoid tampering)
Alternatives
- VK SDK (native, not cross-platform)
Terminology
- Please visit Terminology section
Understanding how it works
-
General workflow:
- Your app navigates users to VK
Authorization Endpointusing a default web browser (embedded webviews are not allowed) - Users perform sign-in using their login and password (if they are not signed in already)
- VK
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 VK
-
For Android, iOS, Universal Windows Platform (these platforms support deep linking):
Redirect URIis a deep link which activates the app and providescodein URI parameters
-
For Windows, Mac, WebGL and Editor (these platforms don't support deep linking):
Redirect URIis empty (not used), in this caseAuthorization Middlewarewill temporarily savecodethat can be further requested by the app- The app obtains
codefromAuthorization Middlewarewith a POST request - Further workflow is the same (exchanging
codeforaccess token, requesting user data)
Authorization Middleware
Authorization Middleware is used to workaround 2 issues:
- VK doesn't allow deep links for
Authorized redirect URI(deep linking works for Android, iOS and UWP). - Standalone platforms (Windows and Mac) and WebGL don't support deep linking (direct
OAuth Redirectis not possible in this case).Authorization MiddlewarehandlesOAuth Redirectand temporarily savescodethat can be further requested by the app using POST.
Authorization Middleware has the following URL https://hippogames.dev/api/oauth/ and contains 3 methods:
initshould be called before navigating to VKAuthorization EndpointwithstateandRedirect URIparametersredirectis called by VKAuthorization Endpointwithstateandcodeafter users perform sign-ingetcodeshould be called from Standalone platforms (Windows and Mac) and WebGL to obtaincode
Can I trust Authorization Middleware? Is it secure to use a 3rd party service?
Authorization Middlewarecan't exchangecodeforaccess tokenwithout knowingSecure key. Only the app itself can exchangecodeforaccess token.- You can implement your own trusted
Authorization Middleware. Feel free to request source code (ASP.NET). - You can hire me to setup your own trusted
Authorization Middleware(ASP.NET web-hosting required).
Preconditions
- Pick your
Custom URI scheme(orProtocol). In my example it issimple.oauth, but it can bemyapp.auth(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: navigate to
Player Settings > Other > Configurationand add yourCustom URI schemetoSupported URL schemes. - For Universal Windows Platform: navigate to
Player Settings > Publishing Settingsand setProtocol(it MUST contain a period symbol, for examplesimple.oauth), then enableInternetClientinCapabilities.
Setup steps
- Visit VK Developers
- Create a new
Standalone app - Go to settings and enable
Open API - Set
Website addressashttps://hippogames.dev - Set
Base domainashippogames.dev - Set
Authorized redirect URIashttps://hippogames.dev/api/oauth/redirect(add a new address) - Copy
App IDandSecure key
Usage
- Check our
Examplescene and C# code ofExample.cs - Configure
Resources/Settings.asset- For Windows, Mac and WebGL: set
App IDandSecure key - For Android, iOS, UWP: set
Custom URI SCheme(Protocol) as well
- For Windows, Mac and WebGL: set
- Call
VKAuth.SignIn - Create
OnSignIncallback as the only argument forVKAuth.SignInto obtainUserInfo - Build and test
- Write a review on the Asset Store :)
Best practices
- Before calling
VKAuth.SignIn, check if a user is already signed in (VKAuth.SavedAuth != null) - If so, get
VKAuth.SavedAuth.UserInfoand do not callVKAuth.SignIn - Create
Sign outbutton if needed (callVKAuth.SignOut) - Remove debug logs in
VKAuth.Logfor production
Next steps (optional):
- You can get more user fields in
RequestUserInfomethod, add them to the request URL and extendUserInfoclass - Subscribe to
VKAuth.OnTokenResponseevent to getTokenResponse(alternatively, get it fromVKAuth.SavedAuth.TokenResponse) - If you have a backend (server), send
TokenResponseto it (to avoid tapmering user data when sending from clients to your server) - 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
App ID,Secure keyandCustom URI Schemethat come with the asset in production, they are for test purposes only and can be disabled/blocked - Don't forget to leave a review on the Asset Store
Known issues
- Please visit Common issues section