# Auth API microservice
The application allows validating and decode JSON Web Tokens (JWT). Additional functionality is feature toggles integration (WIP).

## Ownership
Apollo Team and Core Team shared ownership.

## How it Works ?
Services or api gateway send a request to /authorize endpoint to validate token.
If the token valid and this is the enduser`s token, API returns the following headers, that are necessary for user
specific calls:
- X-User-Id with user-id
- X-Userinfo with token payload
- X-Client-Id with "sub" field from the token
If the token is valid and this is the m2m token, API returns the following headers:
- X-Client-Id with "sub" field from the token
Bonus: split.io treeaments returns as well (for particular user)
If token invalid, client gets an error with description.

### Supported auth providers
1. [Auth0](https://auth0.com)
2. [AtlasUM](https://um.atlas.stream)

### Under the hood:
1. Integration with Sentry and DataDog
2. Feature toggling functionality (integration with split.io)
3. Decoding algorithm is "RS256"
4. Public key is getting from HOST/.well-known/jwks.json or the .pem file on the network

## How to configure
Be sure that all env variables are in place, check **.env** file (.env.sample).
For instance:

    AUTH0_DOMAIN=sme-dna.auth0.com,dev-login.apollo.stream
    API_AUDIENCE=https://apollo-private-api/

Run the command in project directory:

    make docker/local/up

To check that everything works property, execute the command:
    ```
    curl 'http://127.0.0.1:8000/authorize' --header 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlJFWXdRMFEzTjBWRk1URTVOVFEwU'
    ```

It is also possible to test the full chain with Traefik edge router. Configure needed env vars for the expected auth
provider (e.g. ATLAS_PUBLIC_KEY_URL) and then execute the command:
    ```
    curl 'http://127.0.0.1:8002' --header 'Authorization: Bearer <some Atlas token>'
    ```

Expected result:
    ```
    {"code":"invalid_token","description":"Error decoding token headers"}
    ```

## How to Test
    make test

Before you push code and create a PR, run the following command:

    make pre-commit
