APY-13 : Complete the security gateway

General refactoring.