support github client_id, client_secret for API access
@@ -6,25 +6,25 @@ control services and providers.
"""
import json
+import os
-from tornado import gen, web
+from tornado import gen
from tornado.httpclient import AsyncHTTPClient, HTTPError
-from traitlets import Unicode
+from tornado.httputil import url_concat
+
+from traitlets import Dict, Unicode, default
from traitlets.config import LoggingConfigurable
class RepoProvider(LoggingConfigurable):
"""Base class for a repo provider"""
name = Unicode(
- None,
help="""
Descriptive human readable name of this repo provider.
"""
)
spec = Unicode(
- None,
- allow_none=True,
help="""
The spec for this builder to parse
"""
@@ -45,27 +45,52 @@ class GitHubRepoProvider(RepoProvider):
"""Repo provider for the GitHub service"""
name = Unicode('GitHub')
- username = Unicode(
- None,
- allow_none=True,
- config=True,
- help="""
- The GitHub user name to use when making GitHub API calls.
+ client_id = Unicode(config=True,
+ help="""GitHub client id for authentication with the GitHub API
- Set to None to not use authenticated API calls
+ For use with client_secret.
+ Loaded from GITHUB_CLIENT_ID env by default.
"""
)
+ @default('client_id')
+ def _client_id_default(self):
+ return os.getenv('GITHUB_CLIENT_ID', '')
- password = Unicode(
- None,
- allow_none=True,
- config=True,
- help="""
- The password to use to make GitHub API calls.
+ client_secret = Unicode(config=True,
+ help="""GitHub client secret for authentication with the GitHub API
+
+ For use with client_id.
+ Loaded from GITHUB_CLIENT_SECRET env by default.
+ """
+ )
+ @default('client_secret')
+ def _client_secret_default(self):
+ return os.getenv('GITHUB_CLIENT_SECRET', '')
+
+ access_token = Unicode(config=True,
+ help="""GitHub access token for authentication with the GitHub API
- Don't use an *actual* password - create a personal access token and use that!
+ Loaded from GITHUB_ACCESS_TOKEN env by default.
"""
)
+ @default('access_token')
+ def _access_token_default(self):
+ return os.getenv('GITHUB_ACCESS_TOKEN', '')
+
+ auth = Dict(
+ help="""Auth parameters for the GitHub API access
+
+ Populated from client_id, client_secret, access_token.
+ """
+ )
+ @default('auth')
+ def _default_auth(self):
+ auth = {}
+ for key in ('client_id', 'client_secret', 'access_token'):
+ value = getattr(self, key)
+ if value:
+ auth[key] = value
+ return auth
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -94,16 +119,12 @@ class GitHubRepoProvider(RepoProvider):
)
self.log.debug("Fetching %s", api_url)
- if self.username and self.password:
- auth = {
- 'auth_username': self.username,
- 'auth_password': self.password
- }
- else:
- auth = {}
+ if self.auth:
+ # Add auth params. After logging!
+ api_url = url_concat(api_url, self.auth)
try:
- resp = yield client.fetch(api_url, user_agent="BinderHub", **auth)
+ resp = yield client.fetch(api_url, user_agent="BinderHub")
except HTTPError as e:
if e.code == 404:
return None
