// // UniWebViewAuthenticationFlowGoogle.cs // Created by Wang Wei (@onevcat) on 2022-06-25. // // This file is a part of UniWebView Project (https://uniwebview.com) // By purchasing the asset, you are allowed to use this code in as many as projects // you want, only if you publish the final products under the name of the same account // used for the purchase. // // This asset and all corresponding files (such as source code) are provided on an // “as is” basis, without warranty of any kind, express of implied, including but not // limited to the warranties of merchantability, fitness for a particular purpose, and // noninfringement. In no event shall the authors or copyright holders be liable for any // claim, damages or other liability, whether in action of contract, tort or otherwise, // arising from, out of or in connection with the software or the use of other dealing in the software. // using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; /// /// A predefined authentication flow for Google Identity. /// /// This implementation follows the flow described here: /// https://developers.google.com/identity/protocols/oauth2/native-app /// /// Google authentication flow is a bit different from the other standard authentication flows. Please read the link /// above carefully to understand it. /// /// See https://docs.uniwebview.com/guide/oauth2.html for a more detailed guide of authentication in UniWebView. /// public class UniWebViewAuthenticationFlowGoogle : UniWebViewAuthenticationCommonFlow, IUniWebViewAuthenticationFlow { /// /// The client ID of your Google application. /// public string clientId = ""; /// /// The redirect URI of your Google application. /// /// It might be something like "com.googleusercontent.apps.${clientId}:${redirect_uri_path}". Be caution that the URI does not /// contain regular double slashes `//`, but should be only one. /// public string redirectUri = ""; /// /// The scope of your Google application. /// /// It might be some full URL in recent Google services, such as "https://www.googleapis.com/auth/userinfo.profile" /// public string scope = ""; /// /// Optional to control this flow's behaviour. /// public UniWebViewAuthenticationFlowGoogleOptional optional; private string responseType = "code"; private string grantType = "authorization_code"; private readonly UniWebViewAuthenticationConfiguration config = new UniWebViewAuthenticationConfiguration( "https://accounts.google.com/o/oauth2/v2/auth", "https://oauth2.googleapis.com/token" ); /// /// Starts the authentication flow with the standard OAuth 2.0. /// This implements the abstract method in `UniWebViewAuthenticationCommonFlow`. /// public override void StartAuthenticationFlow() { var flow = new UniWebViewAuthenticationFlow(this); flow.StartAuth(); } /// /// Starts the refresh flow with the standard OAuth 2.0. /// This implements the abstract method in `UniWebViewAuthenticationCommonFlow`. /// /// The refresh token received with a previous access token response. public override void StartRefreshTokenFlow(string refreshToken) { var flow = new UniWebViewAuthenticationFlow(this); flow.RefreshToken(refreshToken); } /// /// Implements required method in `IUniWebViewAuthenticationFlow`. /// public UniWebViewAuthenticationConfiguration GetAuthenticationConfiguration() { return config; } /// /// Implements required method in `IUniWebViewAuthenticationFlow`. /// public string GetCallbackUrl() { return redirectUri; } /// /// Implements required method in `IUniWebViewAuthenticationFlow`. /// public Dictionary GetAuthenticationUriArguments() { var authorizeArgs = new Dictionary { { "client_id", clientId }, { "redirect_uri", redirectUri }, { "scope", scope }, { "response_type", responseType } }; if (optional != null) { if (optional.enableState) { var state = GenerateAndStoreState(); authorizeArgs.Add("state", state); } if (optional.PKCESupport != UniWebViewAuthenticationPKCE.None) { var codeChallenge = GenerateCodeChallengeAndStoreCodeVerify(optional.PKCESupport); authorizeArgs.Add("code_challenge", codeChallenge); var method = UniWebViewAuthenticationUtils.ConvertPKCEToString(optional.PKCESupport); authorizeArgs.Add("code_challenge_method", method); } if (!String.IsNullOrEmpty(optional.loginHint)) { authorizeArgs.Add("login_hint", optional.loginHint); } } return authorizeArgs; } /// /// Implements required method in `IUniWebViewAuthenticationFlow`. /// public Dictionary GetAccessTokenRequestParameters(string authResponse) { if (!authResponse.StartsWith(redirectUri)) { throw AuthenticationResponseException.UnexpectedAuthCallbackUrl; } var uri = new Uri(authResponse); var response = UniWebViewAuthenticationUtils.ParseFormUrlEncodedString(uri.Query); if (!response.TryGetValue("code", out var code)) { throw AuthenticationResponseException.InvalidResponse(authResponse); } if (optional.enableState) { VerifyState(response); } var parameters = new Dictionary { { "client_id", clientId }, { "code", code }, { "redirect_uri", redirectUri }, { "grant_type", grantType }, }; if (CodeVerify != null) { parameters.Add("code_verifier", CodeVerify); } return parameters; } /// /// Implements required method in `IUniWebViewAuthenticationFlow`. /// public Dictionary GetRefreshTokenRequestParameters(string refreshToken) { return new Dictionary { { "client_id", clientId }, { "refresh_token", refreshToken }, { "grant_type", "refresh_token" } }; } /// /// Implements required method in `IUniWebViewAuthenticationFlow`. /// public UniWebViewAuthenticationGoogleToken GenerateTokenFromExchangeResponse(string exchangeResponse) { return UniWebViewAuthenticationTokenFactory.Parse(exchangeResponse); } /// /// Implements required method in `IUniWebViewAuthenticationFlow`. /// [field: SerializeField] public UnityEvent OnAuthenticationFinished { get; set; } /// /// Implements required method in `IUniWebViewAuthenticationFlow`. /// [field: SerializeField] public UnityEvent OnAuthenticationErrored { get; set; } /// /// Implements required method in `IUniWebViewAuthenticationFlow`. /// [field: SerializeField] public UnityEvent OnRefreshTokenFinished { get; set; } /// /// Implements required method in `IUniWebViewAuthenticationFlow`. /// [field: SerializeField] public UnityEvent OnRefreshTokenErrored { get; set; } } /// /// The authentication flow's optional settings for Google. /// [Serializable] public class UniWebViewAuthenticationFlowGoogleOptional { /// /// Whether to enable PKCE when performing authentication. Default is `S256`. /// public UniWebViewAuthenticationPKCE PKCESupport = UniWebViewAuthenticationPKCE.S256; /// /// Whether to enable the state verification. If enabled, the state will be generated and verified in the /// authentication callback. Default is `true`. /// public bool enableState = true; /// /// If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to /// the Google Authentication Server. /// public string loginHint = ""; } /// /// The token object from Google. Check `UniWebViewAuthenticationStandardToken` for more. /// public class UniWebViewAuthenticationGoogleToken : UniWebViewAuthenticationStandardToken { }