Skip to content

Commit 7184994

Browse files
committed
feat: add an 'adjustment' to allow username to be parsed from email, if no username exists otherwise
1 parent 4a309f8 commit 7184994

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

library.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ OAuth.listStrategies = async (full) => {
4747
const names = await db.getSortedSetMembers('oauth2-multiple:strategies');
4848
names.sort();
4949

50+
return await getStrategies(names, full);
51+
};
52+
53+
OAuth.getStrategy = async (name) => {
54+
const strategies = await getStrategies([name], true);
55+
return strategies.length ? strategies[0] : null;
56+
};
57+
58+
async function getStrategies(names, full) {
5059
const strategies = await db.getObjects(names.map(name => `oauth2-multiple:strategies:${name}`), full ? undefined : ['enabled']);
5160
strategies.forEach((strategy, idx) => {
5261
strategy.name = names[idx];
@@ -55,7 +64,7 @@ OAuth.listStrategies = async (full) => {
5564
});
5665

5766
return strategies;
58-
};
67+
}
5968

6069
OAuth.loadStrategies = async (strategies) => {
6170
const passportOAuth = require('passport-oauth').OAuth2Strategy;
@@ -127,14 +136,14 @@ OAuth.getUserProfile = function (name, userRoute, accessToken, done) {
127136
// instead of the request headers, comment out the next line:
128137
this._oauth2._useAuthorizationHeaderForGET = true;
129138

130-
this._oauth2.get(userRoute, accessToken, (err, body/* , res */) => {
139+
this._oauth2.get(userRoute, accessToken, async (err, body/* , res */) => {
131140
if (err) {
132141
return done(err);
133142
}
134143

135144
try {
136145
const json = JSON.parse(body);
137-
const profile = OAuth.parseUserReturn(json);
146+
const profile = await OAuth.parseUserReturn(name, json);
138147
profile.provider = name;
139148
done(null, profile);
140149
} catch (e) {
@@ -143,18 +152,25 @@ OAuth.getUserProfile = function (name, userRoute, accessToken, done) {
143152
});
144153
};
145154

146-
OAuth.parseUserReturn = ({
147-
provider, id, sub, name, nickname, preferred_username, picture, email, /* , email_verified */
148-
}) => {
149-
const profile = {
155+
OAuth.parseUserReturn = async (provider, profile) => {
156+
const {
157+
id, sub, name, nickname, preferred_username, picture,
158+
email, /* , email_verified */
159+
} = profile;
160+
const { usernameViaEmail } = await OAuth.getStrategy(provider);
161+
const normalized = {
150162
provider,
151163
id: id || sub,
152164
displayName: nickname || preferred_username || name,
153165
picture,
154166
email,
155167
};
156168

157-
return profile;
169+
if (!normalized.displayName && email && usernameViaEmail === 'on') {
170+
normalized.displayName = email.split('@')[0];
171+
}
172+
173+
return normalized;
158174
};
159175

160176
OAuth.login = async (payload) => {

static/templates/partials/edit-oauth2-strategy.tpl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,18 @@
9191
</fieldset>
9292
</div>
9393
</div>
94+
<div class="row border-top mt-3 pt-3">
95+
<div class="col-12">
96+
<h6>Adjustments</h6>
97+
<p class="form-text">
98+
Occasionally, some providers may have slightly different implementations that require adjustments on this end.
99+
We offer some common adjustments below.
100+
</p>
101+
102+
<div class="form-check form-switch">
103+
<input type="checkbox" class="form-check-input" id="usernameViaEmail" name="usernameViaEmail" {{{ if (./usernameViaEmail == "on") }}}checked{{{ end }}}>
104+
<label for="usernameViaEmail" class="form-check-label">Fall back to email as username if no username available (e.g. <code><strong>username</strong>@example.org</code>)</label>
105+
</div>
106+
</div>
107+
</div>
94108
</form>

0 commit comments

Comments
 (0)