Skip to content

Commit 739dbab

Browse files
committed
specify user for a given token
1 parent 21520e7 commit 739dbab

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -559,11 +559,14 @@ Token refresh succeeded.
559559
$ service postfix restart
560560
```
561561

562-
## Using Multiple Mail Providers Simultaneously
563-
564-
One instance of sasl-xoauth2 may provide tokens for different mail providers,
565-
but each provider will require its own client ID, client secret, and token
566-
endpoint. In this case, each of these may be set in the token file rather than
562+
## Using Multiple Mail Providers or Users Simultaneously
563+
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
567570
in `/etc/sasl-xoauth2.conf`. Set them when setting the initial access token:
568571

569572
```json
@@ -573,7 +576,8 @@ in `/etc/sasl-xoauth2.conf`. Set them when setting the initial access token:
573576
"client_secret": "client secret goes here, if required",
574577
"token_endpoint": "token endpoint goes here, for non-Gmail",
575578
"expiry" : "0",
576-
"refresh_token" : "refresh token goes here"
579+
"refresh_token" : "refresh token goes here",
580+
"user" : "username goes here"
577581
}
578582
```
579583

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)