2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2021-11-18 15:52:39 +00:00
|
|
|
import Component from '@glimmer/component';
|
2020-05-11 15:37:11 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2021-11-18 15:52:39 +00:00
|
|
|
|
2020-05-11 15:37:11 +00:00
|
|
|
import { fromPromise } from 'consul-ui/utils/dom/event-source';
|
|
|
|
|
2021-11-18 15:52:39 +00:00
|
|
|
// TODO: We could probably update this to be a template only component now
|
|
|
|
// rather than a JS only one.
|
|
|
|
export default class JWTSource extends Component {
|
|
|
|
@service('repository/oidc-provider') repo;
|
|
|
|
@service('dom') dom;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
2020-05-11 15:37:11 +00:00
|
|
|
if (this.source) {
|
|
|
|
this.source.close();
|
|
|
|
}
|
2021-11-18 15:52:39 +00:00
|
|
|
this._listeners = this.dom.listeners();
|
2020-05-11 15:37:11 +00:00
|
|
|
// TODO: Could this use once? Double check but I don't think it can
|
2021-11-18 15:52:39 +00:00
|
|
|
this.source = fromPromise(this.repo.findCodeByURL(this.args.src));
|
2020-05-11 15:37:11 +00:00
|
|
|
this._listeners.add(this.source, {
|
2022-09-15 08:43:17 +00:00
|
|
|
message: (e) => this.onchange(e),
|
|
|
|
error: (e) => this.onerror(e),
|
2020-05-11 15:37:11 +00:00
|
|
|
});
|
2021-11-18 15:52:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onchange(e) {
|
2022-09-15 08:43:17 +00:00
|
|
|
if (typeof this.args.onchange === 'function') {
|
2021-11-18 15:52:39 +00:00
|
|
|
this.args.onchange(...arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onerror(e) {
|
2022-09-15 08:43:17 +00:00
|
|
|
if (typeof this.args.onerror === 'function') {
|
2021-11-18 15:52:39 +00:00
|
|
|
this.args.onerror(...arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
willDestroy() {
|
|
|
|
super.willDestroy(...arguments);
|
|
|
|
if (this.source) {
|
|
|
|
this.source.close();
|
|
|
|
}
|
|
|
|
this.repo.close();
|
|
|
|
this._listeners.remove();
|
|
|
|
}
|
|
|
|
}
|