admin管理员组文章数量:1310273
I'm working on a ZenDesk app that pulls customer info from a back-end system. We need to authenticate against that system using OAuth 2's browser-based authentication flow.
It's no problem to include a link to the authentication page, something like:
?
response_type=token&
client_id=CLIENT_ID&
redirect_uri=REDIRECT_URI&
scope=photos
Once the user has logged in, though, the OAuth server wants to redirect the client and include the authorization token. So the REDIRECT_URI would typically look like:
However, ZenDesk already makes use of the fragment identifier to indicate what content to show on the page:
My ZD App only appears on certain pages, so how can I both
- have my app rendered and Javascript run, and
- have the fragment identifier with the auth token available?
(I do have control over the backend OAuth server, so if you can't think of a nice clean way to acplish this, OAuth server-side hack suggestions are also gratefully accepted.)
I'm working on a ZenDesk app that pulls customer info from a back-end system. We need to authenticate against that system using OAuth 2's browser-based authentication flow.
It's no problem to include a link to the authentication page, something like:
https://oauth2server./auth?
response_type=token&
client_id=CLIENT_ID&
redirect_uri=REDIRECT_URI&
scope=photos
Once the user has logged in, though, the OAuth server wants to redirect the client and include the authorization token. So the REDIRECT_URI would typically look like:
https://example.zendesk./agent/#token=ACCESS_TOKEN
However, ZenDesk already makes use of the fragment identifier to indicate what content to show on the page:
https://example.zendesk./agent/#/dashboard
https://example.zendesk./agent/#/tickets/1234
My ZD App only appears on certain pages, so how can I both
- have my app rendered and Javascript run, and
- have the fragment identifier with the auth token available?
(I do have control over the backend OAuth server, so if you can't think of a nice clean way to acplish this, OAuth server-side hack suggestions are also gratefully accepted.)
Share Improve this question asked Aug 14, 2013 at 13:34 ESVESV 7,7304 gold badges40 silver badges30 bronze badges2 Answers
Reset to default 9Here's a really simple ZenDesk App (framework version 0.5) that
- authenticates against Google (in a seperate popup window)
- fetches a custom ticket field value from the currently visible ticket
- retrieves the Google user's name
In manifest.json, this ZenDesk App should specify "location": "ticket_sidebar"
.
app.js
(function (window) {
return {
zenDeskSubdomain: 'YOUR_ZENDESK_SUBDOMAIN',
googleClientId: 'YOUR_GOOGLE_CLIENT_ID',
events: {
'app.activated': 'onActivate',
'app.deactivated': 'onDeactivate',
'click .loginout': 'onLogInOutClick',
'click .show-username': 'onShowUserNameClick'
},
requests: {
getUserInfo: function (access_token) {
return {
url: 'https://www.googleapis./oauth2/v1/userinfo?access_token=' + access_token,
type: 'GET',
proxy_v2: true
};
}
},
onActivate: function () {
console.info("onActivate()");
this.accessToken();
var user_id = this.ticket().customField("custom_field_22931898");
this.$('.userid').text(user_id);
},
onDeactivate: function () {
console.info("onDeactivate()");
if (this.timer) {
clearTimeout(this.timer);
}
},
onShowUserNameClick: function () {
var access_token = this.accessToken();
if (!access_token) {
console.info("Can't do it! No access_token!");
return;
}
this.ajax('getUserInfo', access_token)
.done(function (data) {
console.info(data);
this.$('.username').text(data.name);
});
},
onLogInOutClick: function (event) {
if (this.accessToken()) {
this.logout(event);
} else {
this.login(event);
}
},
login: function (event) {
console.info("login()");
event.preventDefault();
var popup = this.createLoginPopup();
this.awaitAccessToken(popup);
},
logout: function (event) {
console.info("logout()");
event.preventDefault();
this.accessToken(null);
console.info(" access_token = " + this.accessToken());
this.$('.loginout').text('login');
},
createLoginPopup: function () {
console.info("createLoginPopup()");
return window.open(
'https://accounts.google./o/oauth2/auth?response_type=token&client_id=' + this.googleClientId + '&redirect_uri=https%3A%2F%2F' + this.zenDeskSubdomain + '.zendesk.%2Fagent%2F&scope=https%3A%2F%2Fwww.googleapis.%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.%2Fauth%2Fuserinfo.profile',
'Login Popup',
'width=400,height=400');
},
timer: null,
awaitAccessToken: function (popup) {
console.info("awaitAccessToken()");
if (this.isLoaded(popup)) {
console.info(" popup is loaded");
} else {
console.info(" popup is NOT loaded; sleeping");
var t = this;
this.timer = setTimeout(
function () { t.awaitAccessToken(popup); },
1000);
return;
}
var access_token = this.parseAccessToken(popup.location.href);
if (access_token) {
console.info(' access_token = ' + access_token);
popup.close();
this.accessToken(access_token);
} else {
services.notify('Error requesting code...');
}
},
isLoaded: function (win) {
try {
return ('about:blank' !== win.location.href)
&& (null !== win.document.body.innerHTML);
} catch (err) {
return false;
}
},
parseAccessToken: function (uri) {
var match = uri.match(/[#&]access_token=([^&]*)/i);
return match[1] || null;
},
accessToken: function (value) {
if (1 === arguments.length) {
console.info("Storing access_token = " + value);
this.store({ access_token: value });
}
var token = this.store('access_token');
console.info("access_token = " + value);
var loginout = this.$('.loginout');
if (token) {
loginout.text('logout');
} else {
loginout.text('login');
}
return token;
}
};
}(this));
layout.hdbs
<header>
<span class="logo"/>
<h3>{{setting "name"}}</h3>
</header>
<section data-main/>
<footer>
<div><a class="loginout">login</a></div>
<div><a class="show-username">show username</a></div>
<div><b>user id: </b><span class="userid">unknown</span></div>
<div><b>username: </b><span class="username">unknown</span></div>
</footer>
Google OAuth configuration
Configure Google OAuth to allow traffic from your application.
Redirect URIs- http://localhost:XXX - for the ZAT development/testing environment
- https://YOUR_ZENDESK_SUBDOMAIN.zendesk./agent/ - for production
- http://localhost:XXX - for the ZAT development/testing environment
- https://YOUR_ZENDESK_SUBDOMAIN.zendesk. - for production
You can use a lightweight server-side app to manage the authorization flow and tokens. The Zendesk app can municate with it through the framework's iframe helper. I wrote an extended tutorial for the Zendesk Help Center at https://support.zendesk./hc/en-us/articles/205225558.
本文标签: javascriptZenDesk App OAuth browserbased authenticationStack Overflow
版权声明:本文标题:javascript - ZenDesk App OAuth browser-based authentication - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741839204a2400397.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论