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

use saved secrets in trello and paper trail

Alter trello to used stored secret token.
Alter papertrail to auto-login where a secret exists.
This commit is contained in:
Elliott Stoneham 2016-07-08 12:29:53 +01:00
parent 5f584f7895
commit 61e66e5203
5 changed files with 224 additions and 219 deletions

View file

@ -42,9 +42,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
this.set('config', config);
if (this.get('config.APIToken').length > 0) {
this.send('auth');
}
},
willDestroyElement() {
@ -65,15 +63,6 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
},
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 config = this.get('config');
let self = this;
@ -93,7 +82,12 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
self.set('waiting', false);
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)) {
Ember.set(config, 'group', group);
}

View file

@ -25,7 +25,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
noBoards: false,
appKey: "",
boardStyle: Ember.computed('config.board', function() {
boardStyle: Ember.computed('config.board', function () {
let board = this.get('config.board');
if (is.null(board) || is.undefined(board)) {
@ -43,8 +43,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
try {
config = JSON.parse(this.get('meta.config'));
}
catch (e) {}
} catch (e) {}
if (is.empty(config)) {
config = {
@ -58,8 +57,9 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
this.set('config', config);
this.get('sectionService').fetch(page, "config", {})
.then(function(s) {
.then(function (s) {
self.set('appKey', s.appKey);
self.set('config.token', s.token); // the user's own token, drawn from the DB
// On auth callback capture user token
let hashToken = window.location.hash;
@ -72,13 +72,12 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
if (self.get('appKey') !== "" && self.get('config.token') !== "") {
self.send('auth');
}
else {
Ember.$.getScript("https://api.trello.com/1/client.js?key=" + self.get('appKey'), function() {
} else {
Ember.$.getScript("https://api.trello.com/1/client.js?key=" + self.get('appKey'), function () {
Trello.deauthorize();
});
}
}, function(error) { //jshint ignore: line
}, function (error) { //jshint ignore: line
console.log(error);
});
},
@ -107,19 +106,18 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
board = boards[0];
this.set('config.board', board);
}
}
else {
} else {
this.set('config.board', boards.findBy('id', board.id));
}
this.get('sectionService').fetch(page, "lists", self.get('config'))
.then(function(lists) {
.then(function (lists) {
let savedLists = self.get('config.lists');
if (savedLists === null) {
savedLists = [];
}
lists.forEach(function(list) {
lists.forEach(function (list) {
let saved = savedLists.findBy("id", list.id);
let included = true;
if (is.not.undefined(saved)) {
@ -130,7 +128,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
self.set('config.lists', lists);
self.set('busy', false);
}, function(error) { //jshint ignore: line
}, function (error) { //jshint ignore: line
self.set('busy', false);
self.set('authenticated', false);
self.showNotification("Unable to fetch board lists");
@ -164,7 +162,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
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({
type: "redirect",
interactive: true,
@ -175,30 +173,30 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
},
expiration: "never",
persist: true,
success: function() {
success: function () {
self.set('authenticated', true);
self.set('config.token', Trello.token());
self.set('busy', true);
Trello.members.get("me", function(user) {
Trello.members.get("me", function (user) {
self.set('config.user', user);
}, function(error) {
}, function (error) {
console.log(error);
});
self.get('sectionService').fetch(page, "boards", self.get('config'))
.then(function(boards) {
.then(function (boards) {
self.set('busy', false);
self.set('boards', boards);
self.getBoardLists();
}, function(error) { //jshint ignore: line
}, function (error) { //jshint ignore: line
self.set('busy', false);
self.set('authenticated', false);
self.showNotification("Unable to fetch boards");
console.log(error);
});
},
error: function(error) {
error: function (error) {
self.set('busy', false);
self.set('authenticated', false);
self.showNotification("Unable to authenticate");
@ -231,11 +229,11 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin,
meta.set('externalSource', true);
this.get('sectionService').fetch(page, "cards", this.get('config'))
.then(function(response) {
.then(function (response) {
meta.set('rawBody', JSON.stringify(response));
self.set('busy', false);
self.attrs.onAction(page, meta);
}, function(reason) { //jshint ignore: line
}, function (reason) { //jshint ignore: line
self.set('busy', false);
self.attrs.onAction(page, meta);
});

View file

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

View file

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

View file

@ -25,7 +25,6 @@ import (
)
var meta provider.TypeMeta
var appKey string
func init() {
meta = provider.TypeMeta{}
@ -49,11 +48,6 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
query := r.URL.Query()
method := query.Get("method")
if len(method) == 0 {
provider.WriteMessage(w, "trello", "missing method name")
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
@ -70,19 +64,20 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
return
}
if appKey == "" {
appKey = request.ConfigString(meta.ConfigHandle(), "appKey")
}
config.Clean()
config.AppKey = appKey
config.AppKey = request.ConfigString(meta.ConfigHandle(), "appKey")
if method != "config" {
if len(config.AppKey) == 0 {
log.ErrorString("missing trello App Key")
provider.WriteMessage(w, "trello", "Missing appKey")
return
}
if len(config.Token) == 0 {
config.Token = ctx.GetSecrets("token") // get a token, if we have one
}
if method != "config" {
if len(config.Token) == 0 {
provider.WriteMessage(w, "trello", "Missing token")
return
@ -94,8 +89,9 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
render, err := getCards(config)
if err != nil {
fmt.Println(err)
log.IfErr(err)
provider.WriteError(w, "trello", err)
log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid
return
}
@ -105,8 +101,9 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
render, err := getBoards(config)
if err != nil {
fmt.Println(err)
log.IfErr(err)
provider.WriteError(w, "trello", err)
log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid
return
}
@ -116,24 +113,36 @@ func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.R
render, err := getLists(config)
if err != nil {
fmt.Println(err)
log.IfErr(err)
provider.WriteError(w, "trello", err)
log.IfErr(ctx.SaveSecrets("")) // failure means our secrets are invalid
return
}
provider.WriteJSON(w, render)
case "config":
if method == "config" {
var config struct {
var ret struct {
AppKey string `json:"appKey"`
Token string `json:"token"`
}
ret.AppKey = config.AppKey
ret.Token = config.Token
provider.WriteJSON(w, ret)
return
config.AppKey = appKey
provider.WriteJSON(w, config)
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.