users: Get all API keys via wrapper method.

Now reading API keys from a user is done with the get_api_key wrapper
method, rather than directly fetching it from the user object.

Also, every place where an action should be done for each API key is now
using get_all_api_keys. This method returns for the moment a single-item
list, containing the specified user's API key.

This commit is the first step towards allowing users have multiple API
keys.
This commit is contained in:
Yago González
2018-08-01 10:53:40 +02:00
committed by Tim Abbott
parent 13b9dd33fa
commit f6219745de
20 changed files with 110 additions and 52 deletions

View File

@@ -32,6 +32,7 @@ from zerver.lib.push_notifications import push_notifications_enabled
from zerver.lib.request import REQ, has_request_variables, JsonableError
from zerver.lib.response import json_success, json_error
from zerver.lib.subdomains import get_subdomain, is_subdomain_root_or_alias
from zerver.lib.users import get_api_key
from zerver.lib.validator import validate_login_email
from zerver.models import PreregistrationUser, UserProfile, remote_user_to_email, Realm, \
get_realm
@@ -730,7 +731,8 @@ def api_dev_fetch_api_key(request: HttpRequest, username: str=REQ()) -> HttpResp
return json_error(_("This user is not registered."),
data={"reason": "unregistered"}, status=403)
do_login(request, user_profile)
return json_success({"api_key": user_profile.api_key, "email": user_profile.email})
api_key = get_api_key(user_profile)
return json_success({"api_key": api_key, "email": user_profile.email})
@csrf_exempt
def api_dev_list_users(request: HttpRequest) -> HttpResponse:
@@ -793,7 +795,8 @@ def api_fetch_api_key(request: HttpRequest, username: str=REQ(), password: str=R
process_client(request, user_profile)
request._email = user_profile.email
return json_success({"api_key": user_profile.api_key, "email": user_profile.email})
api_key = get_api_key(user_profile)
return json_success({"api_key": api_key, "email": user_profile.email})
def get_auth_backends_data(request: HttpRequest) -> Dict[str, Any]:
"""Returns which authentication methods are enabled on the server"""
@@ -863,7 +866,9 @@ def json_fetch_api_key(request: HttpRequest, user_profile: UserProfile,
if not authenticate(username=user_profile.email, password=password,
realm=realm):
return json_error(_("Your username or password is incorrect."))
return json_success({"api_key": user_profile.api_key})
api_key = get_api_key(user_profile)
return json_success({"api_key": api_key})
@csrf_exempt
def api_fetch_google_client_id(request: HttpRequest) -> HttpResponse: