tpitale / mail_room

Forward mail from gmail IMAP to a callback URL or job worker, simply.
MIT License
193 stars 51 forks source link

Add Jwt authentication support to postback delivery method #134

Closed 0x2c7 closed 2 years ago

0x2c7 commented 2 years ago

Hi there 👋

I'm looking forward to add JWT token support to postback delivery method. This change is essential for an initiative at GitLab to convert the push-directly-to-redis way to webhook style. The full context is available at https://gitlab.com/groups/gitlab-com/gl-infra/-/epics/644.

The mailroom gem already supports postback strategy that delivers the mail contents via HTTP request. However, mail_room gem uses Faraday and its token_auth authentication method. This method adds a configured token to Authorization header: Authorization: Token token="something". Unfortunately, we are following a different approach for authentication. We are using JWT tokens for internal APIs. The token is generated using HS256 algorithm using a shard symmetric secret file. The tokens are then embedded into a custom request header. Since GitLab as a whole is a collection of different services, each service uses a different secret and embeds into a different header.

I would like to propose to add the following configurations to the postback delivery method:

    :delivery_options:
      :delivery_url: "http://localhost:3000/inbox"
      :jwt_auth_header: "Gitlab-Mailroom-Api-Request"
      :jwt_issuer: "gitlab-mailroom"
      :jwt_algorithm: "HS256"
      :jwt_secret_path: "/etc/gitlab-secrets/mailroom/.gitlab_mailroom_secret"

The request headers look something like:

{ 
  "Gitlab-Mailroom-Api-Request" =>  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
}

The meanings of each configuration is self-explanatory. At the moment, I implemented just subset of JWT reserved claims and haven't supported custom payload. I would love to add more to make the solution becomes more useful for everyone. As a result, the official JWT gem is added as a dependency of this gem.

cc @stanhu

tpitale commented 2 years ago

Looks good so far @nguyenquangminh0711

0x2c7 commented 2 years ago

@tpitale @stanhu Sorry for the delay, as I was on a long holiday. Could you have a look again?

stanhu commented 2 years ago

Looks good to me, thanks!