Skip to content

Instantly share code, notes, and snippets.

@andreivmaksimov
Last active January 15, 2018 10:49
Show Gist options
  • Save andreivmaksimov/53316e1d3351c46fb1e39c14e52a1299 to your computer and use it in GitHub Desktop.
Save andreivmaksimov/53316e1d3351c46fb1e39c14e52a1299 to your computer and use it in GitHub Desktop.
How to integrate Zendesk Mobile SDK with Firebase using AWS Lambda or Google Cloud Functions
#!/usr/bin/env bash
firebase deploy --only functions
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var jwt = require('jwt-simple');
var uuid = require('uuid');
var url = require('url');
var subdomain = 'dev-ops-notes'; // You Zendesk sub-domain
var shared_key = '.....'; // Zendesk provided shared key
exports.jwt_auth = functions.https.onRequest((req, res) => {
// Uncomment the following code if you want to
//console.log('Request method', req.method);
//console.log('Request: ', req);
//console.log('Body: ', req.body);
//console.log('Query: ', req.query);
if (!req.body.user_token) {
console.error('No jwt token provided in URL');
res.status(401).send('Unauthorized');
return;
}
const jwt_token = req.body.user_token;
console.log("Verifying token...");
admin.auth().verifyIdToken(jwt_token).then(decodedIdToken => {
console.log('ID Token correctly decoded', decodedIdToken);
let user = decodedIdToken;
var displayName = user.email;
if (user.displayName != null) {
displayName = user.displayName;
}
var payload = {
iat: (new Date().getTime() / 1000),
jti: uuid.v4(),
name: displayName,
email: user.email
};
// encode
var token = jwt.encode(payload, shared_key);
console.log('Token', token)
var redirect = 'https://' + subdomain + '.zendesk.com/access/jwt?jwt=' + token;
var query = url.parse(req.url, true).query;
if(query['return_to']) {
redirect += '&return_to=' + encodeURIComponent(query['return_to']);
}
console.log('Redirect response', redirect)
let response = {
"jwt": token
}
res.status(200).send(response)
return;
}).catch(error => {
console.error('Error while verifying Firebase ID token:', error);
res.status(401).send('Unauthorized');
return;
});
});
//Launch MyViewController options
URLProtocol.registerClass(ZDKAuthenticationURLProtocol.self)
let jwtUserIdentity = ZDKJwtIdentity(jwtUserIdentifier:idToken)
ZDKConfig.instance().userIdentity = jwtUserIdentity
let helpCenterContentModel = ZDKHelpCenterOverviewContentModel.defaultContent()
ZDKHelpCenter.presentOverview(self, with: helpCenterContentModel)
//Support button pressed method content
if let currentUser = Auth.auth().currentUser {
currentUser.getTokenForcingRefresh(true, completion: { (idToken, error) in
if let error = error {
debugPrint("Error obtaining user token: %@", error)
} else {
URLProtocol.registerClass(ZDKAuthenticationURLProtocol.self)
let jwtUserIdentity = ZDKJwtIdentity(jwtUserIdentifier:idToken)
ZDKConfig.instance().userIdentity = jwtUserIdentity
// Create a Content Model to pass in
let helpCenterContentModel = ZDKHelpCenterOverviewContentModel.defaultContent()
ZDKHelpCenter.presentOverview(self, with: helpCenterContentModel)
}
})
}
#!/usr/bin/env bash
npm install firebase-functions
npm install firebase-admin
npm install jwt-simple
npm install uuid
npm install url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment