Skip to content

Commit 48b1c72

Browse files
committed
- Allow setting API association to Auth Method from Auth Server page.
1 parent 684f8f5 commit 48b1c72

File tree

3 files changed

+101
-13
lines changed

3 files changed

+101
-13
lines changed

public/js/authserver.js

+40-9
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ Vue.component('auth-method', {
8181
this.newAdfsGroup = '';
8282
this.newWickedGroup = '';
8383
this.$set(this.value.config.defaultGroups, adfsGroup, wickedGroup);
84+
},
85+
selectAllApis: function () {
86+
for (let apiId in this.value.apis) {
87+
this.$set(this.value.apis, apiId, true);
88+
}
89+
},
90+
deselectAllApis: function () {
91+
for (let apiId in this.value.apis) {
92+
this.$set(this.value.apis, apiId, false);
93+
}
8494
}
8595
},
8696
template:
@@ -197,6 +207,16 @@ Vue.component('auth-method', {
197207
<div v-else>
198208
<p><i>Unknown auth method type. To change this, please edit the JSON file directly.</i></p>
199209
</div>
210+
211+
<br>
212+
213+
<wicked-panel :open=false
214+
type="success"
215+
title="Supported APIs">
216+
<button v-on:click="selectAllApis" class="btn btn-sm btn-primary">Select all</button>
217+
<button v-on:click="deselectAllApis" class="btn btn-sm btn-default">Deselect all</button>
218+
<wicked-checkbox v-for="(v, apiId) in value.apis" v-model="value.apis[apiId]" :label="apiId" />
219+
</wicked-panel>
200220
</wicked-panel>
201221
`
202222
});
@@ -223,7 +243,8 @@ Vue.component('add-auth-method', {
223243
name: this.authMethodId,
224244
friendlyShort: 'Short friendly name',
225245
friendlyLong: 'Long friendly name',
226-
config: createDefaultConfig(this.selectedType, this.authMethodId)
246+
config: createDefaultConfig(this.selectedType, this.authMethodId),
247+
apis: JSON.parse(JSON.stringify(injectedData.oauthApis))
227248
});
228249
}
229250
},
@@ -265,30 +286,35 @@ Vue.component('password-validation', {
265286
});
266287

267288
function createDefaultConfig(authMethodType, authMethodId) {
289+
let defaultConfig;
268290
switch (authMethodType) {
269291
case 'local':
270-
return {
292+
defaultConfig = {
271293
trustUsers: false,
272294
disableSignup: false
273295
};
296+
break;
274297
case 'external':
275-
return {
298+
defaultConfig = {
276299
validateUserPassUrl: 'http://your-service.default.cluster.local:2000/login',
277300
allowRefreshUrl: 'http://your-service.default.cluster.local:2000/refresh'
278301
};
302+
break;
279303
case 'github':
280304
case 'google':
281-
return {
305+
defaultConfig = {
282306
clientId: 'your-client-id',
283307
clientSecret: 'your-client-secret'
284308
};
309+
break;
285310
case 'twitter':
286-
return {
311+
defaultConfig = {
287312
consumerKey: 'twitter-consumer-key',
288313
consumerSecret: 'twitter-consumer-secret'
289314
};
315+
break;
290316
case 'oauth2':
291-
return {
317+
defaultConfig = {
292318
clientId: 'your-client-id',
293319
clientSecret: 'your-client-secret',
294320
endpoints: {
@@ -303,8 +329,9 @@ function createDefaultConfig(authMethodType, authMethodId) {
303329
lastNameField: 'family_name',
304330
emailField: 'email'
305331
};
332+
break;
306333
case 'adfs':
307-
return {
334+
defaultConfig = {
308335
clientId: 'your-client-id',
309336
clientSecret: 'your-client-secret',
310337
endpoints: {
@@ -322,9 +349,10 @@ function createDefaultConfig(authMethodType, authMethodId) {
322349
"DOMAIN\\_Some_Group": "dev"
323350
}
324351
};
352+
break;
325353
case 'saml': {
326354
const envVarPrefix = '$PORTAL_AUTH_SAML_' + authMethodId.toUpperCase().replace(/-/g, '_') + '_';
327-
return {
355+
defaultConfig = {
328356
trustUsers: true,
329357
profile: JSON.stringify({
330358
"sub": "{{{your_id}}}",
@@ -350,10 +378,13 @@ function createDefaultConfig(authMethodType, authMethodId) {
350378
"allow_unencrypted_assertion": true
351379
}, null, 2)
352380
};
381+
break;
353382
}
354383
default:
355-
return {};
384+
defaultConfig = {};
385+
break;
356386
}
387+
return defaultConfig;
357388
}
358389

359390
function displayCallbackUris(uri, authMethodId) {

routes/authservers.js

+59-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ router.get('/', function (req, res, next) {
1818
// If we only have one, redirect there directly
1919
return res.redirect(`/authservers/${authServerNames[0]}`);
2020
}
21-
21+
2222
res.render('authservers',
2323
{
2424
configPath: req.app.get('config_path'),
@@ -84,7 +84,8 @@ router.get('/:serverId', function (req, res, next) {
8484
const authId = `${serverId}-auth`;
8585
authServer.id = authId;
8686
authServer.config.api.name = authId;
87-
87+
88+
8889
let origPlugins = [];
8990
if (authServer.config && authServer.config.plugins)
9091
origPlugins = authServer.config.plugins;
@@ -117,6 +118,30 @@ router.get('/:serverId', function (req, res, next) {
117118
}
118119
}
119120

121+
// Mix in api auth methods as well
122+
const apis = utils.loadApis(req.app);
123+
// console.log(JSON.stringify(apis, null, 2));
124+
for (let authMethod of authServer.authMethods) {
125+
authMethod.apis = {};
126+
for (let api of apis.apis) {
127+
if (api.auth !== 'oauth2')
128+
continue;
129+
const authMethodName = `${serverId}:${authMethod.name}`;
130+
if (api.authMethods.indexOf(authMethodName) >= 0)
131+
authMethod.apis[api.id] = true;
132+
else
133+
authMethod.apis[api.id] = false;
134+
}
135+
}
136+
137+
// For new auth methods
138+
const oauthApis = {};
139+
for (let api of apis.apis) {
140+
if (api.auth !== 'oauth2')
141+
continue;
142+
oauthApis[api.id] = false;
143+
}
144+
120145
const groups = utils.loadGroups(req.app);
121146
const passwordStrategies = passwordValidator.getStrategies();
122147

@@ -128,7 +153,8 @@ router.get('/:serverId', function (req, res, next) {
128153
authServer: authServer,
129154
plugins: plugins,
130155
groups: groups,
131-
passwordStrategies: passwordStrategies
156+
passwordStrategies: passwordStrategies,
157+
oauthApis: oauthApis
132158
};
133159

134160
res.render('authserver', viewModel);
@@ -197,11 +223,41 @@ router.post('/:serverId/api', function (req, res, next) {
197223
if (thisAm.hasOwnProperty('useForPortal'))
198224
delete thisAm.useForPortal;
199225
}
226+
227+
// Now do the same for the APIs; first strip all method IDs from this Auth Server
228+
const apis = utils.loadApis(req.app);
229+
for (let api of apis.apis) {
230+
if (api.auth !== 'oauth2')
231+
continue;
232+
const strippedList = [];
233+
const apiAuthMethods = api.authMethods ? api.authMethods : [];
234+
for (let authMethodId of apiAuthMethods) {
235+
if (!authMethodId.startsWith(`${serverId}:`))
236+
strippedList.push(authMethodId);
237+
}
238+
api.authMethods = strippedList;
239+
}
240+
// And then add them back from the auth server JSON (and delete the "apis" object)
241+
for (let am of authMethods) {
242+
for (let apiId in am.apis) {
243+
if (am.apis[apiId]) {
244+
const thisApi = apis.apis.find(a => a.id === apiId);
245+
if (!thisApi) {
246+
warn(`Could not add auth method ${am.name} to API ${apiId}, API not found.`);
247+
}
248+
thisApi.authMethods.push(`${serverId}:${am.name}`);
249+
}
250+
}
251+
// Delete the apis object
252+
delete am.apis;
253+
}
254+
200255
debug(glob);
201256
debug(JSON.stringify(authServer, null, 2));
202257

203258
utils.saveGlobals(req.app, glob);
204259
utils.saveAuthServer(req.app, serverId, authServer);
260+
utils.saveApis(req.app, apis);
205261

206262
res.status(204).json({ message: 'OK' });
207263
});

views/authserver.jade

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ block afterScripts
3434
plugins: !{JSON.stringify(plugins)},
3535
groups: !{JSON.stringify(groups)},
3636
glob: !{JSON.stringify(glob)},
37-
passwordStrategies: !{JSON.stringify(passwordStrategies)}
37+
passwordStrategies: !{JSON.stringify(passwordStrategies)},
38+
oauthApis: !{JSON.stringify(oauthApis)}
3839
};
3940

4041
script(src='/js/authserver.js')

0 commit comments

Comments
 (0)