From d3264ebbed578f20a8b1a58a2e8f3c562d1d16d0 Mon Sep 17 00:00:00 2001 From: botnyx Date: Sun, 14 Apr 2019 12:39:53 +0200 Subject: [PATCH 1/3] Create fusionauth.js This module allows HelloJS to interact with a FusionAuth instance. --- src/modules/fusionauth.js | 115 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/modules/fusionauth.js diff --git a/src/modules/fusionauth.js b/src/modules/fusionauth.js new file mode 100644 index 00000000..2073e436 --- /dev/null +++ b/src/modules/fusionauth.js @@ -0,0 +1,115 @@ +/* + Module for HelloJS. + + This module allows HelloJS to interact with a FusionAuth instance. + + + Instructions: + + To interact with a FusionAuth instance, you need to set the correct host. + + There are 2 ways of doing that. + 1. Use this module as is, and add a javascript-variable before you include hello.js + + Example: + + 2. Modify this module, and replace 'window.FusionAuthHost' with your designated oauth-host + + + + +*/ + +(function(hello) { + + hello.init({ + + fusionauth: { + + name: 'fusionauth', + + oauth: { + version: "2", + auth: 'https://'+window.FusionAuthHost+'/oauth2/authorize', + response_type: 'token' + }, + + scope: { + email: 'email' + }, + + base: 'https://'+window.FusionAuthHost+'/', + + get: { + me: 'oauth2/userinfo' + }, + + wrap: { + me: function(o, headers) { + + formatError(o, headers); + formatUser(o); + + return o; + }, + + 'default': function(o, headers, req) { + + formatError(o, headers); + + if (Array.isArray(o)) { + o = {data:o}; + } + + if (o.data) { + paging(o, headers, req); + o.data.forEach(formatUser); + } + + return o; + } + }, + + xhr: function(p) { + var token = p.query.access_token; + delete p.query.access_token; + if(token){ + p.headers = { + "Authorization": "Bearer " + token, + }; + } + return true; + } + } + }); + + function formatError(o, headers) { + var code = headers ? headers.statusCode : (o && 'meta' in o && 'status' in o.meta && o.meta.status); + if ((code === 401 || code === 403)) { + o.error = { + code: 'access_denied', + message: o.message || (o.data ? o.data.message : 'Could not get response') + }; + delete o.message; + } + } + + function formatUser(o) { + if (o.id) { + o.thumbnail = o.picture = o.avatar_url; + o.name = o.login; + } + } + + function paging(res, headers, req) { + if (res.data && res.data.length && headers && headers.Link) { + var next = headers.Link.match(/<(.*?)>;\s*rel=\"next\"/); + if (next) { + res.paging = { + next: next[1] + }; + } + } + } + +})(hello); From 5007be2650db85878cbb844e1cb40ee6f541b809 Mon Sep 17 00:00:00 2001 From: botnyx Date: Sun, 14 Apr 2019 12:57:01 +0200 Subject: [PATCH 2/3] Update fusionauth.js --- src/modules/fusionauth.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/modules/fusionauth.js b/src/modules/fusionauth.js index 2073e436..dad4c485 100644 --- a/src/modules/fusionauth.js +++ b/src/modules/fusionauth.js @@ -20,13 +20,15 @@ */ + + (function(hello) { hello.init({ - fusionauth: { + trustmaster: { - name: 'fusionauth', + name: 'Trustmaster', oauth: { version: "2", @@ -113,3 +115,5 @@ } })(hello); + + From a4b11d27f041870f93187d57fd93cec4ce4dc058 Mon Sep 17 00:00:00 2001 From: botnyx Date: Sun, 14 Apr 2019 15:13:55 +0200 Subject: [PATCH 3/3] Update fusionauth.js --- src/modules/fusionauth.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/fusionauth.js b/src/modules/fusionauth.js index dad4c485..acfa0268 100644 --- a/src/modules/fusionauth.js +++ b/src/modules/fusionauth.js @@ -26,9 +26,9 @@ hello.init({ - trustmaster: { + fusionauth: { - name: 'Trustmaster', + name: 'Fusionauth', oauth: { version: "2",