The authority, exactly
If you are outside Portugal, the Portuguese consulate or embassy responsible for where you live issues the entry visa first — AIMA takes over once you have entered. AIMA became Portugal's immigration authority on 29 October 2023, succeeding the dissolved Foreigners and Borders Service (SEF).