Login through external API

I use recently Drupal and I would ask if and how is possible to have a customized login through a php code in which I can access through my API at external application.
I’ll explain well:
I have an application that exposes services and has its own db. Among features there is one for authentication that I can use only after getting a required ( to my API) token.
How Can I integrate this architecture in Drupal?

Drupal version: 

Source: https://www.drupal.org/taxonomy/term/22/feed