Skip to content

Commit

Permalink
Merge pull request #4817 from minrk/share-code-full-url
Browse files Browse the repository at this point in the history
add full URLs to share modes
  • Loading branch information
minrk committed May 16, 2024
2 parents abc9581 + 9ff8f3e commit 6912a5a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
19 changes: 17 additions & 2 deletions docs/source/_static/rest-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1176,8 +1176,16 @@ paths:
example: abc123
accept_url:
type: string
description: The URL for accepting the code
description: The URL path for accepting the code
example: /hub/accept-share?code=abc123
full_accept_url:
type:
- string
- "null"
description: |
The full URL for accepting the code,
if JupyterHub.public_url configuration is defined.
example: https://hub.example.org/hub/accept-share?code=abc123
security:
- oauth2:
- shares
Expand Down Expand Up @@ -1877,7 +1885,14 @@ components:
description: the server name. '' for the default server.
url:
type: string
description: the server's URL
description: the server's URL (path only when not using subdomains)
full_url:
type:
- string
- "null"
description: |
The full URL of the server (`https://hub.example.org/user/:name/:servername`).
`null` unless JupyterHub.public_url or subdomains are configured.
ready:
type: boolean
description: whether the server is ready
Expand Down
1 change: 1 addition & 0 deletions docs/source/reference/sharing.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ The response contains the code itself:
{
"code": "abc1234....",
"accept_url": "/hub/accept-share?code=abc1234",
"full_accept_url": "https://hub.example.org/hub/accept-share?code=abc1234",
"id": "sc_1234",
"scopes": [...],
...
Expand Down
3 changes: 3 additions & 0 deletions docs/source/tutorial/sharing.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,14 @@ which will have a JSON response:
'last_exchanged_at': None,
'code': 'U-eYLFT1lGstEqfMHpAIvTZ1MRjZ1Y1a-loGQ0K86to',
'accept_url': '/hub/accept-share?code=U-eYLFT1lGstEqfMHpAIvTZ1MRjZ1Y1a-loGQ0K86to',
'full_accept_url': 'https://hub.example.org/accept-share?code=U-eYLFT1lGstEqfMHpAIvTZ1MRjZ1Y1a-loGQ0K86to',
}
```

The most relevant fields here are `code`, which contains the code itself, and `accept_url`, which is the URL path for the page another user.
Note: it does not contain the _hostname_ of the hub, which JupyterHub often does not know.
If `public_url` configuration is defined, `full_accept_url` will be the full URL including the host.
Otherwise, it will be null.

Share codes are guaranteed to be url-safe, so no encoding is required.

Expand Down
11 changes: 9 additions & 2 deletions jupyterhub/apihandlers/shares.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import re
from typing import List, Optional
from urllib.parse import urlunparse

from pydantic import (
BaseModel,
Expand Down Expand Up @@ -80,7 +81,7 @@ def server_model(self, spawner):
"""Truncated server model for use in shares
- Adds "user" field (just name for now)
- Limits fields to "name", "url", "ready"
- Limits fields to "name", "url", "full_url", "ready"
from standard server model
"""
user = self.users[spawner.user.id]
Expand All @@ -95,7 +96,7 @@ def server_model(self, spawner):
}
}
# subset keys for sharing
for key in ["name", "url", "ready"]:
for key in ["name", "url", "full_url", "ready"]:
if key in full_model:
server_model[key] = full_model[key]

Expand Down Expand Up @@ -128,6 +129,12 @@ def share_code_model(self, share_code, code=None):
model["accept_url"] = url_concat(
self.hub.base_url + "accept-share", {"code": code}
)
model["full_accept_url"] = None
public_url = self.settings.get("public_url")
if public_url:
model["full_accept_url"] = urlunparse(
public_url._replace(path=model["accept_url"])
)
return model

def _init_share_query(self, kind="share"):
Expand Down

0 comments on commit 6912a5a

Please sign in to comment.