1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-24 23:59:47 +02:00

Merge pull request #19 from documize/save-secrets

Keep user section secrets safe
This commit is contained in:
Elliott Stoneham 2016-07-08 20:27:02 +01:00 committed by GitHub
commit 1c92691bdb
9 changed files with 489 additions and 392 deletions

View file

@ -15,194 +15,212 @@ import TooltipMixin from '../../../mixins/tooltip';
import SectionMixin from '../../../mixins/section'; import SectionMixin from '../../../mixins/section';
export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, { export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, {
sectionService: Ember.inject.service('section'), sectionService: Ember.inject.service('section'),
isDirty: false, isDirty: false,
waiting: false, waiting: false,
authenticated: false, authenticated: false,
user: {}, user: {},
workspaces: [], workspaces: [],
config: {}, config: {},
didReceiveAttrs() { didReceiveAttrs() {
let config = {}; let config = {};
try { try {
config = JSON.parse(this.get('meta.config')); config = JSON.parse(this.get('meta.config'));
} catch (e) {} } catch (e) {}
if (is.empty(config)) { if (is.empty(config)) {
config = { config = {
APIKey: "", APIKey: "",
filter: {}, filter: {},
itemCount: 0, itemCount: 0,
url: "", url: "",
userId: 0, userId: 0,
username: "", username: "",
workspaceId: 0, workspaceId: 0,
workspaceName: "", workspaceName: "",
}; };
} }
this.set('config', config); this.set('config', config);
if (this.get('config.userId') > 0) { let self = this;
this.send('auth'); self.set('waiting', true);
} this.get('sectionService').fetch(this.get('page'), "secrets", this.get('config'))
}, .then(function (response) {
console.log(response);
self.set('waiting', false);
willDestroyElement() { self.set('config.APIKey', response.apikey);
this.destroyTooltips(); self.set('config.url', response.url);
}, self.set('config.username', response.username);
getWorkspaces() { if (response.apikey.length > 0 && response.url.length > 0 && response.username.length > 0) {
let page = this.get('page'); self.send('auth');
let self = this; }
this.set('waiting', true); }, function (reason) { //jshint ignore: line
console.log(reason);
self.set('waiting', false);
if (self.get('config.userId') > 0) {
self.send('auth');
}
});
},
this.get('sectionService').fetch(page, "workspace", this.get('config')) willDestroyElement() {
.then(function(response) { this.destroyTooltips();
// console.log(response); },
let workspaceId = self.get('config.workspaceId');
if (response.length > 0 && workspaceId === 0) { getWorkspaces() {
workspaceId = response[0].Id; let page = this.get('page');
} let self = this;
this.set('waiting', true);
self.set("config.workspaceId", workspaceId); this.get('sectionService').fetch(page, "workspace", this.get('config'))
self.set('workspaces', response); .then(function (response) {
self.selectWorkspace(workspaceId); // console.log(response);
let workspaceId = self.get('config.workspaceId');
Ember.run.schedule('afterRender', function() { if (response.length > 0 && workspaceId === 0) {
window.scrollTo(0, document.body.scrollHeight); workspaceId = response[0].Id;
}
response.forEach(function(workspace) { self.set("config.workspaceId", workspaceId);
self.addTooltip(document.getElementById("gemini-workspace-" + workspace.Id)); self.set('workspaces', response);
}); self.selectWorkspace(workspaceId);
});
self.set('waiting', false);
}, function(reason) { //jshint ignore: line
self.set('workspaces', []);
self.set('waiting', false);
});
},
getItems() { Ember.run.schedule('afterRender', function () {
let page = this.get('page'); window.scrollTo(0, document.body.scrollHeight);
let self = this;
this.set('waiting', true); response.forEach(function (workspace) {
self.addTooltip(document.getElementById("gemini-workspace-" + workspace.Id));
});
});
self.set('waiting', false);
}, function (reason) { //jshint ignore: line
self.set('workspaces', []);
self.set('waiting', false);
});
},
this.get('sectionService').fetch(page, "items", this.get('config')) getItems() {
.then(function(response) { let page = this.get('page');
// console.log(response); let self = this;
self.set('items', response);
self.set('config.itemCount', response.length);
self.set('waiting', false);
}, function(reason) { //jshint ignore: line
self.set('items', []);
self.set('waiting', false);
});
},
selectWorkspace(id) { this.set('waiting', true);
let self = this;
let w = this.get('workspaces');
w.forEach(function(w) { this.get('sectionService').fetch(page, "items", this.get('config'))
Ember.set(w, 'selected', w.Id === id); .then(function (response) {
// console.log(response);
self.set('items', response);
self.set('config.itemCount', response.length);
self.set('waiting', false);
}, function (reason) { //jshint ignore: line
self.set('items', []);
self.set('waiting', false);
});
},
if (w.Id === id) { selectWorkspace(id) {
self.set("config.filter", w.Filter); let self = this;
self.set("config.workspaceId", id); let w = this.get('workspaces');
self.set("config.workspaceName", w.Title);
// console.log(self.get('config'));
}
});
this.set('workspaces', w); w.forEach(function (w) {
this.getItems(); Ember.set(w, 'selected', w.Id === id);
},
actions: { if (w.Id === id) {
isDirty() { self.set("config.filter", w.Filter);
return this.get('isDirty'); self.set("config.workspaceId", id);
}, self.set("config.workspaceName", w.Title);
// console.log(self.get('config'));
}
});
auth() { this.set('workspaces', w);
// missing data? this.getItems();
if (is.empty(this.get('config.url'))) { },
$("#gemini-url").addClass("error").focus();
return;
}
if (is.empty(this.get('config.username'))) {
$("#gemini-username").addClass("error").focus();
return;
}
if (is.empty(this.get('config.APIKey'))) {
$("#gemini-apikey").addClass("error").focus();
return;
}
// knock out spaces actions: {
this.set('config.url', this.get('config.url').trim()); isDirty() {
this.set('config.username', this.get('config.username').trim()); return this.get('isDirty');
this.set('config.APIKey', this.get('config.APIKey').trim()); },
// remove trailing slash in URL auth() {
let url = this.get('config.url'); // missing data?
if (url.indexOf("/", url.length - 1) !== -1) { if (is.empty(this.get('config.url'))) {
this.set('config.url', url.substring(0, url.length - 1)); $("#gemini-url").addClass("error").focus();
} return;
}
if (is.empty(this.get('config.username'))) {
$("#gemini-username").addClass("error").focus();
return;
}
if (is.empty(this.get('config.APIKey'))) {
$("#gemini-apikey").addClass("error").focus();
return;
}
let page = this.get('page'); // knock out spaces
let self = this; this.set('config.url', this.get('config.url').trim());
this.set('config.username', this.get('config.username').trim());
this.set('config.APIKey', this.get('config.APIKey').trim());
this.set('waiting', true); // remove trailing slash in URL
let url = this.get('config.url');
if (url.indexOf("/", url.length - 1) !== -1) {
this.set('config.url', url.substring(0, url.length - 1));
}
this.get('sectionService').fetch(page, "auth", this.get('config')) let page = this.get('page');
.then(function(response) { let self = this;
self.set('authenticated', true);
self.set('user', response);
self.set('config.userId', response.BaseEntity.id);
self.set('waiting', false);
self.getWorkspaces();
}, function(reason) { //jshint ignore: line
self.set('authenticated', false);
self.set('user', null);
self.set('config.userId', 0);
self.set('waiting', false);
switch (reason.status) { this.set('waiting', true);
case 400:
self.showNotification(`Unable to connect to Gemini URL`);
break;
case 403:
self.showNotification(`Unable to authenticate`);
break;
default:
self.showNotification(`Something went wrong, try again!`);
}
});
},
onWorkspaceChange(id) { this.get('sectionService').fetch(page, "auth", this.get('config'))
this.set('isDirty', true); .then(function (response) {
this.selectWorkspace(id); self.set('authenticated', true);
}, self.set('user', response);
self.set('config.userId', response.BaseEntity.id);
self.set('waiting', false);
self.getWorkspaces();
}, function (reason) { //jshint ignore: line
self.set('authenticated', false);
self.set('user', null);
self.set('config.userId', 0);
self.set('waiting', false);
onCancel() { switch (reason.status) {
this.attrs.onCancel(); case 400:
}, self.showNotification(`Unable to connect to Gemini URL`);
break;
case 403:
self.showNotification(`Unable to authenticate`);
break;
default:
self.showNotification(`Something went wrong, try again!`);
}
});
},
onAction(title) { onWorkspaceChange(id) {
let page = this.get('page'); this.set('isDirty', true);
let meta = this.get('meta'); this.selectWorkspace(id);
page.set('title', title); },
meta.set('rawBody', JSON.stringify(this.get("items")));
meta.set('config', JSON.stringify(this.get('config')));
meta.set('externalSource', true);
this.attrs.onAction(page, meta); onCancel() {
} this.attrs.onCancel();
} },
onAction(title) {
let page = this.get('page');
let meta = this.get('meta');
page.set('title', title);
meta.set('rawBody', JSON.stringify(this.get("items")));
meta.set('config', JSON.stringify(this.get('config')));
meta.set('externalSource', true);
this.attrs.onAction(page, meta);
}
}
}); });

View file

@ -42,9 +42,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
this.set('config', config); this.set('config', config);
if (this.get('config.APIToken').length > 0) { this.send('auth');
this.send('auth');
}
}, },
willDestroyElement() { willDestroyElement() {
@ -65,15 +63,6 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
}, },
auth() { auth() {
// missing data?
this.set('config.APIToken', this.get('config.APIToken').trim());
if (is.empty(this.get('config.APIToken'))) {
$("#papertrail-apitoken").addClass("error").focus();
console.log("auth token empty");
return;
}
let page = this.get('page'); let page = this.get('page');
let config = this.get('config'); let config = this.get('config');
let self = this; let self = this;
@ -93,7 +82,12 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
self.set('waiting', false); self.set('waiting', false);
let options = self.get('options'); let options = self.get('options');
let group = _.findWhere(options.groups, { id: config.group.id }); let group = {};
if (is.not.null(config.group)) {
group = _.findWhere(options.groups, { id: config.group.id });
} else {
group = options.groups[0];
}
if (is.not.undefined(group)) { if (is.not.undefined(group)) {
Ember.set(config, 'group', group); Ember.set(config, 'group', group);
} }

View file

@ -16,50 +16,50 @@ import TooltipMixin from '../../../mixins/tooltip';
import SectionMixin from '../../../mixins/section'; import SectionMixin from '../../../mixins/section';
export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, { export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, {
sectionService: Ember.inject.service('section'), sectionService: Ember.inject.service('section'),
isDirty: false, isDirty: false,
busy: false, busy: false,
authenticated: false, authenticated: false,
config: {}, config: {},
boards: null, boards: null,
noBoards: false, noBoards: false,
appKey: "", appKey: "",
boardStyle: Ember.computed('config.board', function() { boardStyle: Ember.computed('config.board', function () {
let board = this.get('config.board'); let board = this.get('config.board');
if (is.null(board) || is.undefined(board)) { if (is.null(board) || is.undefined(board)) {
return "#4c4c4c"; return "#4c4c4c";
} }
let color = board.prefs.backgroundColor; let color = board.prefs.backgroundColor;
return Ember.String.htmlSafe("background-color: " + color); return Ember.String.htmlSafe("background-color: " + color);
}), }),
didReceiveAttrs() { didReceiveAttrs() {
let page = this.get('page'); let page = this.get('page');
let config = {}; let config = {};
let self = this; let self = this;
try { try {
config = JSON.parse(this.get('meta.config')); config = JSON.parse(this.get('meta.config'));
} } catch (e) {}
catch (e) {}
if (is.empty(config)) { if (is.empty(config)) {
config = { config = {
token: "", token: "",
user: null, user: null,
board: null, board: null,
lists: [] lists: []
}; };
} }
this.set('config', config); this.set('config', config);
this.get('sectionService').fetch(page, "config", {}) this.get('sectionService').fetch(page, "config", {})
.then(function(s) { .then(function (s) {
self.set('appKey', s.appKey); self.set('appKey', s.appKey);
self.set('config.token', s.token); // the user's own token has been stored in the DB
// On auth callback capture user token // On auth callback capture user token
let hashToken = window.location.hash; let hashToken = window.location.hash;
@ -70,175 +70,173 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
} }
} }
if (self.get('appKey') !== "" && self.get('config.token') !== "") { if (self.get('appKey') !== "" && self.get('config.token') !== "") {
self.send('auth'); self.send('auth');
} } else {
else { Ember.$.getScript("https://api.trello.com/1/client.js?key=" + self.get('appKey'), function () {
Ember.$.getScript("https://api.trello.com/1/client.js?key=" + self.get('appKey'), function() { Trello.deauthorize();
Trello.deauthorize(); });
}); }
} }, function (error) { //jshint ignore: line
}, function(error) { //jshint ignore: line console.log(error);
console.log(error); });
}); },
},
willDestroyElement() { willDestroyElement() {
this.destroyTooltips(); this.destroyTooltips();
}, },
getBoardLists() { getBoardLists() {
this.set('busy', true); this.set('busy', true);
let self = this; let self = this;
let boards = this.get('boards'); let boards = this.get('boards');
let board = this.get('config.board'); let board = this.get('config.board');
let page = this.get('page'); let page = this.get('page');
if (is.null(boards) || is.undefined(boards) || boards.length === 0) { if (is.null(boards) || is.undefined(boards) || boards.length === 0) {
this.set('noBoards', true); this.set('noBoards', true);
return; return;
} }
this.set('noBoards', false); this.set('noBoards', false);
if (is.null(board) || is.undefined(board)) { if (is.null(board) || is.undefined(board)) {
if (boards.length) { if (boards.length) {
board = boards[0]; board = boards[0];
this.set('config.board', board); this.set('config.board', board);
} }
} } else {
else { this.set('config.board', boards.findBy('id', board.id));
this.set('config.board', boards.findBy('id', board.id)); }
}
this.get('sectionService').fetch(page, "lists", self.get('config')) this.get('sectionService').fetch(page, "lists", self.get('config'))
.then(function(lists) { .then(function (lists) {
let savedLists = self.get('config.lists'); let savedLists = self.get('config.lists');
if (savedLists === null) { if (savedLists === null) {
savedLists = []; savedLists = [];
} }
lists.forEach(function(list) { lists.forEach(function (list) {
let saved = savedLists.findBy("id", list.id); let saved = savedLists.findBy("id", list.id);
let included = true; let included = true;
if (is.not.undefined(saved)) { if (is.not.undefined(saved)) {
included = saved.included; included = saved.included;
} }
list.included = included; list.included = included;
}); });
self.set('config.lists', lists); self.set('config.lists', lists);
self.set('busy', false); self.set('busy', false);
}, function(error) { //jshint ignore: line }, function (error) { //jshint ignore: line
self.set('busy', false); self.set('busy', false);
self.set('authenticated', false); self.set('authenticated', false);
self.showNotification("Unable to fetch board lists"); self.showNotification("Unable to fetch board lists");
console.log(error); console.log(error);
}); });
}, },
actions: { actions: {
isDirty() { isDirty() {
return this.get('isDirty'); return this.get('isDirty');
}, },
onListCheckbox(id) { onListCheckbox(id) {
let lists = this.get('config.lists'); let lists = this.get('config.lists');
let list = lists.findBy('id', id); let list = lists.findBy('id', id);
if (list !== null) { if (list !== null) {
Ember.set(list, 'included', !list.included); Ember.set(list, 'included', !list.included);
} }
}, },
auth() { auth() {
if (this.get('appKey') === "") { if (this.get('appKey') === "") {
$("#trello-appkey").addClass('error').focus(); $("#trello-appkey").addClass('error').focus();
this.set('authenticated', false); this.set('authenticated', false);
return; return;
} }
let self = this; let self = this;
let page = this.get('page'); let page = this.get('page');
self.set('busy', true); self.set('busy', true);
Ember.$.getScript("https://api.trello.com/1/client.js?key=" + this.get('appKey'), function() { Ember.$.getScript("https://api.trello.com/1/client.js?key=" + this.get('appKey'), function () {
Trello.authorize({ Trello.authorize({
type: "redirect", type: "redirect",
interactive: true, interactive: true,
name: "Documize", name: "Documize",
scope: { scope: {
read: true, read: true,
write: false write: false
}, },
expiration: "never", expiration: "never",
persist: true, persist: true,
success: function() { success: function () {
self.set('authenticated', true); self.set('authenticated', true);
self.set('config.token', Trello.token()); self.set('config.token', Trello.token());
self.set('busy', true); self.set('busy', true);
Trello.members.get("me", function(user) { Trello.members.get("me", function (user) {
self.set('config.user', user); self.set('config.user', user);
}, function(error) { }, function (error) {
console.log(error); console.log(error);
}); });
self.get('sectionService').fetch(page, "boards", self.get('config')) self.get('sectionService').fetch(page, "boards", self.get('config'))
.then(function(boards) { .then(function (boards) {
self.set('busy', false); self.set('busy', false);
self.set('boards', boards); self.set('boards', boards);
self.getBoardLists(); self.getBoardLists();
}, function(error) { //jshint ignore: line }, function (error) { //jshint ignore: line
self.set('busy', false); self.set('busy', false);
self.set('authenticated', false); self.set('authenticated', false);
self.showNotification("Unable to fetch boards"); self.showNotification("Unable to fetch boards");
console.log(error); console.log(error);
}); });
}, },
error: function(error) { error: function (error) {
self.set('busy', false); self.set('busy', false);
self.set('authenticated', false); self.set('authenticated', false);
self.showNotification("Unable to authenticate"); self.showNotification("Unable to authenticate");
console.log(error); console.log(error);
} }
}); });
}); });
}, },
onBoardChange(board) { onBoardChange(board) {
this.set('isDirty', true); this.set('isDirty', true);
this.set('config.board', board); this.set('config.board', board);
this.set('config.lists', []); this.set('config.lists', []);
this.getBoardLists(); this.getBoardLists();
}, },
onCancel() { onCancel() {
this.attrs.onCancel(); this.attrs.onCancel();
}, },
onAction(title) { onAction(title) {
this.set('busy', true); this.set('busy', true);
let self = this; let self = this;
let page = this.get('page'); let page = this.get('page');
let meta = this.get('meta'); let meta = this.get('meta');
page.set('title', title); page.set('title', title);
meta.set('rawBody', ''); meta.set('rawBody', '');
meta.set('config', JSON.stringify(this.get('config'))); meta.set('config', JSON.stringify(this.get('config')));
meta.set('externalSource', true); meta.set('externalSource', true);
this.get('sectionService').fetch(page, "cards", this.get('config')) this.get('sectionService').fetch(page, "cards", this.get('config'))
.then(function(response) { .then(function (response) {
meta.set('rawBody', JSON.stringify(response)); meta.set('rawBody', JSON.stringify(response));
self.set('busy', false); self.set('busy', false);
self.attrs.onAction(page, meta); self.attrs.onAction(page, meta);
}, function(reason) { //jshint ignore: line }, function (reason) { //jshint ignore: line
self.set('busy', false); self.set('busy', false);
self.attrs.onAction(page, meta); self.attrs.onAction(page, meta);
}); });
} }
} }
}); });

View file

@ -74,12 +74,14 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
} }
switch method { switch method {
case "secrets":
secs(ctx, w, r)
case "auth": case "auth":
auth(w, r) auth(ctx, w, r)
case "workspace": case "workspace":
workspace(w, r) workspace(ctx, w, r)
case "items": case "items":
items(w, r) items(ctx, w, r)
} }
} }
@ -93,7 +95,7 @@ func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData st
return return
} }
c.Clean() c.Clean(ctx)
if len(c.URL) == 0 { if len(c.URL) == 0 {
log.Info("Gemini.Refresh received empty URL") log.Info("Gemini.Refresh received empty URL")
@ -150,7 +152,7 @@ func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData st
return return
} }
func auth(w http.ResponseWriter, r *http.Request) { func auth(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body) body, err := ioutil.ReadAll(r.Body)
@ -167,7 +169,7 @@ func auth(w http.ResponseWriter, r *http.Request) {
return return
} }
config.Clean() config.Clean(nil) // don't look at the database for the parameters
if len(config.URL) == 0 { if len(config.URL) == 0 {
provider.WriteMessage(w, "gemini", "Missing URL value") provider.WriteMessage(w, "gemini", "Missing URL value")
@ -203,6 +205,8 @@ func auth(w http.ResponseWriter, r *http.Request) {
return return
} }
config.SaveSecrets(ctx)
defer res.Body.Close() defer res.Body.Close()
var g = geminiUser{} var g = geminiUser{}
@ -218,7 +222,7 @@ func auth(w http.ResponseWriter, r *http.Request) {
provider.WriteJSON(w, g) provider.WriteJSON(w, g)
} }
func workspace(w http.ResponseWriter, r *http.Request) { func workspace(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body) body, err := ioutil.ReadAll(r.Body)
@ -235,7 +239,7 @@ func workspace(w http.ResponseWriter, r *http.Request) {
return return
} }
config.Clean() config.Clean(ctx)
if len(config.URL) == 0 { if len(config.URL) == 0 {
provider.WriteMessage(w, "gemini", "Missing URL value") provider.WriteMessage(w, "gemini", "Missing URL value")
@ -291,7 +295,7 @@ func workspace(w http.ResponseWriter, r *http.Request) {
provider.WriteJSON(w, workspace) provider.WriteJSON(w, workspace)
} }
func items(w http.ResponseWriter, r *http.Request) { func items(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body) body, err := ioutil.ReadAll(r.Body)
@ -308,7 +312,7 @@ func items(w http.ResponseWriter, r *http.Request) {
return return
} }
config.Clean() config.Clean(ctx)
if len(config.URL) == 0 { if len(config.URL) == 0 {
provider.WriteMessage(w, "gemini", "Missing URL value") provider.WriteMessage(w, "gemini", "Missing URL value")
@ -367,3 +371,9 @@ func items(w http.ResponseWriter, r *http.Request) {
provider.WriteJSON(w, items) provider.WriteJSON(w, items)
} }
func secs(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
sec, err := getSecrets(ctx)
log.IfErr(err)
provider.WriteJSON(w, sec)
}

View file

@ -11,7 +11,12 @@
package gemini package gemini
import "strings" import (
"strings"
"github.com/documize/community/documize/section/provider"
"github.com/documize/community/wordsmith/log"
)
// the HTML that is rendered by this section. // the HTML that is rendered by this section.
const renderTemplate = ` const renderTemplate = `
@ -82,8 +87,37 @@ type geminiConfig struct {
Filter map[string]interface{} `json:"filter"` Filter map[string]interface{} `json:"filter"`
} }
func (c *geminiConfig) Clean() { func (c *geminiConfig) Clean(ctx *provider.Context) {
if ctx != nil {
sec, err := getSecrets(ctx)
if err == nil {
if len(sec.APIKey) > 0 && len(sec.Username) > 0 && len(sec.URL) > 0 {
c.APIKey = strings.TrimSpace(sec.APIKey)
c.Username = strings.TrimSpace(sec.Username)
c.URL = strings.TrimSpace(sec.URL)
}
}
}
c.APIKey = strings.TrimSpace(c.APIKey) c.APIKey = strings.TrimSpace(c.APIKey)
c.Username = strings.TrimSpace(c.Username) c.Username = strings.TrimSpace(c.Username)
c.URL = strings.TrimSpace(c.URL) c.URL = strings.TrimSpace(c.URL)
} }
func (c *geminiConfig) SaveSecrets(ctx *provider.Context) {
var sec secrets
sec.APIKey = strings.TrimSpace(c.APIKey)
sec.Username = strings.TrimSpace(c.Username)
sec.URL = strings.TrimSpace(c.URL)
log.IfErr(ctx.MarshalSecrets(sec))
}
type secrets struct {
URL string `json:"url"`
Username string `json:"username"`
APIKey string `json:"apikey"`
}
func getSecrets(ctx *provider.Context) (sec secrets, err error) {
err = ctx.UnmarshalSecrets(&sec)
return
}

View file

@ -104,7 +104,7 @@ func (p *Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http
config.Clean() config.Clean()
if config.APIToken == provider.SecretReplacement { if config.APIToken == provider.SecretReplacement || config.APIToken == "" {
config.APIToken = ctx.GetSecrets("APIToken") config.APIToken = ctx.GetSecrets("APIToken")
} }

View file

@ -209,7 +209,21 @@ func (c *Context) SaveSecrets(JSONobj string) error {
if !c.inCommand { if !c.inCommand {
return errors.New("SaveSecrets() may only be called from within Command()") return errors.New("SaveSecrets() may only be called from within Command()")
} }
return request.UserConfigSetJSON(c.OrgID, c.UserID, c.prov.Meta().ContentType, JSONobj) m := c.prov.Meta()
return request.UserConfigSetJSON(c.OrgID, c.UserID, m.ContentType, JSONobj)
}
// MarshalSecrets to the database.
// Parameter the same as for json.Marshal().
func (c *Context) MarshalSecrets(sec interface{}) error {
if !c.inCommand {
return errors.New("MarshalSecrets() may only be called from within Command()")
}
byts, err := json.Marshal(sec)
if err != nil {
return err
}
return c.SaveSecrets(string(byts))
} }
// GetSecrets for the current context user/org. // GetSecrets for the current context user/org.
@ -218,7 +232,21 @@ func (c *Context) SaveSecrets(JSONobj string) error {
// An empty JSONpath returns the whole JSON object, as JSON. // An empty JSONpath returns the whole JSON object, as JSON.
// Errors return the empty string. // Errors return the empty string.
func (c *Context) GetSecrets(JSONpath string) string { func (c *Context) GetSecrets(JSONpath string) string {
return request.UserConfigGetJSON(c.OrgID, c.UserID, c.prov.Meta().ContentType, JSONpath) m := c.prov.Meta()
return request.UserConfigGetJSON(c.OrgID, c.UserID, m.ContentType, JSONpath)
}
// ErrNoSecrets is returned if no secret is found in the database.
var ErrNoSecrets = errors.New("no secrets in database")
// UnmarshalSecrets from the database.
// Parameter the same as for "v" in json.Unmarshal().
func (c *Context) UnmarshalSecrets(v interface{}) error {
secTxt := c.GetSecrets("") // get all the json of the secrets
if len(secTxt) > 0 {
return json.Unmarshal([]byte(secTxt), v)
}
return ErrNoSecrets
} }
// sort sections in order that that should be presented. // sort sections in order that that should be presented.

View file

@ -35,6 +35,10 @@ const renderTemplate = `
</div> </div>
` `
type secrets struct {
Token string `json:"token"`
}
type trelloConfig struct { type trelloConfig struct {
AppKey string `json:"appKey"` AppKey string `json:"appKey"`
Token string `json:"token"` Token string `json:"token"`

View file

@ -25,7 +25,6 @@ import (
) )
var meta provider.TypeMeta var meta provider.TypeMeta
var appKey string
func init() { func init() {
meta = provider.TypeMeta{} meta = provider.TypeMeta{}
@ -49,11 +48,6 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
query := r.URL.Query() query := r.URL.Query()
method := query.Get("method") method := query.Get("method")
if len(method) == 0 {
provider.WriteMessage(w, "trello", "missing method name")
return
}
defer r.Body.Close() defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body) body, err := ioutil.ReadAll(r.Body)
@ -70,19 +64,20 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
return return
} }
if appKey == "" { config.Clean()
appKey = request.ConfigString(meta.ConfigHandle(), "appKey") config.AppKey = request.ConfigString(meta.ConfigHandle(), "appKey")
if len(config.AppKey) == 0 {
log.ErrorString("missing trello App Key")
provider.WriteMessage(w, "trello", "Missing appKey")
return
} }
config.Clean() if len(config.Token) == 0 {
config.AppKey = appKey config.Token = ctx.GetSecrets("token") // get a token, if we have one
}
if method != "config" { if method != "config" {
if len(config.AppKey) == 0 {
provider.WriteMessage(w, "trello", "Missing appKey")
return
}
if len(config.Token) == 0 { if len(config.Token) == 0 {
provider.WriteMessage(w, "trello", "Missing token") provider.WriteMessage(w, "trello", "Missing token")
return return
@ -94,8 +89,9 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
render, err := getCards(config) render, err := getCards(config)
if err != nil { if err != nil {
fmt.Println(err) log.IfErr(err)
provider.WriteError(w, "trello", err) provider.WriteError(w, "trello", err)
log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid
return return
} }
@ -105,8 +101,9 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
render, err := getBoards(config) render, err := getBoards(config)
if err != nil { if err != nil {
fmt.Println(err) log.IfErr(err)
provider.WriteError(w, "trello", err) provider.WriteError(w, "trello", err)
log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid
return return
} }
@ -116,24 +113,38 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
render, err := getLists(config) render, err := getLists(config)
if err != nil { if err != nil {
fmt.Println(err) log.IfErr(err)
provider.WriteError(w, "trello", err) provider.WriteError(w, "trello", err)
log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid
return return
} }
provider.WriteJSON(w, render) provider.WriteJSON(w, render)
case "config": case "config":
if method == "config" { var ret struct {
var config struct { AppKey string `json:"appKey"`
AppKey string `json:"appKey"` Token string `json:"token"`
}
config.AppKey = appKey
provider.WriteJSON(w, config)
return
} }
ret.AppKey = config.AppKey
if config.Token != "" {
ret.Token = provider.SecretReplacement
}
provider.WriteJSON(w, ret)
return
default:
log.ErrorString("trello unknown method name: " + method)
provider.WriteMessage(w, "trello", "missing method name")
return
} }
// the token has just worked, so save it as our secret
var s secrets
s.Token = config.Token
b, e := json.Marshal(s)
log.IfErr(e)
log.IfErr(ctx.SaveSecrets(string(b)))
} }
// Render just sends back HMTL as-is. // Render just sends back HMTL as-is.