@@ -338,6 +338,49 @@ def authenticate(
338338 raise exceptions .AuthenticationFailed (self .auth_failed_message )
339339
340340
341+ class TestAnalyticsTokenlessAuthentication (TokenlessAuthentication ):
342+ def _get_info_from_request_path (
343+ self , request : HttpRequest
344+ ) -> tuple [Repository , str | None ]:
345+ try :
346+ body = json .loads (str (request .body , "utf8" ))
347+
348+ # Validate provider
349+ service_enum = Service (body .get ("service" ))
350+
351+ # Validate that next group exists and decode slug
352+ repo = get_repository_from_string (service_enum , body .get ("slug" ))
353+ if repo is None :
354+ # Purposefully using the generic message so that we don't tell that
355+ # we don't have a certain repo
356+ raise exceptions .AuthenticationFailed (self .auth_failed_message )
357+
358+ return repo , body .get ("commit" )
359+ except json .JSONDecodeError :
360+ # Validate request body format
361+ raise exceptions .AuthenticationFailed (self .auth_failed_message )
362+ except ValueError :
363+ # Validate provider
364+ raise exceptions .AuthenticationFailed (self .auth_failed_message )
365+
366+ def get_branch (
367+ self ,
368+ request : HttpRequest ,
369+ repoid : Optional [int ] = None ,
370+ commitid : Optional [str ] = None ,
371+ ) -> str :
372+ body = json .loads (str (request .body , "utf8" ))
373+
374+ # If commit is not created yet (ie first upload for this commit), we just validate branch format.
375+ # However, if a commit exists already (ie not the first upload for this commit), we must additionally
376+ # validate the saved commit branch matches what is requested in this upload call.
377+ commit = Commit .objects .filter (repository_id = repoid , commitid = commitid ).first ()
378+ if commit and commit .branch != body .get ("branch" ):
379+ raise exceptions .AuthenticationFailed (self .auth_failed_message )
380+
381+ return body .get ("branch" )
382+
383+
341384class BundleAnalysisTokenlessAuthentication (TokenlessAuthentication ):
342385 def _get_info_from_request_path (
343386 self , request : HttpRequest
0 commit comments