core: add automatic launch_url detection based on provider

This commit is contained in:
Jens Langhammer 2020-09-14 18:12:42 +02:00
parent ae6304c05e
commit 2c07859b68
5 changed files with 44 additions and 2 deletions

View File

@ -29,7 +29,16 @@ class ApplicationForm(forms.ModelForm):
]
widgets = {
"name": forms.TextInput(),
"meta_launch_url": forms.TextInput(),
"meta_launch_url": forms.TextInput(
attrs={
"placeholder": _(
(
"If left empty, passbook will try to extract the launch URL "
"based on the selected provider."
)
)
}
),
"meta_icon_url": forms.TextInput(),
"meta_publisher": forms.TextInput(),
}

View File

@ -92,6 +92,12 @@ class Provider(models.Model):
objects = InheritanceManager()
@property
def launch_url(self) -> Optional[str]:
"""URL to this provider and initiate authorization for the user.
Can return None for providers that are not URL-based"""
return None
def form(self) -> Type[ModelForm]:
"""Return Form class used to edit this object"""
raise NotImplementedError
@ -119,6 +125,14 @@ class Application(PolicyBindingModel):
meta_description = models.TextField(default="", blank=True)
meta_publisher = models.TextField(default="", blank=True)
def get_launch_url(self) -> Optional[str]:
"""Get launch URL if set, otherwise attempt to get launch URL based on provider."""
if self.meta_launch_url:
return self.meta_launch_url
if self.provider:
return self.provider.launch_url
return None
def get_provider(self) -> Optional[Provider]:
"""Get casted provider instance"""
if not self.provider:

View File

@ -24,7 +24,7 @@
{% if applications %}
<div class="pf-l-gallery pf-m-gutter">
{% for app in applications %}
<a href="{{ app.meta_launch_url }}" class="pf-c-card pf-m-hoverable pf-m-compact">
<a href="{{ app.get_launch_url }}" class="pf-c-card pf-m-hoverable pf-m-compact">
<div class="pf-c-card__header">
{% if not app.meta_icon_url %}
<i class="pf-icon pf-icon-arrow"></i>

View File

@ -6,6 +6,7 @@ import time
from dataclasses import asdict, dataclass, field
from hashlib import sha256
from typing import Any, Dict, List, Optional, Type
from urllib.parse import urlparse
from uuid import uuid4
from django.conf import settings
@ -230,6 +231,16 @@ class OAuth2Provider(Provider):
except Provider.application.RelatedObjectDoesNotExist:
return None
@property
def launch_url(self) -> Optional[str]:
"""Guess launch_url based on first redirect_uri"""
if not self.redirect_uris:
return None
main_url = self.redirect_uris[0]
launch_url = urlparse(main_url)
launch_url.path = ""
return launch_url.geturl()
def form(self) -> Type[ModelForm]:
from passbook.providers.oauth2.forms import OAuth2ProviderForm

View File

@ -1,5 +1,6 @@
"""passbook saml_idp Models"""
from typing import Optional, Type
from urllib.parse import urlparse
from django.db import models
from django.forms import ModelForm
@ -102,6 +103,13 @@ class SAMLProvider(Provider):
),
)
@property
def launch_url(self) -> Optional[str]:
"""Guess launch_url based on acs URL"""
launch_url = urlparse(self.acs_url)
launch_url.path = ""
return launch_url.geturl()
def form(self) -> Type[ModelForm]:
from passbook.providers.saml.forms import SAMLProviderForm