allow pre-loading API tokens from config

This is the first small part of easing the pain of services,
which is generating the API tokens,
and used to require initializing the JupyterHub database.
This commit is contained in:
Min RK
2016-04-14 15:31:36 +02:00
parent dfd01bbf5f
commit fa4b666693
4 changed files with 79 additions and 3 deletions

View File

@@ -303,11 +303,19 @@ class User(Base):
name=self.name,
)
def new_api_token(self):
"""Create a new API token"""
def new_api_token(self, token=None):
"""Create a new API token
If `token` is given, load that token.
"""
assert self.id is not None
db = inspect(self).session
token = new_token()
if token is None:
token = new_token()
else:
found = APIToken.find(db, token)
if found:
raise ValueError("Collision on token: %s..." % token[:4])
orm_token = APIToken(user_id=self.id)
orm_token.token = token
db.add(orm_token)