Skip to content

Commit 05297d3

Browse files
committed
fix(autodiscovery): await fetching config in beforeModel and afterModel
1 parent 445cb53 commit 05297d3

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ module.exports = function (environment) {
189189

190190
Further there is the possibilty to user the `.well-known` endpoint of your authentication backend (specified [in the OpenID provider configuration chapter](https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig). For this to work you must **at least provide a valid `host` configuration value.**
191191

192+
To enforce the autodiscovery, but also providing some keys (autodiscovery will overwrite duplicate keys), set `forceAutodiscovery: true`.
193+
192194
### Options
193195

194196
Here is a complete list of all possible config options:

addon/routes/oidc-authentication.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ export default class OIDCAuthenticationRoute extends Route {
6767
* @param {String} transition.to.queryParams.state The state given by the identity provider
6868
*/
6969
async afterModel(_, transition) {
70-
if (!this.config.authEndpoint) {
70+
await this.config.loadConfig();
71+
72+
if (!this.config.hasEndpointsConfigured) {
7173
throw new Error(
7274
"Please define all OIDC endpoints (auth, token, logout, userinfo)",
7375
);

addon/services/config.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const defaultConfig = {
2525
};
2626

2727
const aliases = {
28-
authEndpoint: "authorizationEndpoint",
28+
// "originalKey": "aliasKey"
29+
authorizationEndpoint: "authEndpoint",
2930
};
3031

3132
export const applyAliases = (config) => {
@@ -50,12 +51,8 @@ export default class ConfigurationService extends Service {
5051
constructor(...args) {
5152
super(...args);
5253

53-
const envConfig =
54-
getOwner(this).resolveRegistration("config:environment")[
55-
"ember-simple-auth-oidc"
56-
];
57-
if (envConfig) {
58-
this.resolvedConfig = applyAliases(camelizeObjectKeys(envConfig));
54+
if (this.environment) {
55+
this.resolvedConfig = applyAliases(camelizeObjectKeys(this.environment));
5956
}
6057

6158
this.configKeys.forEach((key) => {
@@ -67,6 +64,12 @@ export default class ConfigurationService extends Service {
6764
});
6865
}
6966

67+
get environment() {
68+
return getOwner(this).resolveRegistration("config:environment")[
69+
"ember-simple-auth-oidc"
70+
];
71+
}
72+
7073
@cached
7174
get configuration() {
7275
return { ...defaultConfig, ...this.resolvedConfig, ...this.fetchedConfig };
@@ -85,8 +88,12 @@ export default class ConfigurationService extends Service {
8588
}
8689

8790
async loadConfig() {
88-
if (!this.hasEndpointsConfigured) {
89-
await this._fetchAuthConfiguration.perform();
91+
if (this.environment.forceAutodiscovery || !this.hasEndpointsConfigured) {
92+
if (this._fetchAuthConfiguration.isRunning) {
93+
await this._fetchAuthConfiguration.last;
94+
} else {
95+
await this._fetchAuthConfiguration.perform();
96+
}
9097
}
9198
}
9299

@@ -95,7 +102,7 @@ export default class ConfigurationService extends Service {
95102
* SPEC: https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig
96103
*/
97104
@lastValue("_fetchAuthConfiguration") fetchedConfig;
98-
_fetchAuthConfiguration = task(async () => {
105+
_fetchAuthConfiguration = task({ drop: true }, async () => {
99106
if (this._fetchAuthConfiguration.lastSuccessful) {
100107
return this.fetchedConfig;
101108
}

tests/dummy/config/environment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = function (environment) {
2020
loginHintName: "custom_login_hint",
2121
expiresIn: 60000, // Short expire time (60s) for testing purpose
2222
refreshLeeway: 1000,
23+
forceAutodiscovery: false,
2324
},
2425

2526
EmberENV: {

tests/unit/services/config-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ module("Unit | Service | config", function (hooks) {
3030

3131
test("alias configuration keys", function (assert) {
3232
const aliased = applyAliases({
33-
authEndpoint: "auth",
33+
authorizationEndpoint: "auth",
3434
});
3535

36-
assert.strictEqual(aliased.authorizationEndpoint, "auth");
36+
assert.strictEqual(aliased.authEndpoint, "auth");
3737
});
3838

3939
test("fetch configuration if necessary keys are not given", async function (assert) {

0 commit comments

Comments
 (0)