Skip to content

Commit 963ec4b

Browse files
committed
Make redirect_uri optional for flows that don't use it
1 parent 075752e commit 963ec4b

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

emailproxy.config

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ local_address = 127.0.0.1
8383
[Account setup]
8484
documentation = Accounts are specified using your email address as the section heading (e.g., [[email protected]],
8585
etc, below). Account usernames (i.e., email addresses) must be unique - only one entry per account is permitted.
86-
Each account section must provide values for `permission_url`, `token_url`, `oauth2_scope` and `redirect_uri`. If
87-
you are adding an account for a service other than the examples shown below then the provider's documentation should
88-
provide these details.
86+
Each account section must provide values for at least `token_url`, `oauth2_scope` and `client_id`. Depending on the
87+
OAuth 2.0 flow you are using, other values may also be required (see examples below). If you are adding an account
88+
for a service other than the examples shown below then the provider's documentation should provide these details.
8989

9090
You will also need to add your own `client_id` and `client_secret` values as indicated below. These can either be
9191
reused from an existing source (such as another email client that supports OAuth 2.0), or you can register and use
@@ -228,7 +228,6 @@ documentation = *** note: this is an advanced O365 account example; in most case
228228
token_url = https://login.microsoftonline.com/*** your tenant id here ***/oauth2/v2.0/token
229229
oauth2_scope = https://outlook.office365.com/.default
230230
oauth2_flow = client_credentials
231-
redirect_uri = http://localhost
232231
client_id = *** your client id here ***
233232
client_secret = *** your client secret here ***
234233

@@ -237,7 +236,6 @@ documentation = *** note: this is an advanced O365 account example; in most case
237236
token_url = https://login.microsoftonline.com/*** your tenant id here ***/oauth2/v2.0/token
238237
oauth2_scope = https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/POP.AccessAsUser.All https://outlook.office365.com/SMTP.Send offline_access
239238
oauth2_flow = password
240-
redirect_uri = http://localhost
241239
client_id = *** your client id here ***
242240
client_secret = *** your client secret here ***
243241

@@ -246,7 +244,6 @@ documentation = *** note: this is an advanced Google account example; in most ca
246244
token_url = https://oauth2.googleapis.com/token
247245
oauth2_scope = https://mail.google.com/
248246
oauth2_flow = service_account
249-
redirect_uri = http://localhost
250247
client_id = file
251248
client_secret = *** your /path/to/service-account-key.json here ***
252249

@@ -255,7 +252,6 @@ documentation = *** note: this is an advanced Google account example; in most ca
255252
token_url = https://oauth2.googleapis.com/token
256253
oauth2_scope = https://mail.google.com/
257254
oauth2_flow = service_account
258-
redirect_uri = http://localhost
259255
client_id = key
260256
client_secret = *** your pasted service account JSON key file contents here,
261257
making sure to indent all lines by at least one space ***

emailproxy.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
__author__ = 'Simon Robinson'
77
__copyright__ = 'Copyright (c) 2024 Simon Robinson'
88
__license__ = 'Apache 2.0'
9-
__version__ = '2024-09-07' # ISO 8601 (YYYY-MM-DD)
9+
__version__ = '2024-09-10' # ISO 8601 (YYYY-MM-DD)
1010
__package_version__ = '.'.join([str(int(i)) for i in __version__.split('-')]) # for pyproject.toml usage only
1111

1212
import abc
@@ -725,13 +725,12 @@ def get_oauth2_credentials(username, password, reload_remote_accounts=True):
725725
jwt_certificate_path = AppConfig.get_option_with_catch_all_fallback(config, username, 'jwt_certificate_path')
726726
jwt_key_path = AppConfig.get_option_with_catch_all_fallback(config, username, 'jwt_key_path')
727727

728-
# note that we don't require permission_url here because it is not needed for the client credentials grant flow,
729-
# and likewise for client_secret here because it can be optional for Office 365 configurations
730-
if not (token_url and oauth2_scope and redirect_uri and client_id):
728+
# because the proxy supports a wide range of OAuth 2.0 flows, in addition to the token_url we only mandate the
729+
# core parameters that are required by all methods: oauth2_scope and client_id
730+
if not (token_url and oauth2_scope and client_id):
731731
Log.error('Proxy config file entry incomplete for account', username, '- aborting login')
732732
return (False, '%s: Incomplete config file entry found for account %s - please make sure all required '
733-
'fields are added (permission_url, token_url, oauth2_scope, redirect_uri, client_id '
734-
'and client_secret)' % (APP_NAME, username))
733+
'fields are added (at least token_url, oauth2_scope and client_id)' % (APP_NAME, username))
735734

736735
# while not technically forbidden (RFC 6749, A.1 and A.2), it is highly unlikely the example value is valid
737736
example_client_value = '*** your'
@@ -1136,6 +1135,8 @@ def get_oauth2_authorisation_tokens(token_url, redirect_uri, client_id, client_s
11361135
if oauth2_flow == 'password':
11371136
params['username'] = username
11381137
params['password'] = password
1138+
if not redirect_uri:
1139+
del params['redirect_uri'] # redirect_uri is not typically required in non-code flows; remove if empty
11391140
try:
11401141
response = urllib.request.urlopen(
11411142
urllib.request.Request(token_url, data=urllib.parse.urlencode(params).encode('utf-8'),

0 commit comments

Comments
 (0)