Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XOAUTH2 authentication #152

Merged
merged 15 commits into from
Jul 26, 2023
Merged

Conversation

robstewart57
Copy link
Contributor

Adds OAuth based authentication.

Fixes #150

This implementation accepts the OAuth token when `mailmerge` prompts
for a password.

To use this OAuth authentication, the `mailmerge_server.conf` should
use `XOAUTH` for security, e.g.

```
[smtp_server]
host = smtp.office365.com
port = 587
security = XOAUTH
username = me@example.com
```

This implementation assumes that an OAuth token has already been
obtained by 3rd party tool using the tenant_id, client_id and
client_secret.

The OAuth integration implemented in this commit encodes the username
and OAuth token in base 64 in SASL XOAUTH2 format.

For reference about this SASL XOAUTH2 encoding, see:

https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth#sasl-xoauth2
Copy link
Owner

@awdeorio awdeorio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great addition! I left a question about encodings.

Would you be willing to add a test? You could copy paste test_security_starttls as a starting point.

mailmerge/sendmail_client.py Outdated Show resolved Hide resolved
@robstewart57
Copy link
Contributor Author

Is ASCII required?

Yes, without .encode("ascii") this type error is thrown:

TypeError: can only concatenate str (not "bytes") to str

I've added exception handling here: 7f31846

Would you be willing to add a test? You could copy paste test_security_starttls as a starting point.

I'm unfamiliar with Python tooling, so I'm unsure how to run the existing tests.

  1. How do I install the dependencies for running the tests?
  2. How do I run the existing tests?
  3. To add a test for this XOAUTH authentication would it literally be copy/pasting the test_security_starttls test?
  4. Do I need to add this new test to a list of tests anywhere?

@awdeorio
Copy link
Owner

Thanks for adding the exception handling. To install deps and run the unit tests check out these commands. You don't need to add anything to a list, just copy paste an old test, change the name, and modify the code a bit.

@robstewart57
Copy link
Contributor Author

Hi @awdeorio , the test_security_xoauth test is added in 406716c .

@codecov-commenter
Copy link

codecov-commenter commented Jul 20, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.02 🎉

Comparison is base (6e3d342) 99.72% compared to head (ac7c89d) 99.74%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #152      +/-   ##
===========================================
+ Coverage    99.72%   99.74%   +0.02%     
===========================================
  Files            5        5              
  Lines          358      386      +28     
===========================================
+ Hits           357      385      +28     
  Misses           1        1              
Impacted Files Coverage Δ
mailmerge/__main__.py 99.27% <ø> (ø)
mailmerge/sendmail_client.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Owner

@awdeorio awdeorio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a bit of refactoring to satisfy the linter. The addition of the XOAUTH code had made the enclosing function too complicated, so I split it up. I also added a check to the unit test to verify the formatting of the username/password string.

Would you be able to test this updated code on a real server just to be extra sure?

@awdeorio awdeorio changed the title Wip xoauth Add XOAUTH2 authentication Jul 20, 2023
@robstewart57
Copy link
Contributor Author

Would you be able to test this updated code on a real server just to be extra sure?

@awdeorio I've tested this branch with your latest commits, against smtp.office365.com on port 587.

It sends emails as expected, so I think we're good to go.

@awdeorio
Copy link
Owner

Great! Merging!

@awdeorio awdeorio merged commit e23b607 into awdeorio:develop Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for 2FA OAuth authentication
3 participants