Skip to content

Commit ea5c8f2

Browse files
shchekleinefiop
authored andcommitted
feat(auth): add GDRIVE_NON_INTERACTIVE to avoid launching browser
1 parent be873e6 commit ea5c8f2

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pydrive2/auth.py

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import json
23
import webbrowser
34
import httplib2
@@ -224,6 +225,9 @@ def LocalWebserverAuth(
224225
This function is not for web server application. It creates local web
225226
server for user from standalone application.
226227
228+
If GDRIVE_NON_INTERACTIVE environment variable is set, this function
229+
raises AuthenticationError.
230+
227231
:param host_name: host name of the local web server.
228232
:type host_name: str.
229233
:param port_numbers: list of port numbers to be tried to used.
@@ -237,6 +241,11 @@ def LocalWebserverAuth(
237241
:returns: str -- code returned from local web server
238242
:raises: AuthenticationRejected, AuthenticationError
239243
"""
244+
if os.getenv("GDRIVE_NON_INTERACTIVE"):
245+
raise AuthenticationError(
246+
"Non interactive mode (GDRIVE_NON_INTERACTIVE env) is enabled"
247+
)
248+
240249
if port_numbers is None:
241250
port_numbers = [
242251
8080,

pydrive2/test/test_oauth.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import json
22
import os
3+
import re
34
import time
45
import pytest
6+
from pytest import MonkeyPatch
57

6-
from pydrive2.auth import GoogleAuth
8+
from pydrive2.auth import AuthenticationError, GoogleAuth
79
from pydrive2.test.test_util import (
810
setup_credentials,
911
delete_file,
@@ -188,6 +190,27 @@ def test_12_ServiceAuthFromJsonDictNoCredentialsSaving():
188190
time.sleep(1)
189191

190192

193+
def test_13_LocalWebServerAuthNonInterativeRaises():
194+
settings = {
195+
"client_config_backend": "file",
196+
"client_config_file": "client_secrets.json",
197+
"oauth_scope": ["https://www.googleapis.com/auth/drive"],
198+
}
199+
ga = GoogleAuth(settings=settings)
200+
201+
with MonkeyPatch.context() as m:
202+
m.setenv("GDRIVE_NON_INTERACTIVE", "true")
203+
# Test that exception is raised on trying to do browser auth if
204+
# we are running in a non interactive environment.
205+
with pytest.raises(
206+
AuthenticationError,
207+
match=re.escape(
208+
"Non interactive mode (GDRIVE_NON_INTERACTIVE env) is enabled"
209+
),
210+
):
211+
ga.LocalWebserverAuth()
212+
213+
191214
def CheckCredentialsFile(credentials, no_file=False):
192215
ga = GoogleAuth(settings_file_path("test_oauth_default.yaml"))
193216
ga.LoadCredentialsFile(credentials)

0 commit comments

Comments
 (0)