Skip to content

Commit

Permalink
add an option to use specific loopback port (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasboattini authored Jan 19, 2024
1 parent 57028e1 commit a01c1f9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
9 changes: 6 additions & 3 deletions Assets/UnityGoogleDrive/Editor/GoogleDriveSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ public class GoogleDriveSettingsEditor : Editor
private SerializedProperty uriSchemeClientCredentials;
private SerializedProperty accessScopes;
private SerializedProperty loopbackUri;
private SerializedProperty loopbackPort;
private SerializedProperty loopbackResponseHtml;
private SerializedProperty accessTokenPrefsKey;
private SerializedProperty refreshTokenPrefsKey;

private static readonly GUIContent genericClientCredentialsContent = new GUIContent("Generic Credentials", "Google Drive API application credentials used to authorize requests via loopback and redirect schemes.");
private static readonly GUIContent uriSchemeClientCredentialsContent = new GUIContent("URI Scheme Credentials", "Google Drive API application credentials used to authorize requests via custom URI scheme.");
private static readonly GUIContent accessScopesContent = new GUIContent("Access Scopes", "Scopes of access to the user's Google Drive the app will request.");
private static readonly GUIContent loopbackUriContent = new GUIContent("Loopback URI", "A web address for the loopback authentication requests. Defult is 'localhost'.");
private static readonly GUIContent loopbackUriContent = new GUIContent("Loopback URI", "A web address for the loopback authentication requests. Default is 'localhost'.");
private static readonly GUIContent loopbackPortContent = new GUIContent("Loopback Port", "Specific port to use for loopback scheme, eg: 8888. Leave empty to find random unused port.");
private static readonly GUIContent loopbackResponseHtmlContent = new GUIContent("Loopback Response HTML", "HTML page shown to the user when loopback response is received.");
private static readonly GUIContent accessTokenPrefsKeyContent = new GUIContent("Access Token Key", "PlayerPrefs key used to store access token.");
private static readonly GUIContent refreshTokenPrefsKeyContent = new GUIContent("Refresh Token Key", "PlayerPrefs key used to store refresh token.");
Expand Down Expand Up @@ -59,6 +61,7 @@ private void OnEnable ()
uriSchemeClientCredentials = serializedObject.FindProperty("uriSchemeClientCredentials");
accessScopes = serializedObject.FindProperty("accessScopes");
loopbackUri = serializedObject.FindProperty("loopbackUri");
loopbackPort = serializedObject.FindProperty("loopbackPort");
loopbackResponseHtml = serializedObject.FindProperty("loopbackResponseHtml");
accessTokenPrefsKey = serializedObject.FindProperty("accessTokenPrefsKey");
refreshTokenPrefsKey = serializedObject.FindProperty("refreshTokenPrefsKey");
Expand All @@ -76,6 +79,7 @@ public override void OnInspectorGUI ()
EditorGUILayout.PropertyField(uriSchemeClientCredentials, uriSchemeClientCredentialsContent, true);
EditorGUILayout.PropertyField(accessScopes, accessScopesContent, true);
EditorGUILayout.PropertyField(loopbackUri, loopbackUriContent);
EditorGUILayout.PropertyField(loopbackPort, loopbackPortContent);
EditorGUILayout.PropertyField(loopbackResponseHtml, loopbackResponseHtmlContent);
EditorGUILayout.PropertyField(accessTokenPrefsKey, accessTokenPrefsKeyContent);
EditorGUILayout.PropertyField(refreshTokenPrefsKey, refreshTokenPrefsKeyContent);
Expand All @@ -87,8 +91,7 @@ public override void OnInspectorGUI ()

using (new EditorGUI.DisabledScope(string.IsNullOrEmpty(TargetSettings.GenericClientCredentials.ProjectId)))
if (GUILayout.Button("Manage Google Drive API app"))
Application.OpenURL(string.Format("https://console.developers.google.com/apis/credentials?project={0}",
TargetSettings.GenericClientCredentials.ProjectId));
Application.OpenURL($"https://console.developers.google.com/apis/credentials?project={TargetSettings.GenericClientCredentials.ProjectId}");

if (GUILayout.Button("Parse generic credentials JSON file..."))
ParseGenericCredentialsJson(EditorUtility.OpenFilePanel("Select Drive API app credentials JSON file", "", "json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ private void ExecuteFullAuth ()
var codeChallenge = CryptoUtils.Base64UriEncodeNoPadding(codeVerifierHash);

// Creates a redirect URI using an available port on the loopback address.
redirectUri = $"{settings.LoopbackUri}:{GetRandomUnusedPort()}";
var port = string.IsNullOrWhiteSpace(settings.LoopbackPort)
? GetRandomUnusedPort() : int.Parse(settings.LoopbackPort);
redirectUri = $"{settings.LoopbackUri}:{port}";

// Listen for requests on the redirect URI.
var httpListener = new HttpListener();
Expand All @@ -113,9 +115,10 @@ private void ExecuteFullAuth ()

// Create the OAuth 2.0 authorization request.
// https://developers.google.com/identity/protocols/OAuth2WebServer#creatingclient
var authRequest = string.Format("{0}?response_type=code&scope={1}&redirect_uri={2}&client_id={3}&state={4}&code_challenge={5}&code_challenge_method={6}" +
"&access_type=offline" + // Forces to return a refresh token at the auth code exchange phase.
"&approval_prompt=force", // Forces to show consent screen for each auth request. Needed to return refresh tokens on consequent auth runs.
var authRequest = string.Format(
"{0}?response_type=code&scope={1}&redirect_uri={2}&client_id={3}&state={4}&code_challenge={5}&code_challenge_method={6}" +
"&access_type=offline" + // Forces to return a refresh token at the auth code exchange phase.
"&approval_prompt=force", // Forces to show consent screen for each auth request. Needed to return refresh tokens on consequent auth runs.
settings.GenericClientCredentials.AuthUri,
Uri.EscapeDataString(settings.AccessScope),
Uri.EscapeDataString(redirectUri),
Expand Down
5 changes: 5 additions & 0 deletions Assets/UnityGoogleDrive/Runtime/GoogleDriveSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public class GoogleDriveSettings : ScriptableObject
/// <see href="https://forum.unity.com/threads/515360/page-2#post-3504547"/>
public string LoopbackUri => loopbackUri;
/// <summary>
/// Specific port to use for loopback scheme. When empty find random unused port instead.
/// </summary>
public string LoopbackPort => loopbackPort;
/// <summary>
/// HTML page shown to the user when loopback response is received.
/// </summary>
public string LoopbackResponseHtml { get => loopbackResponseHtml; set => loopbackResponseHtml = value; }
Expand All @@ -51,6 +55,7 @@ public class GoogleDriveSettings : ScriptableObject
[SerializeField] private UriSchemeClientCredentials uriSchemeClientCredentials;
[SerializeField] private List<string> accessScopes = new List<string> { "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.appdata" };
[SerializeField] private string loopbackUri = "http://localhost";
[SerializeField] private string loopbackPort = "";
[SerializeField] private string loopbackResponseHtml = "<html><h1>Please return to the app.</h1></html>";
[SerializeField] private string accessTokenPrefsKey = "GoogleDriveAccessToken";
[SerializeField] private string refreshTokenPrefsKey = "GoogleDriveRefreshToken";
Expand Down
2 changes: 1 addition & 1 deletion Assets/UnityGoogleDrive/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.elringus.unitygoogledrive",
"version": "0.28.0",
"version": "0.29.0",
"displayName": "UnityGoogleDrive",
"description": "Google Drive SDK for Unity game engine",
"unity": "2019.4",
Expand Down
Binary file modified UnityGoogleDrive.unitypackage
Binary file not shown.

0 comments on commit a01c1f9

Please sign in to comment.