Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork144
devel: rest api token auth#469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Implement a simple token authentication header for various "restish"endpoints we might want for adoption/disowning of packages.
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,13 +7,55 @@ | ||
| from django.contrib.auth.models import User, Group | ||
| from django_countries.fields import CountryField | ||
| from django.core.validators import MinValueValidator, MaxValueValidator | ||
| from django.core.exceptions import ImproperlyConfigured | ||
| from django.utils.deprecation import MiddlewareMixin | ||
| from .fields import PGPKeyField | ||
| from main.utils import make_choice, set_created_field | ||
| from planet.models import Feed | ||
| class AuthTokenBackend(object): | ||
| def authenticate(self, request, username=None, password=None): | ||
| if 'X-Archweb-Token' in request.headers: | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Wouldn't it make sense to use MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Sounds fine to me, it shouldn't conflict with things I suppose. | ||
| try: | ||
| profile = UserProfile.objects.get(api_token=request.headers['X-Archweb-Token']) | ||
| except UserProfile.DoesNotExist: | ||
| return None | ||
| print(profile) | ||
| return profile.user | ||
| return None | ||
| class AuthTokenMiddleware(MiddlewareMixin): | ||
| header = "X-Archweb-Token" | ||
| def process_request(self, request): | ||
| print("process", request) | ||
| # AuthenticationMiddleware is required so that request.user exists. | ||
| if not hasattr(request, "user"): | ||
| raise ImproperlyConfigured( | ||
| "The Django token user auth middleware requires the" | ||
| " authentication middleware to be installed. Edit your" | ||
| " MIDDLEWARE setting to insert" | ||
| " 'django.contrib.auth.middleware.AuthenticationMiddleware'" | ||
| " before the RemoteUserMiddleware class." | ||
| ) | ||
| try: | ||
| token = request.headers[self.header] | ||
| print(token) | ||
| except KeyError: | ||
| return | ||
| try: | ||
| profile = UserProfile.objects.get(api_token=token) | ||
| except UserProfile.DoesNotExist: | ||
| return None | ||
| request.user = profile.user | ||
| class UserProfile(models.Model): | ||
| latin_name = models.CharField( | ||
| max_length=255, null=True, blank=True, help_text="Latin-form name; used only for non-Latin full names") | ||
| @@ -56,6 +98,7 @@ class UserProfile(models.Model): | ||
| rebuilderd_updates = models.BooleanField( | ||
| default=False, help_text='Receive reproducible build package updates') | ||
| repos_auth_token = models.CharField(max_length=32, null=True, blank=True) | ||
| api_token = models.CharField(max_length=32, null=True, blank=True) | ||
| last_modified = models.DateTimeField(editable=False) | ||
| class Meta: | ||
| @@ -198,6 +241,7 @@ def delete_user_model(sender, **kwargs): | ||
| return | ||
| userprofile.repos_auth_token = '' | ||
| userprofile.api_token = '' | ||
| Feed.objects.filter(website_rss=userprofile.website_rss).delete() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -23,5 +23,20 @@ <h2>Developer Profile</h2> | ||
| <p><label></label> <input title="Save changes" type="submit" value="Save" /></p> | ||
| </form> | ||
| <form id="api-profile-form" enctype="multipart/form-data" method="post" action="">{% csrf_token %} | ||
| <h3>API token</h3> | ||
| <p>Token for completing todolist items with for example, rebuild-todo</p> | ||
| {% if profile.api_token is None %} | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I assume it would be a good pattern to be able to revoke a token, or to phrase it differently: Generate a new token and invalidate the existing one. | ||
| <fieldset> | ||
| <input type="hidden" name="api_token" id="api_token" value="generate"> | ||
| </fieldset> | ||
| <p><label></label> <input title="Generate token" type="submit" value="Generate API Token" /></p> | ||
| {% else %} | ||
| <fieldset> | ||
| <label>Token:</label> | ||
| {{ profile.api_token }} | ||
| </fieldset> | ||
| {% endif %} | ||
| </form> | ||
| </div> | ||
| {% endblock %} | ||