2021-02-24 08:43:35 +01:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import CustomWizardApi from "../models/custom-wizard-api";
|
|
|
|
import { default as discourseComputed } from "discourse-common/utils/decorators";
|
2020-04-05 03:37:09 +02:00
|
|
|
import { not, and, equal } from "@ember/object/computed";
|
2021-02-24 08:43:35 +01:00
|
|
|
import { selectKitContent } from "../lib/wizard";
|
2020-04-05 03:37:09 +02:00
|
|
|
import Controller from "@ember/controller";
|
2020-05-28 05:06:06 +02:00
|
|
|
import I18n from "I18n";
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2020-04-05 03:37:09 +02:00
|
|
|
export default Controller.extend({
|
2021-02-24 08:43:35 +01:00
|
|
|
queryParams: ["refresh_list"],
|
2019-05-30 07:04:34 +02:00
|
|
|
loadingSubscriptions: false,
|
2021-02-24 08:43:35 +01:00
|
|
|
notAuthorized: not("api.authorized"),
|
|
|
|
endpointMethods: selectKitContent(["GET", "PUT", "POST", "PATCH", "DELETE"]),
|
|
|
|
showRemove: not("isNew"),
|
|
|
|
showRedirectUri: and("threeLeggedOauth", "api.name"),
|
2019-06-03 04:49:54 +02:00
|
|
|
responseIcon: null,
|
2021-02-24 08:43:35 +01:00
|
|
|
contentTypes: selectKitContent([
|
|
|
|
"application/json",
|
|
|
|
"application/x-www-form-urlencoded",
|
|
|
|
]),
|
|
|
|
successCodes: selectKitContent([
|
|
|
|
100,
|
|
|
|
101,
|
|
|
|
102,
|
|
|
|
200,
|
|
|
|
201,
|
|
|
|
202,
|
|
|
|
203,
|
|
|
|
204,
|
|
|
|
205,
|
|
|
|
206,
|
|
|
|
207,
|
|
|
|
208,
|
|
|
|
226,
|
|
|
|
300,
|
|
|
|
301,
|
|
|
|
302,
|
|
|
|
303,
|
|
|
|
303,
|
|
|
|
304,
|
|
|
|
305,
|
|
|
|
306,
|
|
|
|
307,
|
|
|
|
308,
|
|
|
|
]),
|
|
|
|
|
|
|
|
@discourseComputed(
|
|
|
|
"saveDisabled",
|
|
|
|
"api.authType",
|
|
|
|
"api.authUrl",
|
|
|
|
"api.tokenUrl",
|
|
|
|
"api.clientId",
|
|
|
|
"api.clientSecret",
|
|
|
|
"threeLeggedOauth"
|
|
|
|
)
|
|
|
|
authDisabled(
|
|
|
|
saveDisabled,
|
|
|
|
authType,
|
|
|
|
authUrl,
|
|
|
|
tokenUrl,
|
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
threeLeggedOauth
|
|
|
|
) {
|
|
|
|
if (saveDisabled || !authType || !tokenUrl || !clientId || !clientSecret)
|
|
|
|
return true;
|
2019-06-07 05:09:31 +02:00
|
|
|
if (threeLeggedOauth) return !authUrl;
|
|
|
|
return false;
|
2019-06-02 12:54:31 +02:00
|
|
|
},
|
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
@discourseComputed("api.name", "api.authType")
|
2019-06-03 01:40:54 +02:00
|
|
|
saveDisabled(name, authType) {
|
|
|
|
return !name || !authType;
|
|
|
|
},
|
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
authorizationTypes: selectKitContent(["none", "basic", "oauth_2", "oauth_3"]),
|
|
|
|
isBasicAuth: equal("api.authType", "basic"),
|
2019-06-07 05:09:31 +02:00
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
@discourseComputed("api.authType")
|
2019-06-07 05:09:31 +02:00
|
|
|
isOauth(authType) {
|
2021-02-24 08:43:35 +01:00
|
|
|
return authType && authType.indexOf("oauth") > -1;
|
2019-06-07 05:09:31 +02:00
|
|
|
},
|
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
twoLeggedOauth: equal("api.authType", "oauth_2"),
|
|
|
|
threeLeggedOauth: equal("api.authType", "oauth_3"),
|
2019-06-07 05:09:31 +02:00
|
|
|
|
2019-05-30 07:04:34 +02:00
|
|
|
actions: {
|
|
|
|
addParam() {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.get("api.authParams").pushObject({});
|
2019-05-30 07:04:34 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
removeParam(param) {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.get("api.authParams").removeObject(param);
|
2019-05-30 07:04:34 +02:00
|
|
|
},
|
|
|
|
|
2019-05-31 09:54:11 +02:00
|
|
|
addEndpoint() {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.get("api.endpoints").pushObject({});
|
2019-05-31 09:54:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
removeEndpoint(endpoint) {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.get("api.endpoints").removeObject(endpoint);
|
2019-05-31 09:54:11 +02:00
|
|
|
},
|
|
|
|
|
2019-05-30 07:04:34 +02:00
|
|
|
authorize() {
|
2021-02-24 08:43:35 +01:00
|
|
|
const api = this.get("api");
|
2019-06-07 05:09:31 +02:00
|
|
|
const { name, authType, authUrl, authParams } = api;
|
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("authErrorMessage", "");
|
2019-06-07 05:09:31 +02:00
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
if (authType === "oauth_2") {
|
|
|
|
this.set("authorizing", true);
|
|
|
|
ajax(`/admin/wizards/apis/${name.underscore()}/authorize`)
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.then((result) => {
|
2019-06-07 05:09:31 +02:00
|
|
|
if (result.success) {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("api", CustomWizardApi.create(result.api));
|
2019-06-07 05:09:31 +02:00
|
|
|
} else if (result.failed && result.message) {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("authErrorMessage", result.message);
|
2019-06-07 05:09:31 +02:00
|
|
|
} else {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("authErrorMessage", "Authorization Failed");
|
2019-06-07 05:09:31 +02:00
|
|
|
}
|
|
|
|
setTimeout(() => {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("authErrorMessage", "");
|
2019-06-07 05:09:31 +02:00
|
|
|
}, 6000);
|
2021-02-24 08:43:35 +01:00
|
|
|
})
|
|
|
|
.finally(() => this.set("authorizing", false));
|
|
|
|
} else if (authType === "oauth_3") {
|
|
|
|
let query = "?";
|
2019-06-07 05:09:31 +02:00
|
|
|
|
|
|
|
query += `client_id=${api.clientId}`;
|
|
|
|
query += `&redirect_uri=${encodeURIComponent(api.redirectUri)}`;
|
|
|
|
query += `&response_type=code`;
|
|
|
|
|
|
|
|
if (authParams) {
|
2021-02-24 08:43:35 +01:00
|
|
|
authParams.forEach((p) => {
|
2019-06-07 05:09:31 +02:00
|
|
|
query += `&${p.key}=${encodeURIComponent(p.value)}`;
|
|
|
|
});
|
|
|
|
}
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-06-07 05:09:31 +02:00
|
|
|
window.location.href = authUrl + query;
|
2019-05-30 07:04:34 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
save() {
|
2021-02-24 08:43:35 +01:00
|
|
|
const api = this.get("api");
|
2019-06-02 12:54:31 +02:00
|
|
|
const name = api.name;
|
2019-06-03 01:40:54 +02:00
|
|
|
const authType = api.authType;
|
2019-06-02 12:54:31 +02:00
|
|
|
let refreshList = false;
|
2019-06-03 04:49:54 +02:00
|
|
|
let error;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-06-03 01:40:54 +02:00
|
|
|
if (!name || !authType) return;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-06-02 12:54:31 +02:00
|
|
|
let data = {
|
2021-02-24 08:43:35 +01:00
|
|
|
auth_type: authType,
|
2019-06-02 12:54:31 +02:00
|
|
|
};
|
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
if (api.title) data["title"] = api.title;
|
2019-06-03 01:40:54 +02:00
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
const originalTitle = this.get("api.originalTitle");
|
|
|
|
if (api.get("isNew") || (originalTitle && api.title !== originalTitle)) {
|
2019-06-02 12:54:31 +02:00
|
|
|
refreshList = true;
|
|
|
|
}
|
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
if (api.get("isNew")) {
|
|
|
|
data["new"] = true;
|
|
|
|
}
|
2019-06-02 12:54:31 +02:00
|
|
|
|
2019-06-03 04:49:54 +02:00
|
|
|
let requiredParams;
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
if (authType === "basic") {
|
|
|
|
requiredParams = ["username", "password"];
|
|
|
|
} else if (authType === "oauth_2") {
|
|
|
|
requiredParams = ["tokenUrl", "clientId", "clientSecret"];
|
|
|
|
} else if (authType === "oauth_3") {
|
|
|
|
requiredParams = ["authUrl", "tokenUrl", "clientId", "clientSecret"];
|
2019-06-03 04:49:54 +02:00
|
|
|
}
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-08-13 06:11:46 +02:00
|
|
|
if (requiredParams) {
|
|
|
|
for (let rp of requiredParams) {
|
|
|
|
if (!api[rp]) {
|
2021-02-24 08:43:35 +01:00
|
|
|
let key = rp.replace("auth", "");
|
|
|
|
error = `${I18n.t(
|
|
|
|
`admin.wizard.api.auth.${key.underscore()}`
|
|
|
|
)} is required for ${authType}`;
|
2019-08-13 06:11:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
data[rp.underscore()] = api[rp];
|
2019-05-30 07:04:34 +02:00
|
|
|
}
|
2019-06-03 04:49:54 +02:00
|
|
|
}
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2019-06-03 04:49:54 +02:00
|
|
|
const params = api.authParams;
|
|
|
|
if (params.length) {
|
2021-02-24 08:43:35 +01:00
|
|
|
data["auth_params"] = JSON.stringify(params);
|
2019-05-30 07:04:34 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 23:32:24 +02:00
|
|
|
const endpoints = api.endpoints;
|
2019-06-02 12:54:31 +02:00
|
|
|
if (endpoints.length) {
|
2019-06-03 04:49:54 +02:00
|
|
|
for (let e of endpoints) {
|
|
|
|
if (!e.name) {
|
2021-02-24 08:43:35 +01:00
|
|
|
error = "Every endpoint must have a name";
|
2019-06-03 04:49:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
data["endpoints"] = JSON.stringify(endpoints);
|
2019-05-31 09:54:11 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 04:49:54 +02:00
|
|
|
if (error) {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("error", error);
|
2019-06-03 04:49:54 +02:00
|
|
|
setTimeout(() => {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("error", "");
|
2019-06-03 04:49:54 +02:00
|
|
|
}, 6000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("updating", true);
|
2019-05-30 07:04:34 +02:00
|
|
|
|
2020-04-22 15:03:18 +02:00
|
|
|
ajax(`/admin/wizards/api/${name.underscore()}`, {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "PUT",
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.then((result) => {
|
2019-05-30 07:04:34 +02:00
|
|
|
if (result.success) {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.send("afterSave", result.api.name);
|
2019-06-03 04:49:54 +02:00
|
|
|
} else {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("responseIcon", "times");
|
2019-06-02 12:54:31 +02:00
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
})
|
|
|
|
.finally(() => this.set("updating", false));
|
2019-06-02 12:54:31 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
remove() {
|
2021-02-24 08:43:35 +01:00
|
|
|
const name = this.get("api.name");
|
2019-06-02 12:54:31 +02:00
|
|
|
if (!name) return;
|
|
|
|
|
2021-02-24 08:43:35 +01:00
|
|
|
this.set("updating", true);
|
2019-06-02 12:54:31 +02:00
|
|
|
|
2020-04-22 15:03:18 +02:00
|
|
|
ajax(`/admin/wizards/api/${name.underscore()}`, {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "DELETE",
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.then((result) => {
|
2019-06-02 12:54:31 +02:00
|
|
|
if (result.success) {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.send("afterDestroy");
|
2019-05-30 07:04:34 +02:00
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
})
|
|
|
|
.finally(() => this.set("updating", false));
|
2019-06-06 18:10:13 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
clearLogs() {
|
2021-02-24 08:43:35 +01:00
|
|
|
const name = this.get("api.name");
|
2019-06-16 15:33:30 +02:00
|
|
|
if (!name) return;
|
|
|
|
|
2020-04-22 15:03:18 +02:00
|
|
|
ajax(`/admin/wizards/api/${name.underscore()}/logs`, {
|
2021-02-24 08:43:35 +01:00
|
|
|
type: "DELETE",
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.then((result) => {
|
2019-06-06 18:10:13 +02:00
|
|
|
if (result.success) {
|
2021-02-24 08:43:35 +01:00
|
|
|
this.transitionToRoute("adminWizardsApis").then(() => {
|
|
|
|
this.send("refreshModel");
|
2019-06-06 18:10:13 +02:00
|
|
|
});
|
|
|
|
}
|
2021-02-24 08:43:35 +01:00
|
|
|
})
|
|
|
|
.finally(() => this.set("updating", false));
|
|
|
|
},
|
|
|
|
},
|
2019-05-30 07:04:34 +02:00
|
|
|
});
|