Do the professors in Germany have the authority to issue contract or hire a PhD students directly with a formal contract without previous advertisement?
Do PhD positions in Germany need to be formally advertised?
tl;dr: Theoretically uncertain, practically yes.
PhD students in Germany are generally* employed as civil service employees (Angestellte des öffentlichen Dienstes). For civil service employees, the law company Gloistein & Partner on their website gives a nice summary of various, partially conflicting judgements (see "2. Ausschreibung von Arbeitsplätzen im öffentlichen Dienst" - use Google Translate as required). An important one is from the Federal Administrative Court (Bundesverwaltungsgericht), which rejects a general responsibility to formally advertise such positions, but leaves open the possibility that such responsibilities can arise in practice, for example, if an institution regularly formally advertises its open positions.
HR people at public institutions in Germany are typically keen to "cover their asses" and avoid uncertain legal situations. Therefore, in practice, very most if not all PhD positions are formally advertised.
* Actually, PhD students do not need to be employed, but most are (at least in STEM fields), and then they are always employed as civil service employees. Typical alternatives to employment are stipends and being self-funded.