A unified framework for the Kondo problem and for an impurity in a Luttinger liquid