<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def key_required(group=None, perm=None, keytype=None):
 """ Decorator for key authentication """

    def decorator(f):
        def wrapper(request, *args, **kwargs):
            try:
                validate_key( request, group, perm, keytype )
                return f(request, *args, **kwargs)
            except AccessUnauthorized:
                return HttpResponse401( request )
            except AccessForbidden:
                return HttpResponse403( request )
        return wrapper 
    return decorator