-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Fix U2F registration and signing by replacing jQuery requests by fetch #11311
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
const {AppSubUrl, csrf} = window.config; | ||
|
||
export async function initU2FAuth() { | ||
if ($('#wait-for-key').length === 0) { | ||
return; | ||
} | ||
try { | ||
await u2fApi.ensureSupport(); | ||
} catch (e) { | ||
// Fallback in case browser do not support U2F | ||
window.location.href = `${AppSubUrl}/user/two_factor`; | ||
} | ||
try { | ||
const response = await fetch(`${AppSubUrl}/user/u2f/challenge`); | ||
if (!response.ok) throw new Error('cannot retrieve challenge'); | ||
const {appId, challenge, registeredKeys} = await response.json(); | ||
const signature = await u2fApi.sign(appId, challenge, registeredKeys, 30); | ||
u2fSigned(signature); | ||
} catch (e) { | ||
if (e === undefined || e.metaData === undefined) { | ||
u2fError(1); | ||
return; | ||
} | ||
u2fError(e.metaData.code); | ||
} | ||
} | ||
|
||
function u2fSigned(resp) { | ||
$.ajax({ | ||
url: `${AppSubUrl}/user/u2f/sign`, | ||
type: 'POST', | ||
headers: {'X-Csrf-Token': csrf}, | ||
data: JSON.stringify(resp), | ||
contentType: 'application/json; charset=utf-8', | ||
}).done((res) => { | ||
window.location.replace(res); | ||
}).fail(() => { | ||
u2fError(1); | ||
}); | ||
} | ||
|
||
function u2fRegistered(resp) { | ||
if (checkError(resp)) { | ||
return; | ||
} | ||
$.ajax({ | ||
url: `${AppSubUrl}/user/settings/security/u2f/register`, | ||
type: 'POST', | ||
headers: {'X-Csrf-Token': csrf}, | ||
data: JSON.stringify(resp), | ||
contentType: 'application/json; charset=utf-8', | ||
success() { | ||
window.location.reload(); | ||
}, | ||
fail() { | ||
u2fError(1); | ||
} | ||
}); | ||
} | ||
|
||
function checkError(resp) { | ||
if (!('errorCode' in resp)) { | ||
return false; | ||
} | ||
if (resp.errorCode === 0) { | ||
return false; | ||
} | ||
u2fError(resp.errorCode); | ||
return true; | ||
} | ||
|
||
function u2fError(errorType) { | ||
const u2fErrors = { | ||
browser: $('#unsupported-browser'), | ||
1: $('#u2f-error-1'), | ||
2: $('#u2f-error-2'), | ||
3: $('#u2f-error-3'), | ||
4: $('#u2f-error-4'), | ||
5: $('.u2f-error-5') | ||
}; | ||
u2fErrors[errorType].removeClass('hide'); | ||
|
||
Object.keys(u2fErrors).forEach((type) => { | ||
if (type !== errorType) { | ||
u2fErrors[type].addClass('hide'); | ||
} | ||
}); | ||
$('#u2f-error').modal('show'); | ||
} | ||
|
||
export function initU2FRegister() { | ||
$('#register-device').modal({allowMultiple: false}); | ||
$('#u2f-error').modal({allowMultiple: false}); | ||
$('#register-security-key').on('click', (e) => { | ||
e.preventDefault(); | ||
u2fApi.ensureSupport() | ||
.then(u2fRegisterRequest) | ||
.catch(() => { | ||
u2fError('browser'); | ||
}); | ||
}); | ||
} | ||
|
||
async function u2fRegisterRequest() { | ||
const body = new FormData(); | ||
body.append('_csrf', csrf); | ||
body.append('name', $('#nickname').val()); | ||
const response = await fetch(`${AppSubUrl}/user/settings/security/u2f/request_register`, { | ||
method: 'POST', | ||
body, | ||
}); | ||
if (!response.ok) { | ||
if (response.status === 409) { | ||
$('#nickname').closest('div.field').addClass('error'); | ||
return; | ||
} | ||
throw new Error('request register failed'); | ||
} | ||
let {appId, registerRequests, registeredKeys} = await response.json(); | ||
$('#nickname').closest('div.field').removeClass('error'); | ||
$('#register-device').modal('show'); | ||
if (registeredKeys === null) { | ||
registeredKeys = []; | ||
} | ||
u2fApi.register(appId, registerRequests, registeredKeys, 30) | ||
.then(u2fRegistered) | ||
.catch((reason) => { | ||
if (reason === undefined) { | ||
u2fError(1); | ||
return; | ||
} | ||
u2fError(reason.metaData.code); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.