Skip to content

Commit 64d60f0

Browse files
committed
feat(oauth2): add authorization code for any grant type
1 parent b19d5fc commit 64d60f0

7 files changed

Lines changed: 57 additions & 13 deletions

File tree

charts/passport/templates/passport-job-upgrade.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ spec:
3737
- |
3838
python -u /workspace/manage.py cluster_lock lock
3939
python -u /workspace/manage.py migrate --noinput
40+
if [ "${ADMIN_USERNAME}" ] && [ "${ADMIN_PASSWORD}" ] && [ "${ADMIN_EMAIL}" ]; then
41+
echo "Create administrator"
42+
python /workspace/manage.py createadminuser --username "${ADMIN_USERNAME}" --password "${ADMIN_PASSWORD}" --noinput --email "${ADMIN_EMAIL}"
43+
fi
44+
python /workspace/manage.py create_oauth2_application --path /etc/drycc/passport/init-applications.json
4045
python -u /workspace/manage.py cluster_lock unlock
4146
{{- end }}
4247
{{- include "passport.limits" . | indent 8 }}

rootfs/api/management/commands/create_oauth2_application.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import pathlib
66
from django.contrib.auth import get_user_model
77
from django.core.management.base import BaseCommand
8-
from oauth2_provider.models import Application
8+
from oauth2_provider.models import get_application_model
99

1010

1111
User = get_user_model()
12+
Application = get_application_model()
1213
secrets_path = "/var/run/secrets/drycc/passport"
1314

1415

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Generated by Django 4.2.10 on 2024-04-11 08:22
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
import oauth2_provider.generators
7+
import oauth2_provider.models
8+
9+
10+
class Migration(migrations.Migration):
11+
12+
dependencies = [
13+
('api', '0001_initial'),
14+
]
15+
16+
operations = [
17+
migrations.CreateModel(
18+
name='Application',
19+
fields=[
20+
('id', models.BigAutoField(primary_key=True, serialize=False)),
21+
('client_id', models.CharField(db_index=True, default=oauth2_provider.generators.generate_client_id, max_length=100, unique=True)),
22+
('redirect_uris', models.TextField(blank=True, help_text='Allowed URIs list, space separated')),
23+
('post_logout_redirect_uris', models.TextField(blank=True, help_text='Allowed Post Logout URIs list, space separated')),
24+
('client_type', models.CharField(choices=[('confidential', 'Confidential'), ('public', 'Public')], max_length=32)),
25+
('authorization_grant_type', models.CharField(choices=[('authorization-code', 'Authorization code'), ('implicit', 'Implicit'), ('password', 'Resource owner password-based'), ('client-credentials', 'Client credentials'), ('openid-hybrid', 'OpenID connect hybrid')], max_length=32)),
26+
('client_secret', oauth2_provider.models.ClientSecretField(blank=True, db_index=True, default=oauth2_provider.generators.generate_client_secret, help_text='Hashed on Save. Copy it now if this is a new secret.', max_length=255)),
27+
('name', models.CharField(blank=True, max_length=255)),
28+
('skip_authorization', models.BooleanField(default=False)),
29+
('created', models.DateTimeField(auto_now_add=True)),
30+
('updated', models.DateTimeField(auto_now=True)),
31+
('algorithm', models.CharField(blank=True, choices=[('', 'No OIDC support'), ('RS256', 'RSA with SHA-2 256'), ('HS256', 'HMAC with SHA-2 256')], default='', max_length=5)),
32+
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s', to=settings.AUTH_USER_MODEL)),
33+
],
34+
options={
35+
'abstract': False,
36+
},
37+
),
38+
]

rootfs/api/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
from django.contrib.auth.models import AbstractUser
33
from django.utils.translation import gettext_lazy as _
44

5+
from oauth2_provider.models import AbstractApplication
6+
57

68
class User(AbstractUser):
79
email = models.EmailField(_('email address'), unique=True)
10+
11+
12+
class Application(AbstractApplication):
13+
14+
def allows_grant_type(self, *grant_types):
15+
return self.GRANT_AUTHORIZATION_CODE in grant_types or super().allows_grant_type(
16+
*grant_types
17+
)

rootfs/api/settings/production.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@
323323
"DEFAULT_SCOPES": ['openid', ],
324324
"DEFAULT_CODE_CHALLENGE_METHOD": 'S256',
325325
}
326+
OAUTH2_PROVIDER_APPLICATION_MODEL = 'api.Application'
326327
# Redis Configuration
327328
DRYCC_REDIS_ADDRS = os.environ.get('DRYCC_REDIS_ADDRS', '127.0.0.1:6379').split(",")
328329
DRYCC_REDIS_PASSWORD = os.environ.get('DRYCC_REDIS_PASSWORD', '')

rootfs/api/views.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from rest_framework.exceptions import AuthenticationFailed
2424
from rest_framework.response import Response
2525
from rest_framework.viewsets import ModelViewSet
26+
from oauth2_provider.models import AccessToken
2627

2728
from api import serializers
2829
from api.forms import AuthenticationForm, RegistrationForm
@@ -261,7 +262,6 @@ def get_queryset(self, *args, **kwargs):
261262

262263

263264
class UserTokensTemplateView(ListViewSet):
264-
from oauth2_provider.models import AccessToken
265265
model = AccessToken
266266
serializer_class = serializers.UserTokensSerializer
267267
order_by = '-created'
@@ -273,7 +273,6 @@ def retrieve(self, request, *args, **kwargs):
273273

274274

275275
class UserTokenDeleteView(ListViewSet):
276-
from oauth2_provider.models import AccessToken
277276
model = AccessToken
278277

279278
def destroy(self, request, *args, **kwargs):

rootfs/bin/boot

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ echo ""
2626
echo "Collect static files in a single location:"
2727
python manage.py collectstatic --noinput
2828

29-
echo ""
30-
if [ "${ADMIN_USERNAME}" ] && [ "${ADMIN_PASSWORD}" ] && [ "${ADMIN_EMAIL}" ]; then
31-
echo "Create administrator"
32-
python /workspace/manage.py createadminuser --username "${ADMIN_USERNAME}" --password "${ADMIN_PASSWORD}" --noinput --email "${ADMIN_EMAIL}"
33-
fi
34-
35-
echo ""
36-
echo "Create application for drycc controller "
37-
python /workspace/manage.py create_oauth2_application --path /etc/drycc/passport/init-applications.json
38-
3929
# spawn a gunicorn server in the background
4030
echo ""
4131
echo "Starting up Gunicorn"

0 commit comments

Comments
 (0)