My bosses force me to add an author to my paper who didn't contribute to it
There are a couple of clauses in your question which you may want to reconsider.
- I worked completely independent — did you? You wrote a paper working on your own, which is indeed a big achievement. However, you've been working with data, and the data have been obtained by A in the lab run by X. This definitely does not sound like a completely independent work to me.
- X contributed nothing to the work — did s/he not? They set up and run a lab, where the data have been obtained. Could you write the same paper without the data A collected? Could A collect the data without X's lab?
The arguments above do not necessarily mean that X should co-author the paper, but this is a matter of agreement between all collaborators. In ideal world, this should be discussed before the project is started; in reality, discussions like you've had are often held too late into the process, and the frustration you're feeling is understandable. Be strong — you will be a completely independent researcher one day very soon, and hopefully you will manage your projects and relations in much more thoughtful and transparent way. Until then it is best not to waste your life in such battles.
Neither of the arguments brought forth to justify X’s authorship is valid. Hosting another researcher’s sabbatical, being the possible author of a reference letter, or being good for somebody’s career are far from valid reasons for authorship.
As others noted, it may be that X actually did something worthy of authorship with respect to acquiring the data. But this poses the question why A and B did not bother to bring this good argument for X’s authorship. Thus we have to assume that this was not the case (or A and B do care so little about authorship ethics that they do not even know how to base arguments on it).
Have I done everything right?
It was A, B, and X who abused their power over you to make you do this and they are the main culprits. Sure, you could have not complied, but at what cost? How much guilt this imposes on you is a question of fundamental ethics and beyond the scope of this platform.
You'd add X for the same reasons you're adding A and B.
A and B are your bosses, and supervise / advise / host you. Probably they also provide a lot of infrastructure and what I would call "institutional knowledge". Are you sure X wasn't doing anything like that for B?
Also, you sound a little bit hostile (because you think X took your ideas). Hostility doesn't get you anywhere, and this isn't a fight where you were going to win anything. Even if this is an ethical dilemma, you made your case and the decision has to be made by A and B.