def authenticate(self, request):
try:<EOL><INDENT>oauth_request = oauth_provider.utils.get_oauth_request(request)<EOL><DEDENT>except oauth.Error as err:<EOL><INDENT>raise exceptions.AuthenticationFailed(err.message)<EOL><DEDENT>if not oauth_request:<EOL><INDENT>return None<EOL><DEDENT>oauth_params = oauth_provider.consts.OAUTH_PARAMETERS_NAMES<EOL>found = any(param for param in oauth_params if param in oauth_request)<EOL>missing = list(param for param in oauth_params if param not in oauth_request)<EOL>if not found:<EOL><INDENT>return None<EOL><DEDENT>if missing:<EOL><INDENT>msg = '<STR_LIT>' % ('<STR_LIT:U+002CU+0020>'.join(missing))<EOL>raise exceptions.AuthenticationFailed(msg)<EOL><DEDENT>if not self.check_nonce(request, oauth_request):<EOL><INDENT>msg = '<STR_LIT>'<EOL>raise exceptions.AuthenticationFailed(msg)<EOL><DEDENT>try:<EOL><INDENT>consumer_key = oauth_request.get_parameter('<STR_LIT>')<EOL>consumer = oauth_provider_store.get_consumer(request, oauth_request, consumer_key)<EOL><DEDENT>except oauth_provider.store.InvalidConsumerError:<EOL><INDENT>msg = '<STR_LIT>' % oauth_request.get_parameter('<STR_LIT>')<EOL>raise exceptions.AuthenticationFailed(msg)<EOL><DEDENT>if consumer.status != oauth_provider.consts.ACCEPTED:<EOL><INDENT>msg = '<STR_LIT>' % consumer.get_status_display()<EOL>raise exceptions.AuthenticationFailed(msg)<EOL><DEDENT>try:<EOL><INDENT>token_param = oauth_request.get_parameter('<STR_LIT>')<EOL>token = oauth_provider_store.get_access_token(request, oauth_request, consumer, token_param)<EOL><DEDENT>except oauth_provider.store.InvalidTokenError:<EOL><INDENT>msg = '<STR_LIT>' % oauth_request.get_parameter('<STR_LIT>')<EOL>raise exceptions.AuthenticationFailed(msg)<EOL><DEDENT>try:<EOL><INDENT>self.validate_token(request, consumer, token)<EOL><DEDENT>except oauth.Error as err:<EOL><INDENT>raise exceptions.AuthenticationFailed(err.message)<EOL><DEDENT>user = token.user<EOL>if not user.is_active:<EOL><INDENT>msg = '<STR_LIT>' % user.username<EOL>raise exceptions.AuthenticationFailed(msg)<EOL><DEDENT>return (token.user, token)<EOL>
Returns two-tuple of (user, token) if authentication succeeds,
or None otherwise.