Skip to content

Commit 8b77f00

Browse files
committed
Backport PR #1994: Further highlight token info in log output
1 parent dd8bf2b commit 8b77f00

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

notebook/notebookapp.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
from nbformat.sign import NotebookNotary
8080
from traitlets import (
8181
Dict, Unicode, Integer, List, Bool, Bytes, Instance,
82-
TraitError, Type, Float
82+
TraitError, Type, Float, observe
8383
)
8484
from ipython_genutils import py3compat
8585
from jupyter_core.paths import jupyter_runtime_dir, jupyter_path
@@ -533,13 +533,21 @@ def _write_cookie_secret_file(self, secret):
533533
Once used, this token cannot be used again.
534534
"""
535535
)
536+
537+
_token_generated = True
536538

537539
def _token_default(self):
538540
if self.password:
539541
# no token if password is enabled
542+
self._token_generated = False
540543
return u''
541544
else:
545+
self._token_generated = True
542546
return binascii.hexlify(os.urandom(24)).decode('ascii')
547+
548+
@observe('token')
549+
def _token_changed(self, change):
550+
self._token_generated = False
543551

544552
password = Unicode(u'', config=True,
545553
help="""Hashed password to use for web authentication.
@@ -977,6 +985,12 @@ def init_webapp(self):
977985
@property
978986
def display_url(self):
979987
ip = self.ip if self.ip else '[all ip addresses on your system]'
988+
url = self._url(ip)
989+
if self.token:
990+
# Don't log full token if it came from config
991+
token = self.token if self._token_generated else '...'
992+
url = url_concat(url, {'token': token})
993+
return url
980994
query = '?token=%s' % self.token if self.token else ''
981995
return self._url(ip) + query
982996

@@ -1195,7 +1209,17 @@ def start(self):
11951209
b = lambda : browser.open(url_path_join(self.connection_url, uri),
11961210
new=2)
11971211
threading.Thread(target=b).start()
1198-
1212+
1213+
if self.token and self._token_generated:
1214+
# log full URL with generated token, so there's a copy/pasteable link
1215+
# with auth info.
1216+
self.log.critical('\n'.join([
1217+
'\n',
1218+
'Copy/paste this URL into your browser when you connect for the first time,',
1219+
'to login with a token:',
1220+
' %s' % url_concat(self.connection_url, {'token': self.token}),
1221+
]))
1222+
11991223
self.io_loop = ioloop.IOLoop.current()
12001224
if sys.platform.startswith('win'):
12011225
# add no-op to wake every 5s
@@ -1251,4 +1275,3 @@ def list_running_servers(runtime_dir=None):
12511275
#-----------------------------------------------------------------------------
12521276

12531277
main = launch_new_instance = NotebookApp.launch_instance
1254-

notebook/templates/login.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<div class="navbar-inner">
2121
<div class="container">
2222
<div class="center-nav">
23-
<p class="navbar-text nav">Password or token:</p>
23+
<p class="navbar-text nav">Password{% if token_available %} or token{% endif %}:</p>
2424
<form action="{{base_url}}login?next={{next}}" method="post" class="navbar-form pull-left">
2525
<input type="password" name="password" id="password_input" class="form-control">
2626
<button type="submit" id="login_submit">Log in</button>
@@ -42,10 +42,11 @@
4242
{% endfor %}
4343
</div>
4444
{% endif %}
45+
{% if token_available %}
4546
{% block token_message %}
4647
<div class="col-sm-6 col-sm-offset-3 text-left">
4748
<p class="warning">
48-
If this notebook server has no password set, token authentication is enabled.
49+
Token authentication is enabled.
4950

5051
You need to open the notebook server with its first-time login token in the URL,
5152
or enable a password in order to gain access.
@@ -62,8 +63,12 @@
6263
<p>
6364
Or you can paste just the token value into the password field on this page.
6465
</p>
66+
<p>
67+
Cookies are required for authenticated access to notebooks.
68+
</p>
6569
</div>
6670
{% endblock token_message %}
71+
{% endif %}
6772
</div>
6873

6974
{% endblock %}

0 commit comments

Comments
 (0)