Is it legal for Poland to have Gdansk?
Sure.
The legality has never been in question: initially, from 1945, Poland's control over Gdańsk was an administrative right sanctioned by the victorious powers (The Potsdam Agreement, Sections IX and XIII), and then the Treaty between the Federal Republic of Germany and the People's Republic of Poland (7 December 1970) recognized the Oder-Neisse line as Poland's western border, with both parties renouncing territorial claims.
Moreover, the definitive international settlement came with the Treaty on the Final Settlement with Respect to Germany (12 September 1990) - often called the Two Plus Four Treaty (two German states + four Allied powers: US, USSR, UK, France). Article 1(2) of the Treaty states that the borders of Germany shall be those of the Federal Republic of Germany and the German Democratic Republic combined, and they shall be final. Germany confirmed that it had no territorial claims beyond those borders.
The Federal Republic of Germany thereafter recognized Polish sovereignty over all territories east of the Oder-Neisse line, including Gdańsk, in the German-Polish Border Treaty (14 November 1990).
Today,
Gdańsk is unequivocally Polish territory under international law.
Sovereignty recognized by:
1. The United Nations Charter (principles of territorial integrity and inviolability of frontiers),
2. The 1990 Two Plus Four Treaty (binding on all relevant former belligerents),
3. The 1990 German-Polish Border Treaty, and
4. Widespread international recognition and state practice.
No state, including Germany, contests Poland's sovereignty over Gdańsk.
The matter is legally final and settled.
Of course, in today's f*cked-up world, with Russia on a rampage, China trying to dominate the world and USA going fascist, international law doesn't mean a lot. But still, you asked about legality.