|
12 | 12 | from .generators import generate_client_id, generate_client_secret |
13 | 13 | from .scopes import get_scopes_backend |
14 | 14 | from .settings import oauth2_settings |
15 | | -from .validators import validate_uris |
| 15 | +from .validators import RedirectURIValidator, WildcardSet |
16 | 16 |
|
17 | 17 |
|
18 | 18 | class AbstractApplication(models.Model): |
@@ -65,7 +65,6 @@ class AbstractApplication(models.Model): |
65 | 65 |
|
66 | 66 | redirect_uris = models.TextField( |
67 | 67 | blank=True, help_text=_("Allowed URIs list, space separated"), |
68 | | - validators=[validate_uris] |
69 | 68 | ) |
70 | 69 | client_type = models.CharField(max_length=32, choices=CLIENT_TYPES) |
71 | 70 | authorization_grant_type = models.CharField( |
@@ -125,12 +124,29 @@ def redirect_uri_allowed(self, uri): |
125 | 124 |
|
126 | 125 | def clean(self): |
127 | 126 | from django.core.exceptions import ValidationError |
128 | | - if not self.redirect_uris \ |
129 | | - and self.authorization_grant_type \ |
130 | | - in (AbstractApplication.GRANT_AUTHORIZATION_CODE, |
131 | | - AbstractApplication.GRANT_IMPLICIT): |
132 | | - error = _("Redirect_uris could not be empty with {grant_type} grant_type") |
133 | | - raise ValidationError(error.format(grant_type=self.authorization_grant_type)) |
| 127 | + |
| 128 | + grant_types = ( |
| 129 | + AbstractApplication.GRANT_AUTHORIZATION_CODE, |
| 130 | + AbstractApplication.GRANT_IMPLICIT, |
| 131 | + ) |
| 132 | + |
| 133 | + redirect_uris = self.redirect_uris.strip().split() |
| 134 | + allowed_schemes = set(s.lower() for s in self.get_allowed_schemes()) |
| 135 | + |
| 136 | + if redirect_uris: |
| 137 | + validator = RedirectURIValidator(WildcardSet()) |
| 138 | + for uri in redirect_uris: |
| 139 | + validator(uri) |
| 140 | + scheme = urlparse(uri).scheme |
| 141 | + if scheme not in allowed_schemes: |
| 142 | + raise ValidationError(_( |
| 143 | + "Unauthorized redirect scheme: {scheme}" |
| 144 | + ).format(scheme=scheme)) |
| 145 | + |
| 146 | + elif self.authorization_grant_type in grant_types: |
| 147 | + raise ValidationError(_( |
| 148 | + "redirect_uris cannot be empty with grant_type {grant_type}" |
| 149 | + ).format(grant_type=self.authorization_grant_type)) |
134 | 150 |
|
135 | 151 | def get_absolute_url(self): |
136 | 152 | return reverse("oauth2_provider:detail", args=[str(self.id)]) |
|
0 commit comments