Their legal basis is the contract you signed to repay the fees.

Such agreements may be restricted, depending upon where you live, by state employment laws.