Example #1
0
 def initialize(self):
     super().initialize()
     if self.settings['auth_enabled']:
         self.hub_auth = HubOAuth.instance(
             config=self.settings['traitlets_config'])
Example #2
0
import docker
import escapism
import gitlab
import requests
from flask import Flask, Response, abort, make_response, redirect, request
from flask import jsonify
from flask import send_from_directory
from jupyterhub.services.auth import HubOAuth

SERVICE_PREFIX = os.environ.get('JUPYTERHUB_SERVICE_PREFIX', '/')
"""Service prefix is set by JupyterHub service spawner."""

auth = HubOAuth(
    api_token=os.environ['JUPYTERHUB_API_TOKEN'],
    cache_max_age=60,
    oauth_client_id=os.getenv('NOTEBOOKS_OAUTH_CLIENT_ID',
                              'service-notebooks'),
)
"""Wrap JupyterHub authentication service API."""

app = Flask(__name__)


def _server_name(namespace, project, commit_sha):
    """Form a DNS-safe server name."""
    escape = partial(
        escapism.escape,
        safe=set(string.ascii_lowercase + string.digits),
        escape_char='-',
    )
    return '{namespace}-{project}-{commit_sha}'.format(
Example #3
0
"""
whoami service authentication with the Hub
"""

from functools import wraps
import json
import os

from flask import Flask, redirect, request, Response, make_response

from jupyterhub.services.auth import HubOAuth

prefix = os.environ.get('JUPYTERHUB_SERVICE_PREFIX', '/')

auth = HubOAuth(
    api_token=os.environ['JUPYTERHUB_API_TOKEN'],
    cache_max_age=60,
)

app = Flask(__name__)


def authenticated(f):
    """Decorator for authenticating with the Hub via OAuth"""
    @wraps(f)
    def decorated(*args, **kwargs):
        token = request.cookies.get(auth.cookie_name)
        if token:
            user = auth.user_for_token(token)
        else:
            user = None
        if user:
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Functions for interfacing with JupyterHub."""

import json
import os
from hashlib import md5

import requests
from jupyterhub.services.auth import HubOAuth

auth = HubOAuth(api_token=os.environ.get("JUPYTERHUB_API_TOKEN", "token"),
                cache_max_age=60)
"""Wrap JupyterHub authentication service API."""
__prefix = auth.api_url
__headers = {auth.auth_header_name: f"token {auth.api_token}"}


def make_server_name(namespace, project, branch, commit_sha):
    """Form a 16-digit hash server ID."""
    server_string = f"{namespace}{project}{branch}{commit_sha}"
    return "{project}-{hash}".format(
        project=project[:54], hash=md5(server_string.encode()).hexdigest()[:8])


def check_user_has_named_server(user, server_name):
    """Check if the named-server exists in user's JupyterHub servers"""
    user_info = get_user_info(user)