<s> from __future__ import absolute_import <EOL> from django . contrib . auth . models import AnonymousUser <EOL> from rest_framework . authentication import BasicAuthentication <EOL> from rest_framework . exceptions import AuthenticationFailed <EOL> from sentry import options <EOL> from sentry . app import raven <EOL> from sentry . models import ApiKey <EOL> from sentry . models . apikey import ROOT_KEY <EOL> from django . utils . crypto import constant_time_compare <EOL> class QuietBasicAuthentication ( BasicAuthentication ) : <EOL> def authenticate_header ( self , request ) : <EOL> return '<STR_LIT>' % self . www_authenticate_realm <EOL> class ApiKeyAuthentication ( QuietBasicAuthentication ) : <EOL> def authenticate_credentials ( self , userid , password ) : <EOL> if password : <EOL> return <EOL> root_api_key = options . get ( '<STR_LIT>' ) <EOL> if root_api_key : <EOL> if constant_time_compare ( root_api_key , userid ) : <EOL> return ( None , ROOT_KEY ) <EOL> try : <EOL> key = ApiKey . objects . get_from_cache ( key = userid ) <EOL> except ApiKey . DoesNotExist : <EOL> raise AuthenticationFailed ( '<STR_LIT>' ) <EOL> if not key . is_active : <EOL> raise AuthenticationFailed ( '<STR_LIT>' ) <EOL> raven . tags_context ( { <EOL> '<STR_LIT>' : userid , <EOL> } ) <EOL> return ( AnonymousUser ( ) , key ) </s>
