cds-snc / url-shortener

An API written in Python that shortens URLs
MIT License
4 stars 1 forks source link

Create API key for Notify to use #192

Closed dinophile closed 1 year ago

dinophile commented 1 year ago

Need to manually create a key for Notify only. Will be used as a github secret for MVP.

cgye commented 1 year ago

Likely 3 keys are required:

Request

The API Key should be specified in HTTP Authorization header as a Bearer token, see: rfc6750 and rfc7325:

   Authorization: Bearer <api key>

The API Key is base64 encoded (see: here)

Response

The WWW-Authenticate response header field must be used for authentication related error when a bearer token is used (see: here).

Error Codes

When a request fails, the respond contains an HTTP status code and includes one of the following error codes in the response:

Furthermore, the error and error_description fields will be used to provide additional information about an error.

     HTTP/1.1 401 Unauthorized
     WWW-Authenticate: Bearer realm="UrlShortener",
                       error="invalid_token",
                       error_description="The api key is invalid"

If the request lacks any authentication information (e.g., the client was unaware that authentication is necessary or attempted using an unsupported authentication method), the resource server SHOULD NOT include an error code or other error information. For example:

     HTTP/1.1 401 Unauthorized
     WWW-Authenticate: Bearer realm="UrlShortener"

Notes

cgye commented 1 year ago

@sylviamclaughlin @dinophile @maxneuvians If you have some time, can you review the above spec for API key? Thx. Will also update this api doc when finalized: https://github.com/cds-snc/url-shortener/pull/196

sylviamclaughlin commented 1 year ago

@cgye - the spec looks good to me. Don't know if you thought about implementation yet, but Fastapi does provide some innate functionality for handling api tokens that you might want to take a look if you deem appropriate.

cgye commented 1 year ago

@cgye - the spec looks good to me. Don't know if you thought about implementation yet, but Fastapi does provide some innate functionality for handling api tokens that you might want to take a look if you deem appropriate.

Thanks for the link. Yes, this should make implementation easier :)

maxneuvians commented 1 year ago

LGTM as well! I think the JWT route is interesting in terms of that requests after the initial authentication request have a time bound token. Having a fixed API key take away some complexity on the client side ex. reauthenticating after the JWT expires. In the past we just used a straight up fixed API key https://github.com/cds-snc/scan-websites/blob/main/api/api_gateway/routers/scans.py#L64-L75 but happy to also try out the JWT route, if you think it is worth it for the additional security.

patheard commented 1 year ago

This has now been added. You can test the various behaviour for the /v1 shorten route with the following:

# Success
curl -v -X POST https://url-shortener.cdssandbox.xyz/v1 \
-H "Authorization: Bearer $VALID_AUTH_TOKEN" \
-H "Content-Type: application/json" \
-d '{"original_url": "https://digital.canada.ca"}'

# Failed auth
curl -v -X POST https://url-shortener.cdssandbox.xyz/v1 \
-H "Authorization: Bearer foo" \
-H "Content-Type: application/json" \
-d '{"original_url": "https://digital.canada.ca"}'

# Missing auth
curl -v -X POST https://url-shortener.cdssandbox.xyz/v1 \
-H "Content-Type: application/json" \
-d '{"original_url": "https://digital.canada.ca"}'

As for implementation:

cgye commented 1 year ago

Cool, thanks @patheard !

maxneuvians commented 1 year ago

Closed in #210, #212. #213