Skip to content

Commit

Permalink
webauthn sign-in client
Browse files Browse the repository at this point in the history
Signed-off-by: Omri Bornstein <omribor@gmail.com>
  • Loading branch information
AppleGamer22 committed Feb 11, 2024
1 parent 2a93269 commit 469deb0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 19 deletions.
4 changes: 2 additions & 2 deletions server/configuration/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (server *RakerServer) InstagramSignUp(writer http.ResponseWriter, request *
server.InstagramSignIn(writer, request)
}

func (server *RakerServer) WebAuthnBeginRegistration(writer http.ResponseWriter, request *http.Request) {
func (server *RakerServer) WebAuthnBeginSignUp(writer http.ResponseWriter, request *http.Request) {
user := request.Context().Value(authenticatedUserKey).(db.User)
options, session, err := server.WebAuthn.BeginRegistration(user)
if err != nil {
Expand Down Expand Up @@ -142,7 +142,7 @@ func (server *RakerServer) WebAuthnBeginRegistration(writer http.ResponseWriter,
writer.WriteHeader(http.StatusOK)
}

func (server *RakerServer) WebAuthnFinishRegistration(writer http.ResponseWriter, request *http.Request) {
func (server *RakerServer) WebAuthnFinishSignUp(writer http.ResponseWriter, request *http.Request) {
user := request.Context().Value(authenticatedUserKey).(db.User)

credential, err := server.WebAuthn.FinishRegistration(user, user.Session, request)
Expand Down
76 changes: 59 additions & 17 deletions templates/authentication.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,49 @@
</style>
{{if .Username}}
<script>
async function checkWebAuthn() {
if (window.PublicKeyCredential && PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable && PublicKeyCredential.​​isConditionalMediationAvailable) {
try {
const results = await Promise.all([
PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable(),
PublicKeyCredential.isConditionalMediationAvailable()
]);
return results.every(result => result === true);
} catch (err) {
console.error(err);
alert(err);
return false;
}
/**
* @param {boolean} signUp
*/
async function checkWebAuthn(signUp) {
switch (signUp) {
case true:
if (window.PublicKeyCredential && PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable && PublicKeyCredential.isConditionalMediationAvailable) {
try {
const results = await Promise.all([
PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable(),
PublicKeyCredential.isConditionalMediationAvailable()
]);
return results.every(result => result === true);
} catch (err) {
console.error(err);
alert(err);
return false;
}
}
break;
default:
if (window.PublicKeyCredential && PublicKeyCredential.isConditionalMediationAvailable) {
return await PublicKeyCredential.isConditionalMediationAvailable();
}
break;
}
return false;
}

async function createWebAuthn() {
try {
if (!(await checkWebAuthn())) {
if (!(await checkWebAuthn(true))) {
alert("Platform doesn't support Passkeys...");
return;
}
/** @type {PublicKeyCredentialCreationOptions} */
const publicKey = await (await fetch("/auth/webauthn/register/start")).json();
const credential = await navigator.credentials.create({publicKey});
await fetch("/auth/webauthn/register/finish", {
const publicKey = await (await fetch("/auth/webauthn/sign_up/start")).json();
const abortController = new AbortController();
const credential = await navigator.credentials.create({
publicKey,
signal: abortController.signal
});
await fetch("/auth/webauthn/sign_up/finish", {
headers: {
"Content-Type": "application/json",
},
Expand All @@ -76,6 +93,31 @@
alert(err);
}
}

async function sendWebAuthn() {
try {
if (!(await checkWebAuthn(false))) {
alert("Platform doesn't support Passkeys...");
return;
}
const publicKey = await (await fetch("/auth/webauthn/sign_in/start")).json();
const abortController = new AbortController();
const credential = await navigator.credentials.get({
publicKey,
signal: abortController.signal,
mediation: "conditional"
});
await fetch("/auth/webauthn/sign_in/finish", {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(credential)
});
} catch (error) {
console.error(err);
alert(err);
}
}
</script>
{{end}}
</head>
Expand Down

0 comments on commit 469deb0

Please sign in to comment.