|
| 1 | +import os |
1 | 2 | import json |
2 | 3 | import urllib |
3 | 4 | from typing import Optional |
|
11 | 12 | from appointment import utils |
12 | 13 | from appointment.controller.calendar import CalDavConnector, Tools |
13 | 14 | from appointment.database import models, schemas, repo |
14 | | -from appointment.dependencies.auth import get_subscriber |
| 15 | +from appointment.dependencies.auth import get_subscriber, oauth2_scheme |
15 | 16 | from appointment.dependencies.database import get_db, get_redis |
16 | 17 | from appointment.exceptions.calendar import TestConnectionFailed |
17 | 18 | from appointment.exceptions.misc import UnexpectedBehaviourWarning |
@@ -128,6 +129,96 @@ def caldav_autodiscover_auth( |
128 | 129 | return True |
129 | 130 |
|
130 | 131 |
|
| 132 | +@router.post('/oidc/auth') |
| 133 | +def oidc_autodiscover_auth( |
| 134 | + db: Session = Depends(get_db), |
| 135 | + subscriber: models.Subscriber = Depends(get_subscriber), |
| 136 | + redis_client: Redis = Depends(get_redis), |
| 137 | + token: str = Depends(oauth2_scheme), |
| 138 | +): |
| 139 | + """Connects a principal caldav server through oidc token auth""" |
| 140 | + |
| 141 | + connection_url = os.getenv('TB_ACCOUNTS_CALDAV_URL') |
| 142 | + dns_lookup_cache_key = f'dns:{utils.encrypt(connection_url)}' |
| 143 | + lookup_url = None |
| 144 | + |
| 145 | + if redis_client: |
| 146 | + lookup_url = redis_client.get(dns_lookup_cache_key) |
| 147 | + |
| 148 | + if lookup_url and 'http' not in lookup_url: |
| 149 | + debug_obj = {'url': lookup_url, 'branch': 'CACHE'} |
| 150 | + # Raise and catch the unexpected behaviour warning so we can get proper stacktrace in sentry... |
| 151 | + try: |
| 152 | + sentry_sdk.set_extra('debug_object', debug_obj) |
| 153 | + raise UnexpectedBehaviourWarning(message='Cache incorrect', info=debug_obj) |
| 154 | + except UnexpectedBehaviourWarning as ex: |
| 155 | + sentry_sdk.capture_exception(ex) |
| 156 | + |
| 157 | + # Clear cache for that key |
| 158 | + redis_client.delete(dns_lookup_cache_key) |
| 159 | + |
| 160 | + # Ignore cached result and look it up again |
| 161 | + lookup_url = None |
| 162 | + |
| 163 | + # Do a dns lookup first |
| 164 | + if lookup_url is None: |
| 165 | + parsed_url = urlparse(connection_url) |
| 166 | + lookup_url, ttl = Tools.dns_caldav_lookup(parsed_url.hostname, secure=True) |
| 167 | + # set the cached lookup for the remainder of the dns ttl |
| 168 | + if redis_client and lookup_url: |
| 169 | + redis_client.set(dns_lookup_cache_key, utils.encrypt(lookup_url), ex=ttl) |
| 170 | + else: |
| 171 | + # Extract the cached value |
| 172 | + lookup_url = utils.decrypt(lookup_url) |
| 173 | + |
| 174 | + # If we have a lookup_url then apply it |
| 175 | + if lookup_url and 'http' not in lookup_url: |
| 176 | + connection_url = urllib.parse.urljoin(connection_url, lookup_url) |
| 177 | + elif lookup_url: |
| 178 | + connection_url = lookup_url |
| 179 | + |
| 180 | + con = CalDavConnector( |
| 181 | + db=db, |
| 182 | + redis_instance=None, |
| 183 | + url=connection_url, |
| 184 | + subscriber_id=subscriber.id, |
| 185 | + calendar_id=None, |
| 186 | + token=token, |
| 187 | + ) |
| 188 | + |
| 189 | + try: |
| 190 | + if not con.test_connection(): |
| 191 | + raise RemoteCalendarConnectionError() |
| 192 | + except TestConnectionFailed as ex: |
| 193 | + raise RemoteCalendarConnectionError(reason=ex.reason) |
| 194 | + |
| 195 | + caldav_name = subscriber.email |
| 196 | + caldav_id = json.dumps([connection_url, caldav_name]) |
| 197 | + |
| 198 | + external_connection = repo.external_connection.get_by_type( |
| 199 | + db, subscriber.id, models.ExternalConnectionType.caldav, caldav_id |
| 200 | + ) |
| 201 | + |
| 202 | + # Create or update the external connection |
| 203 | + if not external_connection: |
| 204 | + external_connection_schema = schemas.ExternalConnection( |
| 205 | + name=caldav_name, |
| 206 | + type=models.ExternalConnectionType.caldav, |
| 207 | + type_id=caldav_id, |
| 208 | + owner_id=subscriber.id, |
| 209 | + token=token, |
| 210 | + ) |
| 211 | + |
| 212 | + external_connection = repo.external_connection.create(db, external_connection_schema) |
| 213 | + else: |
| 214 | + external_connection = repo.external_connection.update_token( |
| 215 | + db, token, subscriber.id, models.ExternalConnectionType.caldav, caldav_id |
| 216 | + ) |
| 217 | + |
| 218 | + con.sync_calendars(external_connection_id=external_connection.id) |
| 219 | + return True |
| 220 | + |
| 221 | + |
131 | 222 | @router.post('/', response_model=schemas.CalendarOut) |
132 | 223 | def create_my_calendar( |
133 | 224 | calendar: schemas.CalendarConnection, |
|
0 commit comments