Skip to content

Commit 1d52b1d

Browse files
committed
add system_password identity provider example
1 parent 22c477c commit 1d52b1d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Jupyter login with system password
2+
3+
This `jupyter_server_config.py` defines and enables a `SystemPasswordIdentityProvider`.
4+
This IdentityProvider checks the entered password against your system password using PAM.
5+
Only the current user's password (the user the server is running as) is accepted.
6+
7+
The result is a User whose name matches the system user, rather than a randomly generated one.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pwd
2+
from getpass import getuser
3+
4+
from pamela import PAMError, authenticate
5+
6+
from jupyter_server.auth.identity import IdentityProvider, User
7+
8+
9+
class SystemPasswordIdentityProvider(IdentityProvider):
10+
# no need to generate a default token (token can still be used, but it's opt-in)
11+
need_token = False
12+
13+
def process_login_form(self, handler):
14+
username = getuser()
15+
password = handler.get_argument("password", "")
16+
try:
17+
authenticate(username, password)
18+
except PAMError as e:
19+
self.log.error(f"Failed login for {username}: {e}")
20+
return None
21+
22+
user_info = pwd.getpwnam(username)
23+
# get human name from pwd, if not empty
24+
return User(username=username, name=user_info.pw_gecos or username)
25+
26+
27+
c = get_config() # type: ignore # noqa
28+
29+
c.ServerApp.identity_provider_class = SystemPasswordIdentityProvider

0 commit comments

Comments
 (0)