Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace fetch by XHR and remove _dispatchEvent #314

Merged
merged 2 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 39 additions & 40 deletions src/stimulus/Resources/assets/dist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class default_1 extends Controller {
initialize() {
this._dispatchEvent = this._dispatchEvent.bind(this);
this._getData = this._getData.bind(this);
this.fetch = this.fetch.bind(this);
}
connect() {
const options = {
Expand All @@ -20,27 +21,13 @@ class default_1 extends Controller {
async signin(event) {
event.preventDefault();
const data = this._getData();
const optionsHeaders = {
'Content-Type': 'application/json',
};
this._dispatchEvent('webauthn:request:options', { data, headers: optionsHeaders });
const resp = await fetch(this.requestOptionsUrlValue || '/request/options', {
method: 'POST',
headers: optionsHeaders,
body: JSON.stringify(data),
});
const respJson = await resp.json();
this._dispatchEvent('webauthn:request:options', { data });
const resp = await this.fetch('POST', this.requestOptionsUrlValue || '/request/options', JSON.stringify(data));
const respJson = await resp.response;
const asseResp = await startAuthentication(respJson);
const responseHeaders = {
'Content-Type': 'application/json',
};
this._dispatchEvent('webauthn:request:response', { response: asseResp, headers: responseHeaders });
const verificationResp = await fetch(this.requestResultUrlValue || '/request', {
method: 'POST',
headers: responseHeaders,
body: JSON.stringify(asseResp),
});
const verificationJSON = await verificationResp.json();
const verificationResp = await this.fetch('POST', this.requestResultUrlValue || '/request', JSON.stringify(asseResp));
const verificationJSON = await verificationResp.response;
this._dispatchEvent('webauthn:request:response', { response: asseResp });
if (verificationJSON && verificationJSON.errorMessage === '') {
this._dispatchEvent('webauthn:request:success', verificationJSON);
if (this.requestSuccessRedirectUriValue) {
Expand All @@ -54,30 +41,16 @@ class default_1 extends Controller {
async signup(event) {
event.preventDefault();
const data = this._getData();
const optionsHeaders = {
'Content-Type': 'application/json',
};
this._dispatchEvent('webauthn:creation:options', { data, headers: optionsHeaders });
const resp = await fetch(this.creationOptionsUrlValue || '/creation/options', {
method: 'POST',
headers: optionsHeaders,
body: JSON.stringify(data),
});
const respJson = await resp.json();
this._dispatchEvent('webauthn:creation:options', { data });
const resp = await this.fetch('POST', this.creationOptionsUrlValue || '/creation/options', JSON.stringify(data));
const respJson = await resp.response;
if (respJson.excludeCredentials === undefined) {
respJson.excludeCredentials = [];
}
const attResp = await startRegistration(respJson);
const responseHeaders = {
'Content-Type': 'application/json',
};
this._dispatchEvent('webauthn:creation:response', { response: attResp, headers: responseHeaders });
const verificationResp = await fetch(this.creationResultUrlValue || '/creation', {
method: 'POST',
headers: responseHeaders,
body: JSON.stringify(attResp),
});
const verificationJSON = await verificationResp.json();
this._dispatchEvent('webauthn:creation:response', { response: attResp });
const verificationResp = await this.fetch('POST', this.creationResultUrlValue || '/creation', JSON.stringify(attResp));
const verificationJSON = await verificationResp.response;
if (verificationJSON && verificationJSON.errorMessage === '') {
this._dispatchEvent('webauthn:creation:success', verificationJSON);
if (this.creationSuccessRedirectUriValue) {
Expand All @@ -91,6 +64,32 @@ class default_1 extends Controller {
_dispatchEvent(name, payload) {
this.element.dispatchEvent(new CustomEvent(name, { detail: payload, bubbles: true }));
}
fetch(method, url, body) {
return new Promise(function (resolve, reject) {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.responseType = "json";
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr);
}
else {
reject({
status: xhr.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: xhr.status,
statusText: xhr.statusText
});
};
xhr.send(body);
});
}
_getData() {
let data = new FormData();
try {
Expand Down
82 changes: 41 additions & 41 deletions src/stimulus/Resources/assets/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class extends Controller {
initialize() {
this._dispatchEvent = this._dispatchEvent.bind(this);
this._getData = this._getData.bind(this);
this.fetch = this.fetch.bind(this);
}

connect() {
Expand All @@ -41,29 +42,16 @@ export default class extends Controller {
async signin(event: Event) {
event.preventDefault();
const data = this._getData();
const optionsHeaders = {
'Content-Type': 'application/json',
};
this._dispatchEvent('webauthn:request:options', {data, headers: optionsHeaders});
const resp = await fetch(this.requestOptionsUrlValue || '/request/options', {
method: 'POST',
headers: optionsHeaders,
body: JSON.stringify(data),
});
const respJson = await resp.json();
const asseResp = await startAuthentication(respJson);

const responseHeaders = {
'Content-Type': 'application/json',
};
this._dispatchEvent('webauthn:request:options', {data});

this._dispatchEvent('webauthn:request:response', {response: asseResp, headers: responseHeaders});
const verificationResp = await fetch(this.requestResultUrlValue || '/request', {
method: 'POST',
headers: responseHeaders,
body: JSON.stringify(asseResp),
});
const verificationJSON = await verificationResp.json();
const resp = await this.fetch('POST', this.requestOptionsUrlValue || '/request/options', JSON.stringify(data));
const respJson = await resp.response;
const asseResp = await startAuthentication(respJson);

const verificationResp = await this.fetch('POST', this.requestResultUrlValue || '/request', JSON.stringify(asseResp));
const verificationJSON = await verificationResp.response;
this._dispatchEvent('webauthn:request:response', {response: asseResp});

if (verificationJSON && verificationJSON.errorMessage === '') {
this._dispatchEvent('webauthn:request:success', verificationJSON);
Expand All @@ -78,32 +66,18 @@ export default class extends Controller {
async signup(event: Event) {
event.preventDefault();
const data = this._getData();
const optionsHeaders = {
'Content-Type': 'application/json',
};
this._dispatchEvent('webauthn:creation:options', {data, headers: optionsHeaders});
const resp = await fetch(this.creationOptionsUrlValue || '/creation/options', {
method: 'POST',
headers: optionsHeaders,
body: JSON.stringify(data),
});
this._dispatchEvent('webauthn:creation:options', {data});
const resp = await this.fetch('POST', this.creationOptionsUrlValue || '/creation/options', JSON.stringify(data));

const respJson = await resp.json();
const respJson = await resp.response;
if (respJson.excludeCredentials === undefined) {
respJson.excludeCredentials = [];
}
const attResp = await startRegistration(respJson);
const responseHeaders = {
'Content-Type': 'application/json',
};
this._dispatchEvent('webauthn:creation:response', {response: attResp, headers: responseHeaders});
const verificationResp = await fetch(this.creationResultUrlValue || '/creation', {
method: 'POST',
headers: responseHeaders,
body: JSON.stringify(attResp),
});
this._dispatchEvent('webauthn:creation:response', {response: attResp});
const verificationResp = await this.fetch('POST', this.creationResultUrlValue || '/creation', JSON.stringify(attResp));

const verificationJSON = await verificationResp.json();
const verificationJSON = await verificationResp.response;
if (verificationJSON && verificationJSON.errorMessage === '') {
this._dispatchEvent('webauthn:creation:success', verificationJSON);
if (this.creationSuccessRedirectUriValue) {
Expand All @@ -118,6 +92,32 @@ export default class extends Controller {
this.element.dispatchEvent(new CustomEvent(name, {detail: payload, bubbles: true}));
}

fetch (method: string, url: string, body: string): Promise<XMLHttpRequest> {
return new Promise(function (resolve, reject) {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.responseType = "json";
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr);
} else {
reject({
status: xhr.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function () {
reject({
status: xhr.status,
statusText: xhr.statusText
});
};
xhr.send(body);
});
}

_getData() {
let data = new FormData();
try {
Expand Down