From fb7d940a67854154db3c05c17ee2ea6680a77941 Mon Sep 17 00:00:00 2001 From: Elliott Stoneham Date: Thu, 7 Jul 2016 21:38:04 +0100 Subject: [PATCH] github and paper trail secrets --- .../components/section/github/type-editor.js | 6 +++--- .../section/papertrail/type-editor.js | 8 ++++++-- .../section/papertrail/type-editor.hbs | 18 ++++++++++-------- documize/section/papertrail/papertrail.go | 18 ++++++++++++++++-- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/app/app/components/section/github/type-editor.js b/app/app/components/section/github/type-editor.js index 18932bdf..a07327d5 100644 --- a/app/app/components/section/github/type-editor.js +++ b/app/app/components/section/github/type-editor.js @@ -86,9 +86,9 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, self.send('auth'); }); } else { - if (config.userId !== self.session.user.id) { + if (config.userId !== self.get("session.session.authenticated.user.id")) { console.log("github auth wrong user ID, switching"); - self.set('config.userId', self.session.user.id); + self.set('config.userId', self.get("session.session.authenticated.user.id")); } self.get('sectionService').fetch(page, "checkAuth", self.get('config')) .then(function () { @@ -361,7 +361,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, authStage2() { let self = this; - self.set('config.userId', this.session.user.id); + self.set('config.userId', self.get("session.session.authenticated.user.id")); self.set('authenticated', true); self.set('busy', true); let page = this.get('page'); diff --git a/app/app/components/section/papertrail/type-editor.js b/app/app/components/section/papertrail/type-editor.js index 31009ff9..c3245eb7 100644 --- a/app/app/components/section/papertrail/type-editor.js +++ b/app/app/components/section/papertrail/type-editor.js @@ -65,12 +65,12 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, }, auth() { - console.log(this.get('config')); // 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; } @@ -85,6 +85,7 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, self.set('authenticated', true); self.set('items', response); self.set('config.APIToken', '********'); // reset the api token once it has been sent to the host + console.log("auth token OK"); self.get('sectionService').fetch(page, "options", config) .then(function (response) { @@ -97,15 +98,18 @@ export default Ember.Component.extend(SectionMixin, NotifierMixin, TooltipMixin, Ember.set(config, 'group', group); } }, function (reason) { //jshint ignore: line + self.set('authenticated', false); self.set('waiting', false); + self.set('config.APIToken', ''); // clear the api token self.displayError(reason); + console.log("get options call failed"); }); }, function (reason) { //jshint ignore: line self.set('authenticated', false); self.set('waiting', false); self.set('config.APIToken', ''); // clear the api token self.displayError(reason); - $("#papertrail-apitoken").addClass("error").focus(); + console.log("auth token invalid"); }); }, diff --git a/app/app/templates/components/section/papertrail/type-editor.hbs b/app/app/templates/components/section/papertrail/type-editor.hbs index a4759032..4ca4ad77 100644 --- a/app/app/templates/components/section/papertrail/type-editor.hbs +++ b/app/app/templates/components/section/papertrail/type-editor.hbs @@ -2,25 +2,27 @@
-
+
Papertrail Authentication {{#if authenticated}} Complete - {{/if}} + {{/if}}
Provide your Papertrail API token
API Token (from your profile)
- {{focus-input id="papertrail-apitoken" type="password" value=config.APIToken readonly=isReadonly}} + {{focus-input id="papertrail-apitoken" type="password" value=config.APIToken }} +
+
+ {{#if authenticated}} + Re-Authenticate + {{else}} + Authenticate + {{/if}}
- {{#if authenticated}} -
Re-Authenticate
- {{else}} -
Authenticate
- {{/if}}
diff --git a/documize/section/papertrail/papertrail.go b/documize/section/papertrail/papertrail.go index 6e12f874..28e9ab80 100644 --- a/documize/section/papertrail/papertrail.go +++ b/documize/section/papertrail/papertrail.go @@ -14,6 +14,7 @@ package papertrail import ( "bytes" "encoding/json" + "errors" "fmt" "html/template" "io/ioutil" @@ -160,7 +161,14 @@ func (*Provider) Refresh(ctx *provider.Context, config, data string) (newData st func auth(ctx *provider.Context, config papertrailConfig, w http.ResponseWriter, r *http.Request) { result, err := fetchEvents(config) + if result == nil { + err = errors.New("nil result of papertrail query") + } + if err != nil { + + log.IfErr(ctx.SaveSecrets(`{"APIToken":""}`)) // invalid token, so reset it + if err.Error() == "forbidden" { provider.WriteForbidden(w) } else { @@ -256,11 +264,17 @@ func fetchEvents(config papertrailConfig) (result interface{}, err error) { filter = fmt.Sprintf("%s%sgroup_id=%d", filter, prefix, config.Group.ID) } - req, err := http.NewRequest("GET", "https://papertrailapp.com/api/v1/events/search.json?"+filter, nil) + var req *http.Request + req, err = http.NewRequest("GET", "https://papertrailapp.com/api/v1/events/search.json?"+filter, nil) + if err != nil { + log.Error("new request", err) + return + } req.Header.Set("X-Papertrail-Token", config.APIToken) client := &http.Client{} - res, err := client.Do(req) + var res *http.Response + res, err = client.Do(req) if err != nil { log.Error("message", err)