core: add automatic launch_url detection based on provider
This commit is contained in:
parent
ae6304c05e
commit
2c07859b68
|
@ -29,7 +29,16 @@ class ApplicationForm(forms.ModelForm):
|
||||||
]
|
]
|
||||||
widgets = {
|
widgets = {
|
||||||
"name": forms.TextInput(),
|
"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_icon_url": forms.TextInput(),
|
||||||
"meta_publisher": forms.TextInput(),
|
"meta_publisher": forms.TextInput(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,6 +92,12 @@ class Provider(models.Model):
|
||||||
|
|
||||||
objects = InheritanceManager()
|
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]:
|
def form(self) -> Type[ModelForm]:
|
||||||
"""Return Form class used to edit this object"""
|
"""Return Form class used to edit this object"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@ -119,6 +125,14 @@ class Application(PolicyBindingModel):
|
||||||
meta_description = models.TextField(default="", blank=True)
|
meta_description = models.TextField(default="", blank=True)
|
||||||
meta_publisher = 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]:
|
def get_provider(self) -> Optional[Provider]:
|
||||||
"""Get casted provider instance"""
|
"""Get casted provider instance"""
|
||||||
if not self.provider:
|
if not self.provider:
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
{% if applications %}
|
{% if applications %}
|
||||||
<div class="pf-l-gallery pf-m-gutter">
|
<div class="pf-l-gallery pf-m-gutter">
|
||||||
{% for app in applications %}
|
{% 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">
|
<div class="pf-c-card__header">
|
||||||
{% if not app.meta_icon_url %}
|
{% if not app.meta_icon_url %}
|
||||||
<i class="pf-icon pf-icon-arrow"></i>
|
<i class="pf-icon pf-icon-arrow"></i>
|
||||||
|
|
|
@ -6,6 +6,7 @@ import time
|
||||||
from dataclasses import asdict, dataclass, field
|
from dataclasses import asdict, dataclass, field
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
from typing import Any, Dict, List, Optional, Type
|
from typing import Any, Dict, List, Optional, Type
|
||||||
|
from urllib.parse import urlparse
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
@ -230,6 +231,16 @@ class OAuth2Provider(Provider):
|
||||||
except Provider.application.RelatedObjectDoesNotExist:
|
except Provider.application.RelatedObjectDoesNotExist:
|
||||||
return None
|
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]:
|
def form(self) -> Type[ModelForm]:
|
||||||
from passbook.providers.oauth2.forms import OAuth2ProviderForm
|
from passbook.providers.oauth2.forms import OAuth2ProviderForm
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
"""passbook saml_idp Models"""
|
"""passbook saml_idp Models"""
|
||||||
from typing import Optional, Type
|
from typing import Optional, Type
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.forms import ModelForm
|
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]:
|
def form(self) -> Type[ModelForm]:
|
||||||
from passbook.providers.saml.forms import SAMLProviderForm
|
from passbook.providers.saml.forms import SAMLProviderForm
|
||||||
|
|
||||||
|
|
Reference in New Issue