Can enroll keys

This commit is contained in:
2021-05-14 10:59:30 +02:00
parent 163ff8471a
commit 9516190084
5 changed files with 333 additions and 2 deletions

View File

@ -4,6 +4,10 @@
* @author Pierre Hubert
*/
import {
ArrayBufferToBase64,
base64NoPaddingToUint8Array,
} from "../utils/Base64Utils";
import { serverRequest } from "./APIHelper";
export interface AuthOptions {
@ -133,4 +137,49 @@ export class AccountHelper {
email: s.email,
});
}
/**
* First step of access key enrollment
*/
static async GetKeyRegistrationChallenge(): Promise<any> {
const res = await serverRequest("accounts/challenge_register_key");
res.publicKey.challenge = base64NoPaddingToUint8Array(
res.publicKey.challenge
);
res.publicKey.user.id = base64NoPaddingToUint8Array(
res.publicKey.user.id
);
return res;
}
/**
* Register key
*
* @param name The name of the key to create
* @param cred The credentials to register
*/
static async RegisterKey(name: string, cred: any): Promise<void> {
const res = {
id: cred.id,
rawId: ArrayBufferToBase64(cred.rawId),
type: cred.type,
response: {
attestationObject: ArrayBufferToBase64(
cred.response.attestationObject
),
clientDataJSON: ArrayBufferToBase64(
cred.response.clientDataJSON
),
},
};
console.info(cred);
console.info(res);
await serverRequest("accounts/register_key", {
name: name,
key: JSON.stringify(res),
});
}
}