Skip to content

Commit

Permalink
Merge pull request #1241 from appknox/freshdesk-login-updates
Browse files Browse the repository at this point in the history
OIDC login workflow
  • Loading branch information
future-pirate-king authored Feb 27, 2024
2 parents 339bba9 + 110be9f commit 3289ad1
Show file tree
Hide file tree
Showing 27 changed files with 1,138 additions and 98 deletions.
64 changes: 36 additions & 28 deletions app/authenticators/irene.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint-disable prettier/prettier, ember/no-get */
/* eslint-disable ember/no-get */
import Base from 'ember-simple-auth/authenticators/base';
import ENV from 'irene/config/environment';
import { inject as service } from '@ember/service';
import { getOwner } from '@ember/application';


const b64EncodeUnicode = str =>
btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode(`0x${p1}`))
)
;

const b64EncodeUnicode = (str) =>
btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) =>
String.fromCharCode(`0x${p1}`)
)
);
const getB64Token = (user, token) => b64EncodeUnicode(`${user}:${token}`);

const processData = (data) => {
Expand All @@ -18,55 +18,63 @@ const processData = (data) => {
};

const IreneAuthenticator = Base.extend({

ajax: service(),

resumeTransistion() {
const authenticatedRoute = getOwner(this).lookup("route:authenticated");
const lastTransition = authenticatedRoute.get("lastTransition");
const authenticatedRoute = getOwner(this).lookup('route:authenticated');
const lastTransition = authenticatedRoute.get('lastTransition');

if (lastTransition) {
return lastTransition.retry();
} else {
const applicationRoute = getOwner(this).lookup("route:application");
return applicationRoute.transitionTo(ENV['ember-simple-auth']["routeAfterAuthentication"]);
const applicationRoute = getOwner(this).lookup('route:application');

return applicationRoute.transitionTo(
ENV['ember-simple-auth']['routeAfterAuthentication']
);
}
},

async authenticate(identification, password, otp) {
const ajax = this.get("ajax");
const ajax = this.get('ajax');

const data = {
username: identification,
password,
otp
}
otp,
};

const url = ENV['ember-simple-auth']['loginEndPoint'];
return ajax.post(url, { data })
.then(data => {
data = processData(data);
this.resumeTransistion();
return data;
});

return ajax.post(url, { data }).then((data) => {
data = processData(data);

return data;
});
},

async restore(data) {
const ajax = this.get("ajax");
const ajax = this.get('ajax');
const url = ENV['ember-simple-auth']['checkEndPoint'];

await ajax.post(url, {
data: {},
headers: {
'Authorization': `Basic ${data.b64token}`
}
})
Authorization: `Basic ${data.b64token}`,
},
});

return data;
},

async invalidate() {
const ajax = this.get("ajax");
const ajax = this.get('ajax');
const url = ENV['ember-simple-auth']['logoutEndPoint'];

await ajax.post(url);

location.reload();
}
},
});


export default IreneAuthenticator;
58 changes: 37 additions & 21 deletions app/authenticators/saml2.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
/* eslint-disable prettier/prettier, ember/no-get */
/* eslint-disable ember/no-get */
import IreneAuth from './irene';
import ENV from 'irene/config/environment';
import { Promise } from 'rsvp';
import { getOwner } from '@ember/application';

const b64EncodeUnicode = str => btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode(`0x${p1}`)));
const b64EncodeUnicode = (str) =>
btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) =>
String.fromCharCode(`0x${p1}`)
)
);

const getB64Token = (user, token) => b64EncodeUnicode(`${user}:${token}`);

const processData = data => {
const processData = (data) => {
data.b64token = getB64Token(data.user_id, data.token);

return data;
};

export default IreneAuth.extend({
authenticate(ssotoken) {
return new Promise((resolve, reject) => {
const url = ENV['endpoints']['saml2Login'];
this.get("ajax").post(
url, { data: {token: ssotoken}}
).then((data) => {
data = processData(data);
resolve(data);
this.resumeTransistion();
}, (error) => {
let msg = "Login failed";
if(error.payload.message) {
msg = "Login failed: " + error.payload.message;
}
this.get("notify").error(msg);

const authenticatedRoute = getOwner(this).lookup("route:authenticated");
authenticatedRoute.transitionTo('login');
return reject(msg);
});

this.get('ajax')
.post(url, { data: { token: ssotoken } })
.then(
(data) => {
data = processData(data);

resolve(data);
},
(error) => {
let msg = 'Login failed';

if (error.payload.message) {
msg = 'Login failed: ' + error.payload.message;
}

this.get('notify').error(msg);

const authenticatedRoute = getOwner(this).lookup(
'route:authenticated'
);

authenticatedRoute.transitionTo('login');

return reject(msg);
}
);
});
}
},
});
Loading

0 comments on commit 3289ad1

Please sign in to comment.