Skip to content

Commit d5bcf14

Browse files
authored
Merge pull request #74 from kpedro88/user2
Manual username parameter
2 parents edb5b1a + 739dbab commit d5bcf14

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

README.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ Gmail OAuth tokens. Run the script as follows:
235235

236236
```shell
237237
$ sasl-xoauth2-tool get-token gmail \
238+
PATH_TO_TOKENS_FILE \
238239
--client-id=CLIENT_ID_FROM_SASL_XOAUTH2_CONF \
239240
--client-secret=CLIENT_SECRET_FROM_SASL_XOAUTH2_CONF \
240-
--scope="https://mail.google.com/" \
241-
PATH_TO_TOKENS_FILE
241+
--scope="https://mail.google.com/"
242242

243243
Please open this URL in a browser ON THIS HOST:
244244

@@ -344,9 +344,9 @@ Microsoft OAuth tokens. Run the script as follows:
344344

345345
```shell
346346
$ sasl-xoauth2-tool get-token outlook \
347+
PATH_TO_TOKENS_FILE \
347348
--client-id=CLIENT_ID_FROM_SASL_XOAUTH2_CONF \
348-
--use-device-flow \
349-
PATH_TO_TOKENS_FILE
349+
--use-device-flow
350350
To sign in, use a web browser to open the page https://www.microsoft.com/link and enter the code REDACTED to authenticate.
351351
```
352352

@@ -429,8 +429,9 @@ Microsoft OAuth tokens. Run the script as follows:
429429

430430
```shell
431431
$ sasl-xoauth2-tool get-token outlook \
432-
--client-id=CLIENT_ID_FROM_SASL_XOAUTH2_CONF \
433-
PATH_TO_TOKENS_FILE
432+
PATH_TO_TOKENS_FILE \
433+
--client-id=CLIENT_ID_FROM_SASL_XOAUTH2_CONF
434+
434435
Please visit the following link in a web browser, then paste the resulting URL:
435436

436437
https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize?client_id=REDACTED&response_type=code&redirect_uri=https%3A//login.microsoftonline.com/common/oauth2/nativeclient&response_mode=query&scope=openid%20offline_access%20https%3A//outlook.office.com/SMTP.Send
@@ -558,11 +559,14 @@ Token refresh succeeded.
558559
$ service postfix restart
559560
```
560561

561-
## Using Multiple Mail Providers Simultaneously
562+
## Using Multiple Mail Providers or Users Simultaneously
562563

563-
One instance of sasl-xoauth2 may provide tokens for different mail providers,
564-
but each provider will require its own client ID, client secret, and token
565-
endpoint. In this case, each of these may be set in the token file rather than
564+
One instance of sasl-xoauth2 may provide tokens for different mail providers
565+
and/or users.
566+
Each provider will require its own client ID, client secret, and token
567+
endpoint. Each user may require a username to be specified, if the username
568+
automatically obtained from postfix is not correct.
569+
In this case, each of these may be set in the token file rather than
566570
in `/etc/sasl-xoauth2.conf`. Set them when setting the initial access token:
567571

568572
```json
@@ -572,7 +576,8 @@ in `/etc/sasl-xoauth2.conf`. Set them when setting the initial access token:
572576
"client_secret": "client secret goes here, if required",
573577
"token_endpoint": "token endpoint goes here, for non-Gmail",
574578
"expiry" : "0",
575-
"refresh_token" : "refresh token goes here"
579+
"refresh_token" : "refresh token goes here",
580+
"user" : "username goes here"
576581
}
577582
```
578583

src/client.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ int Client::InitialStep(sasl_client_params_t *params,
225225
user_ = auth_name;
226226
token_ = TokenStore::Create(log_.get(), password);
227227
if (!token_) return SASL_FAIL;
228+
if (token_->HasUser()) user_ = token_->User();
228229

229230
err = SendToken(to_server, to_server_len);
230231
if (err != SASL_OK) return err;

src/token_store.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ int TokenStore::Read() {
182182
refresh_.clear();
183183
access_.clear();
184184
expiry_ = 0;
185+
user_.clear();
185186

186187
try {
187188
log_->Write("TokenStore::Read: file=%s", path_.c_str());
@@ -211,9 +212,11 @@ int TokenStore::Read() {
211212
if (root.isMember("access_token"))
212213
access_ = root["access_token"].asString();
213214
if (root.isMember("expiry")) expiry_ = stoi(root["expiry"].asString());
215+
if (root.isMember("user"))
216+
user_ = root["user"].asString();
214217

215-
log_->Write("TokenStore::Read: refresh=%s, access=%s", refresh_.c_str(),
216-
access_.c_str());
218+
log_->Write("TokenStore::Read: refresh=%s, access=%s, user=%s", refresh_.c_str(),
219+
access_.c_str(), user_.c_str());
217220
return SASL_OK;
218221

219222
} catch (const std::exception &e) {
@@ -235,6 +238,7 @@ int TokenStore::Write() {
235238
root["refresh_token"] = refresh_;
236239
root["access_token"] = access_;
237240
root["expiry"] = std::to_string(expiry_);
241+
if (HasUser()) root["user"] = user_;
238242

239243
WriteOverride("client_id", override_client_id_, &root);
240244
WriteOverride("client_secret", override_client_secret_, &root);

src/token_store.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class TokenStore {
3333

3434
int GetAccessToken(std::string *token);
3535
int Refresh();
36+
std::string User() const { return user_; }
37+
bool HasUser() const { return !user_.empty(); }
3638

3739
private:
3840
TokenStore(Log *log, const std::string &path, bool enable_updates);
@@ -55,6 +57,8 @@ class TokenStore {
5557
std::string access_;
5658
std::string refresh_;
5759
time_t expiry_ = 0;
60+
std::string user_;
61+
5862

5963
int refresh_attempts_ = 0;
6064
};

0 commit comments

Comments
 (0)