Skip to content

Commit 011bb39

Browse files
committed
feat(ocid): use drycc ocid discover url
1 parent 90da4cb commit 011bb39

8 files changed

Lines changed: 25 additions & 76 deletions

File tree

rootfs/api/apps_extra/social_core/actions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def form2json(form_data):
3939
query = urlparse("?" + form_data).query
4040
params = parse_qs(query)
4141
return {key: params[key][0] for key in params}
42-
from api.backend import OauthCacheManager
42+
from api.apps_extra.social_core.backends import OauthCacheManager
4343
oauth_cache_manager = OauthCacheManager()
4444
oauth_cache_manager.set_state(data.get("key", ""), form2json(url).get("state"))
4545
return response
@@ -129,7 +129,7 @@ def do_complete(backend, login, user=None, redirect_name="next", *args, **kwargs
129129
if social_auth and social_auth.extra_data:
130130
extra_data = json.loads(social_auth.extra_data) if \
131131
isinstance(social_auth.extra_data, str) else social_auth.extra_data
132-
from api.backend import OauthCacheManager
132+
from api.apps_extra.social_core.backends import OauthCacheManager
133133
oauth_cache_manager = OauthCacheManager()
134134
oauth_cache_manager.set_token(data.get("state"), extra_data)
135135
return response

rootfs/api/backend.py renamed to rootfs/api/apps_extra/social_core/backends.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from django.contrib.auth import get_user_model
55
from django.utils.translation import gettext_lazy
66

7-
from social_core.utils import cache as social_cache
87
from social_core.backends.open_id_connect import OpenIdConnectAuth
98
from rest_framework import exceptions
109

@@ -16,12 +15,6 @@
1615
class DryccOIDC(OpenIdConnectAuth):
1716
"""Drycc Openid Connect authentication backend"""
1817
name = 'drycc'
19-
AUTHORIZATION_URL = settings.SOCIAL_AUTH_DRYCC_AUTHORIZATION_URL
20-
ACCESS_TOKEN_URL = settings.SOCIAL_AUTH_DRYCC_ACCESS_TOKEN_URL
21-
USERINFO_URL = settings.SOCIAL_AUTH_DRYCC_USERINFO_URL
22-
JWKS_URI = settings.SOCIAL_AUTH_DRYCC_JWKS_URI
23-
OIDC_ENDPOINT = settings.SOCIAL_AUTH_DRYCC_OIDC_ENDPOINT
24-
DEFAULT_SCOPE = ['openid', 'profile', 'email']
2518
EXTRA_DATA = [
2619
('id', 'id'),
2720
('access_token', 'access_token'),
@@ -32,20 +25,10 @@ class DryccOIDC(OpenIdConnectAuth):
3225
('scope', 'scope'),
3326
]
3427

35-
def __init__(self, *args, **kwargs):
36-
super().__init__(*args, **kwargs)
37-
38-
@social_cache(ttl=86400)
39-
def oidc_config(self):
40-
return self.get_json(
41-
self.OIDC_ENDPOINT + '/.well-known/openid-configuration/'
42-
)
43-
4428
def get_user_data(self, access_token):
4529
"""Loads user data from service"""
46-
url = settings.SOCIAL_AUTH_DRYCC_USERINFO_URL
4730
response = self.get_json(
48-
url,
31+
self.userinfo_url(),
4932
headers={
5033
'authorization': 'Bearer ' + access_token
5134
},
@@ -62,12 +45,13 @@ def get_user_data(self, access_token):
6245
}
6346

6447
def refresh_token(self, refresh_token):
48+
# Get token URL from OIDC discovery if not already cached
6549
return self.get_json(
66-
settings.SOCIAL_AUTH_DRYCC_ACCESS_TOKEN_URL,
50+
self.access_token_url(),
6751
method='POST',
6852
data={
6953
'grant_type': 'refresh_token',
70-
'client_id': settings.SOCIAL_AUTH_DRYCC_KEY,
54+
'client_id': self.get_key_and_secret()[0],
7155
'refresh_token': refresh_token,
7256
},
7357
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from api.serializers import UserSerializer
2+
3+
4+
def update_or_create(backend, user, response, *args, **kwargs):
5+
user, created = UserSerializer.update_or_create(response)
6+
return {'is_new': created, 'user': user}

rootfs/api/authentication.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def authenticate(self, request):
4747
return None
4848
try:
4949
if token_type == 'bearer': # drycc oauth access token
50-
from api.backend import OauthCacheManager
50+
from api.apps_extra.social_core.backends import OauthCacheManager
5151
return OauthCacheManager().get_user(token), token
5252
# drycc token
5353
user = cache.get(token, None)
@@ -69,7 +69,7 @@ def authenticate_credentials(self, key):
6969
raise exceptions.AuthenticationFailed(gettext_lazy('User inactive or deleted.'))
7070
if token.expires():
7171
try:
72-
from api.backend import OauthCacheManager
72+
from api.apps_extra.social_core.backends import OauthCacheManager
7373
user = OauthCacheManager().get_user(token.oauth['access_token'])
7474
cache.set(key, user, timeout=token.oauth['expires_in'])
7575
token.refresh_token()

rootfs/api/models/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def generate_key(cls):
120120
) + ''.join(random.choices(string.ascii_letters, k=96))
121121

122122
def refresh_token(self):
123-
from api.backend import DryccOIDC
123+
from api.apps_extra.social_core.backends import DryccOIDC
124124
drycc_open_connect = DryccOIDC()
125125
self.oauth = drycc_open_connect.refresh_token(self.oauth['refresh_token'])
126126
self.save()

rootfs/api/pipeline.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

rootfs/api/settings/production.py

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ def randstr(k):
127127
AUTHENTICATION_BACKENDS = (
128128
"django.contrib.auth.backends.ModelBackend",
129129
"guardian.backends.ObjectPermissionBackend",
130+
"api.apps_extra.social_core.backends.DryccOIDC",
130131
)
131132
GUARDIAN_GET_INIT_ANONYMOUS_USER = 'api.models.base.get_anonymous_user_instance'
132133
ANONYMOUS_USER_NAME = os.environ.get('ANONYMOUS_USER_NAME', 'AnonymousUser')
@@ -431,54 +432,31 @@ def randstr(k):
431432
f'{DRYCC_PASSPORT_URL}/user/login/done/',
432433
)
433434

435+
# social auth settings
434436
SOCIAL_AUTH_DRYCC_KEY = os.environ.get(
435437
"DRYCC_PASSPORT_KEY",
436438
os.environ.get("SOCIAL_AUTH_DRYCC_KEY"),
437439
)
438-
439440
SOCIAL_AUTH_DRYCC_SECRET = os.environ.get(
440441
'DRYCC_PASSPORT_SECRET',
441442
os.environ.get("SOCIAL_AUTH_DRYCC_SECRET"),
442443
)
443-
444-
SOCIAL_AUTH_DRYCC_AUTHORIZATION_URL = os.environ.get(
445-
'SOCIAL_AUTH_DRYCC_AUTHORIZATION_URL',
446-
f'{DRYCC_PASSPORT_URL}/oauth/authorize/',
447-
)
448-
SOCIAL_AUTH_DRYCC_ACCESS_TOKEN_URL = os.environ.get(
449-
'SOCIAL_AUTH_DRYCC_ACCESS_TOKEN_URL',
450-
f'{DRYCC_PASSPORT_URL}/oauth/token/'
451-
)
452-
SOCIAL_AUTH_DRYCC_ACCESS_API_URL = os.environ.get(
453-
'SOCIAL_AUTH_DRYCC_ACCESS_API_URL',
454-
f'{DRYCC_PASSPORT_URL}'
455-
)
456-
SOCIAL_AUTH_DRYCC_USERINFO_URL = os.environ.get(
457-
'SOCIAL_AUTH_DRYCC_USERINFO_URL',
458-
f'{DRYCC_PASSPORT_URL}/oauth/userinfo/'
459-
)
460-
SOCIAL_AUTH_DRYCC_JWKS_URI = os.environ.get(
461-
'SOCIAL_AUTH_DRYCC_JWKS_URI',
462-
f'{DRYCC_PASSPORT_URL}/oauth/.well-known/jwks.json'
463-
)
464444
SOCIAL_AUTH_DRYCC_OIDC_ENDPOINT = os.environ.get(
465445
'SOCIAL_AUTH_DRYCC_OIDC_ENDPOINT',
466446
f'{DRYCC_PASSPORT_URL}/oauth'
467447
)
468-
469448
SOCIAL_AUTH_JSONFIELD_ENABLED = True
470449
SOCIAL_AUTH_PIPELINE = (
471450
'social_core.pipeline.social_auth.social_details',
472451
'social_core.pipeline.social_auth.social_uid',
473452
'social_core.pipeline.social_auth.social_user',
474453
'social_core.pipeline.user.get_username',
475454
'social_core.pipeline.social_auth.associate_by_email',
476-
'api.pipeline.update_or_create',
455+
'api.apps_extra.social_core.pipelines.update_or_create',
477456
'social_core.pipeline.social_auth.associate_user',
478457
'social_core.pipeline.social_auth.load_extra_data',
479458
'social_core.pipeline.user.user_details',
480459
)
481-
AUTHENTICATION_BACKENDS = ("api.backend.DryccOIDC", ) + AUTHENTICATION_BACKENDS
482460
DRYCC_CACHE_USER_TIME = int(os.environ.get('DRYCC_CACHE_USER_TIME', 30 * 60))
483461

484462
# Cache Valkey Configuration

rootfs/api/views.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from social_django.views import _do_login
4848
from social_core.utils import setting_name
4949
from api import admissions, utils
50-
from api.backend import OauthCacheManager
50+
from api.apps_extra.social_core.backends import OauthCacheManager
5151
from api.apps_extra.social_core.actions import do_auth, do_complete
5252

5353

@@ -122,12 +122,15 @@ def _create_browser_response(self, key):
122122
return redirect(f"{uri[0:uri.find(self.request.path)]}/v2/login/drycc/?key={key}")
123123

124124
def _create_interactive_response(self, username, password, key):
125+
# Get token endpoint from OIDC discovery
126+
token_url = oauth_cache_manager.drycc_oauth.access_token_url()
127+
client_id, client_secret = oauth_cache_manager.drycc_oauth.get_key_and_secret()
125128
response = requests.post(
126-
settings.SOCIAL_AUTH_DRYCC_ACCESS_TOKEN_URL,
129+
token_url,
127130
data={
128131
'grant_type': 'password',
129-
'client_id': settings.SOCIAL_AUTH_DRYCC_KEY,
130-
'client_secret': settings.SOCIAL_AUTH_DRYCC_SECRET,
132+
'client_id': client_id,
133+
'client_secret': client_secret,
131134
'username': username,
132135
'password': password,
133136
},

0 commit comments

Comments
 (0)