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

    def update_last_access():
        if KEY_LAST_USED_UPDATE:
            request.key.save()
    
    if request.user.is_authenticated() and is_valid_consumer(request):
        if not group and not perm and not keytype:
            return update_last_access()
        elif keytype:
            if request.key.is_type( keytype ):
                return update_last_access()
        elif group:
            if request.key.belongs_to_group( group ):
                return update_last_access()
        elif perm:
            if request.key.has_perm( perm ):
                return update_last_access()
        raise AccessForbidden
    raise AccessUnauthorized