Web Authentication API는 웹 애플리케이션이 사용자를 인증하기 위해 WebAuthn이라는 강력한 공개 키 암호화 기반 인증 자격 증명을 사용할 수 있도록 하는 JavaScript API입니다. WebAuthn은 기존의 사용자 이름/암호 인증을 생체 인식(예: 지문 또는 안면 인식) 및 하드웨어 보안 키(예: YubiKey)와 같은 보다 안전하고 사용자 친화적인 인증 방법으로 대체하도록 설계되었습니다.
다음은 웹 인증 API를 사용하여 하드웨어 보안 키를 사용하여 사용자를 등록하고 인증하는 방법의 예입니다.
// Check if the Web Authentication API is supported
if (!window.PublicKeyCredential) {
console.log('WebAuthn not supported!');
}
// Register a new user with a hardware security key
async function registerUser() {
const publicKeyCredential = await navigator.credentials.create({
publicKey: {
rp: {
name: 'My Web App'
},
user: {
id: new Uint8Array(16),
name: 'user@example.com',
displayName: 'User'
},
pubKeyCredParams: [
{ type: 'public-key', alg: -7 },
{ type: 'public-key', alg: -257 }
],
timeout: 60000,
attestation: 'direct'
}
});
console.log('Registration successful:', publicKeyCredential);
}
// Authenticate a registered user with a hardware security key
async function authenticateUser() {
const publicKeyCredential = await navigator.credentials.get({
publicKey: {
rpId: 'mywebapp.com',
userVerification: 'required',
allowCredentials: [
{
type: 'public-key',
id: new Uint8Array(16),
transports: ['usb']
}
],
timeout: 60000
}
});
console.log('Authentication successful:', publicKeyCredential);
}
이 예제에서 `registerUser` 함수는 `navigator.credentials.create()` 메서드를 사용하여 새 `PublicKeyCredential` 객체를 생성하여 새 사용자를 등록합니다. `PublicKeyCredentialCreationOptions` 개체의 `publicKey` 속성은 RP(신뢰 당사자) 이름, 사용자 식별 정보, 지원되는 공개 키 알고리즘 및 증명 기본 설정을 포함하여 새 자격 증명에 대한 매개 변수를 지정합니다.
`authenticateUser` 함수는 `navigator.credentials.get()` 메서드를 사용하여 사용자의 자격 증명을 검색하여 등록된 사용자를 인증합니다. `PublicKeyCredentialRequestOptions` 개체의 `publicKey` 속성은 RP 식별자, 사용자 확인 요구 사항, 지원되는 자격 증명 및 시간 초과를 포함하여 인증 요청에 대한 매개 변수를 지정합니다.
Web Authentication API는 모든 브라우저에서 지원되지 않으므로 지원되지 않는 브라우저에 대한 대체 인증 방법을 포함해야 합니다. 또한 중간자 공격을 방지하기 위해 API에 보안 HTTPS 연결이 필요합니다.
'IT' 카테고리의 다른 글
자바스크립트 "Web Animations API" (0) | 2023.03.10 |
---|---|
자바스크립트 "Web Audio API" (0) | 2023.03.10 |
자바스크립트 "Web Crypto API" (0) | 2023.03.10 |
자바스크립트 "Web Locks API" (0) | 2023.03.10 |
자바스크립트 "Web MIDI API" (0) | 2023.03.09 |