Skip to content

Commit

Permalink
Merge branch 'refactor-api-keys-service' of github.com:giancarloromeo…
Browse files Browse the repository at this point in the history
…/osparc-simcore into refactor-api-keys-service
  • Loading branch information
giancarloromeo committed Dec 6, 2024
2 parents ff78470 + c10aec8 commit eceb799
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ qx.Class.define("osparc.data.Resources", {
},
delete: {
method: "DELETE",
url: statics.API + "/auth/api-keys"
url: statics.API + "/auth/api-keys/{apiKeyId}"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
const formData = e.getData();
const params = {
data: {
"display_name": formData["name"]
"displayName": formData["name"]
}
};
if (formData["expiration"]) {
Expand All @@ -90,14 +90,17 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
.then(data => {
this.__rebuildAPIKeysList();

const key = data["api_key"];
const secret = data["api_secret"];
const showAPIKeyWindow = new osparc.desktop.preferences.window.ShowAPIKey(key, secret);
const key = data["apiKey"];
const secret = data["apiSecret"];
const baseUrl = data["apiBaseUrl"];
const showAPIKeyWindow = new osparc.desktop.preferences.window.ShowAPIKey(key, secret, baseUrl);
showAPIKeyWindow.center();
showAPIKeyWindow.open();
})
.catch(err => {
osparc.FlashMessenger.getInstance().logAs(err.message, "ERROR");
const errorMsg = err.message || this.tr("Cannot create API Key");
osparc.FlashMessenger.getInstance().logAs(errorMsg, "ERROR");
console.error(err);
})
.finally(() => this.__requestAPIKeyBtn.setFetching(false));
}, this);
Expand All @@ -109,26 +112,24 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
osparc.data.Resources.get("apiKeys")
.then(apiKeys => {
apiKeys.forEach(apiKey => {
const apiKeyForm = this.__createValidAPIKeyForm(apiKey);
const apiKeyForm = this.__createAPIKeyEntry(apiKey);
this.__apiKeysList.add(apiKeyForm);
});
})
.catch(err => console.error(err));
},

__createValidAPIKeyForm: function(apiKeyLabel) {
__createAPIKeyEntry: function(apiKey) {
const grid = this.__createValidEntryLayout();

const nameLabel = new qx.ui.basic.Label(apiKeyLabel);
const nameLabel = new qx.ui.basic.Label(apiKey["displayName"]);
grid.add(nameLabel, {
row: 0,
column: 0
});

const delAPIKeyBtn = new qx.ui.form.Button(null, "@FontAwesome5Solid/trash/14");
delAPIKeyBtn.addListener("execute", e => {
this.__deleteAPIKey(apiKeyLabel);
}, this);
delAPIKeyBtn.addListener("execute", () => this.__deleteAPIKey(apiKey["id"]), this);
grid.add(delAPIKeyBtn, {
row: 0,
column: 1
Expand All @@ -137,7 +138,7 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
return grid;
},

__deleteAPIKey: function(apiKeyLabel) {
__deleteAPIKey: function(apiKeyId) {
if (!osparc.data.Permissions.getInstance().canDo("user.apikey.delete", true)) {
return;
}
Expand All @@ -153,13 +154,17 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
win.addListener("close", () => {
if (win.getConfirmed()) {
const params = {
data: {
"display_name": apiKeyLabel
url: {
"apiKeyId": apiKeyId
}
};
osparc.data.Resources.fetch("apiKeys", "delete", params)
.then(() => this.__rebuildAPIKeysList())
.catch(err => console.error(err));
.catch(err => {
const errorMsg = err.message || this.tr("Cannot delete API Key");
osparc.FlashMessenger.getInstance().logAs(errorMsg, "ERROR");
console.error(err)
});
}
}, this);
},
Expand Down Expand Up @@ -203,7 +208,7 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
const supportedExternalServices = osparc.utils.Utils.deepCloneObject(this.__supportedExternalServices());

tokensList.forEach(token => {
const tokenForm = this.__createValidTokenEntry(token);
const tokenForm = this.__createTokenEntry(token);
this.__validTokensGB.add(tokenForm);
const idx = supportedExternalServices.findIndex(srv => srv.name === token.service);
if (idx > -1) {
Expand Down Expand Up @@ -244,7 +249,7 @@ qx.Class.define("osparc.desktop.preferences.pages.TokensPage", {
.catch(err => console.error(err));
},

__createValidTokenEntry: function(token) {
__createTokenEntry: function(token) {
const grid = this.__createValidEntryLayout();

const service = token["service"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
qx.Class.define("osparc.desktop.preferences.window.ShowAPIKey", {
extend: osparc.desktop.preferences.window.APIKeyBase,

construct: function(key, secret) {
construct: function(key, secret, baseUrl) {
const caption = this.tr("API Key");
const infoText = this.tr("For your protection, store your access keys securely and do not share them. You will not be able to access the key again once this window is closed.");
this.base(arguments, caption, infoText);
Expand All @@ -25,39 +25,60 @@ qx.Class.define("osparc.desktop.preferences.window.ShowAPIKey", {
clickAwayClose: false
});

this.__populateTokens(key, secret);
this.__populateTokens(key, secret, baseUrl);
},

members: {
__populateTokens: function(key, secret) {
const hBox1 = this.__createEntry(this.tr("<b>Key:</b>"), key);
__populateTokens: function(key, secret, baseUrl) {
const hBox1 = this.__createStarredEntry(this.tr("<b>Key:</b>"), key);
this._add(hBox1);

const hBox2 = this.__createEntry(this.tr("<b>Secret:</b>"), secret);
const hBox2 = this.__createStarredEntry(this.tr("<b>Secret:</b>"), secret);
this._add(hBox2);

const hBox3 = new qx.ui.container.Composite(new qx.ui.layout.HBox(10)).set({
const hBox3 = this.__createEntry(this.tr("<b>Base url:</b>"), baseUrl);
this._add(hBox3);

const buttonsLayout = new qx.ui.container.Composite(new qx.ui.layout.HBox(10)).set({
appearance: "margined-layout"
});
const copyAPIKeyBtn = new qx.ui.form.Button(this.tr("Copy API Key"));
const copyAPIKeyBtn = new qx.ui.form.Button(this.tr("API Key"), "@FontAwesome5Solid/copy/12");
copyAPIKeyBtn.addListener("execute", e => {
if (osparc.utils.Utils.copyTextToClipboard(key)) {
copyAPIKeyBtn.setIcon("@FontAwesome5Solid/check/12");
}
});
hBox3.add(copyAPIKeyBtn, {
width: "50%"
buttonsLayout.add(copyAPIKeyBtn, {
flex: 1
});
const copyAPISecretBtn = new qx.ui.form.Button(this.tr("Copy API Secret"));
const copyAPISecretBtn = new qx.ui.form.Button(this.tr("API Secret"), "@FontAwesome5Solid/copy/12");
copyAPISecretBtn.addListener("execute", e => {
if (osparc.utils.Utils.copyTextToClipboard(secret)) {
copyAPISecretBtn.setIcon("@FontAwesome5Solid/check/12");
}
});
hBox3.add(copyAPISecretBtn, {
width: "50%"
buttonsLayout.add(copyAPISecretBtn, {
flex: 1
});
this._add(hBox3);
const copyBaseUrlBtn = new qx.ui.form.Button(this.tr("Base URL"), "@FontAwesome5Solid/copy/12");
copyBaseUrlBtn.addListener("execute", e => {
if (osparc.utils.Utils.copyTextToClipboard(baseUrl)) {
copyBaseUrlBtn.setIcon("@FontAwesome5Solid/check/12");
}
});
buttonsLayout.add(copyBaseUrlBtn, {
flex: 1
});
this._add(buttonsLayout);
},

__createStarredEntry: function(title, label) {
const hBox = this.__createEntry(title);
if (label) {
// partially hide the key and secret
hBox.getChildren()[1].setValue(label.substring(1, 8) + "****")
}
return hBox;
},

__createEntry: function(title, label) {
Expand All @@ -66,13 +87,13 @@ qx.Class.define("osparc.desktop.preferences.window.ShowAPIKey", {
});
const sTitle = new qx.ui.basic.Label(title).set({
rich: true,
width: 40
width: 60
});
hBox.add(sTitle);
const sLabel = new qx.ui.basic.Label();
if (label) {
// partially hide the key and secret
sLabel.setValue(label.substring(1, 8) + "****")
sLabel.setValue(label);
}
hBox.add(sLabel);
return hBox;
Expand Down

0 comments on commit eceb799

Please sign in to comment.