def decode_validate_token(token, sv, api):
    """This tries to be a general function to decode and validade any token.
    Receives a token, a SignerVerifier and an API.
    """
    if not token:
        api.abort(400, 'Error: No token received!')
    try:
        decoded = sv.decode(token)
    except sv.ExpiredSignatureError:
        api.abort(400, 'Error: Expired token!')
    except sv.DecodeError:
        api.abort(400, 'Error: Token decode error!')
    except:
        raise
    for fields in ['username', 'type', 'exp']:
        if fields not in decoded.keys():
            api.abort(400, 'Error: Malformed token! No: %s' % fields)
    return decoded
