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
feat: Hanko passkeys support #14741
Open
merlindru
wants to merge
9
commits into
calcom:main
Choose a base branch
from
merlindru:main
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+617
−6
Open
feat: Hanko passkeys support #14741
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eddbd4e
Hanko passkeys support
merlindru df7482e
Passkeys revision round 1
merlindru e1b850a
Update yarn.lock
PeerRich 04253c6
Merge branch 'main' into main
PeerRich a39e63e
Merge branch 'main' into main
PeerRich 1cf9d57
Merge branch 'main' into main
PeerRich d673f69
Merge branch 'main' into main
zomars e4123ad
Merge branch 'main' into main
PeerRich a9bb2d8
Update passkeys.tsx
emrysal File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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,5 @@ | ||
import { WithLayout } from "app/layoutHOC"; | ||
|
||
import { getLayout } from "@calcom/features/settings/layouts/SettingsLayoutAppDir"; | ||
|
||
export default WithLayout({ getLayout }); |
This file contains 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,10 @@ | ||
import Page from "@pages/settings/security/passkeys"; | ||
import { _generateMetadata } from "app/_utils"; | ||
|
||
export const generateMetadata = async () => | ||
await _generateMetadata( | ||
(t) => t("passkeys"), | ||
(t) => t("passkeys_description") | ||
); | ||
|
||
export default Page; |
This file contains 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,26 @@ | ||
import type { ComponentProps } from "react"; | ||
|
||
export default function PasskeyIcon(props: ComponentProps<"svg">) { | ||
return ( | ||
<svg | ||
id="icon-passkey" | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="3 1.5 19.5 19" | ||
width="16" | ||
height="16" | ||
fill="currentColor" | ||
{...props}> | ||
<g id="icon-passkey-all"> | ||
<circle id="icon-passkey-head" cx="10.5" cy="6" r="4.5" /> | ||
<path | ||
id="icon-passkey-key" | ||
d="M22.5,10.5a3.5,3.5,0,1,0-5,3.15V19L19,20.5,21.5,18,20,16.5,21.5,15l-1.24-1.24A3.5,3.5,0,0,0,22.5,10.5Zm-3.5,0a1,1,0,1,1,1-1A1,1,0,0,1,19,10.5Z" | ||
/> | ||
<path | ||
id="icon-passkey-body" | ||
d="M14.44,12.52A6,6,0,0,0,12,12H9a6,6,0,0,0-6,6v2H16V14.49A5.16,5.16,0,0,1,14.44,12.52Z" | ||
/> | ||
</g> | ||
</svg> | ||
); | ||
} |
This file contains 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 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 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,48 @@ | ||
import { tenant } from "@teamhanko/passkeys-next-auth-provider"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode"; | ||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; | ||
import prisma from "@calcom/prisma"; | ||
|
||
const tenantId = process.env.NEXT_PUBLIC_HANKO_PASSKEYS_TENANT_ID ?? ""; | ||
const apiKey = process.env.HANKO_PASSKEYS_API_KEY ?? ""; | ||
|
||
const passkeyApi = tenant({ tenantId, apiKey }); | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (!tenantId || !apiKey) { | ||
return res.status(501).json({ message: "Passkey API not configured" }); | ||
} | ||
|
||
if (req.method !== "GET") { | ||
return res.status(405).json({ message: "Method not allowed" }); | ||
} | ||
|
||
const session = await getServerSession({ req, res }); | ||
if (!session) { | ||
return res.status(401).json({ message: "Not authenticated" }); | ||
} | ||
|
||
if (!session.user?.id) { | ||
console.error("Session is missing a user id."); | ||
return res.status(500).json({ error: ErrorCode.InternalServerError }); | ||
} | ||
|
||
const user = await prisma.user.findUnique({ where: { id: session.user.id } }); | ||
if (!user) { | ||
console.error(`Session references user that no longer exists.`); | ||
return res.status(401).json({ message: "Not authenticated" }); | ||
} | ||
|
||
const credentials = await passkeyApi.user(user.id.toString()).credentials(); | ||
|
||
return res.status(200).json({ | ||
credentials: credentials.map((c) => ({ | ||
id: c.id, | ||
name: c.name, | ||
createdAt: c.created_at, | ||
lastUsedAt: c.last_used_at, | ||
})), | ||
}); | ||
} |
This file contains 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,41 @@ | ||
import { tenant } from "@teamhanko/passkeys-next-auth-provider"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode"; | ||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; | ||
import prisma from "@calcom/prisma"; | ||
|
||
const tenantId = process.env.NEXT_PUBLIC_HANKO_PASSKEYS_TENANT_ID ?? ""; | ||
const apiKey = process.env.HANKO_PASSKEYS_API_KEY ?? ""; | ||
|
||
const passkeyApi = tenant({ tenantId, apiKey }); | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (!tenantId || !apiKey) { | ||
return res.status(501).json({ message: "Passkey API not configured" }); | ||
} | ||
|
||
if (req.method !== "POST") { | ||
return res.status(405).json({ message: "Method not allowed" }); | ||
} | ||
|
||
const session = await getServerSession({ req, res }); | ||
if (!session) { | ||
return res.status(401).json({ message: "Not authenticated" }); | ||
} | ||
|
||
if (!session.user?.id) { | ||
console.error("Session is missing a user id."); | ||
return res.status(500).json({ error: ErrorCode.InternalServerError }); | ||
} | ||
|
||
const user = await prisma.user.findUnique({ where: { id: session.user.id } }); | ||
if (!user) { | ||
console.error(`Session references user that no longer exists.`); | ||
return res.status(401).json({ message: "Not authenticated" }); | ||
} | ||
|
||
await passkeyApi.registration.finalize(req.body); | ||
|
||
return res.status(200).json({ message: "Passkey registered" }); | ||
} |
This file contains 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,44 @@ | ||
import { tenant } from "@teamhanko/passkeys-next-auth-provider"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode"; | ||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; | ||
import prisma from "@calcom/prisma"; | ||
|
||
const tenantId = process.env.NEXT_PUBLIC_HANKO_PASSKEYS_TENANT_ID ?? ""; | ||
const apiKey = process.env.HANKO_PASSKEYS_API_KEY ?? ""; | ||
|
||
const passkeyApi = tenant({ tenantId, apiKey }); | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (!tenantId || !apiKey) { | ||
return res.status(501).json({ message: "Passkey API not configured" }); | ||
} | ||
|
||
if (req.method !== "POST") { | ||
return res.status(405).json({ message: "Method not allowed" }); | ||
} | ||
|
||
const session = await getServerSession({ req, res }); | ||
if (!session) { | ||
return res.status(401).json({ message: "Not authenticated" }); | ||
} | ||
|
||
if (!session.user?.id) { | ||
console.error("Session is missing a user id."); | ||
return res.status(500).json({ error: ErrorCode.InternalServerError }); | ||
} | ||
|
||
const user = await prisma.user.findUnique({ where: { id: session.user.id } }); | ||
if (!user) { | ||
console.error(`Session references user that no longer exists.`); | ||
return res.status(401).json({ message: "Not authenticated" }); | ||
} | ||
|
||
const createOptions = await passkeyApi.registration.initialize({ | ||
userId: session.user.id.toString(), | ||
username: session.user.username ?? "", | ||
}); | ||
|
||
return res.status(200).json(createOptions); | ||
} |
This file contains 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,46 @@ | ||
import { tenant } from "@teamhanko/passkeys-next-auth-provider"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import { ErrorCode } from "@calcom/features/auth/lib/ErrorCode"; | ||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; | ||
import prisma from "@calcom/prisma"; | ||
|
||
const tenantId = process.env.NEXT_PUBLIC_HANKO_PASSKEYS_TENANT_ID ?? ""; | ||
const apiKey = process.env.HANKO_PASSKEYS_API_KEY ?? ""; | ||
|
||
const passkeyApi = tenant({ tenantId, apiKey }); | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (!tenantId || !apiKey) { | ||
return res.status(501).json({ message: "Passkey API not configured" }); | ||
} | ||
|
||
if (req.method !== "DELETE") { | ||
return res.status(405).json({ message: "Method not allowed" }); | ||
} | ||
|
||
const credentialId = req.body?.credentialId; | ||
if (!credentialId) { | ||
return res.status(400).json({ message: "Missing credential id" }); | ||
} | ||
|
||
const session = await getServerSession({ req, res }); | ||
if (!session) { | ||
return res.status(401).json({ message: "Not authenticated" }); | ||
} | ||
|
||
if (!session.user?.id) { | ||
console.error("Session is missing a user id."); | ||
return res.status(500).json({ error: ErrorCode.InternalServerError }); | ||
} | ||
|
||
const user = await prisma.user.findUnique({ where: { id: session.user.id } }); | ||
if (!user) { | ||
console.error(`Session references user that no longer exists.`); | ||
return res.status(401).json({ message: "Not authenticated" }); | ||
} | ||
|
||
await passkeyApi.credential(credentialId).remove(); | ||
|
||
return res.status(200).json({}); | ||
} |
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish Node had a better way to make optional dependencies 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure since when npm has this, but there is optionalDependencies
https://docs.npmjs.com/cli/v10/configuring-npm/package-json#optionaldependencies
Was supported in yarn v1 so I assume v2+ has it too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opted not to do this for now because optionalDependencies isn't in use for any other packages atm. Also I'm unsure if a build without installing optional deps would be successful, especially because of
import
behavior, haven't tested itDo you want me to test this? What other not-strictly-needed dependencies are there that could be moved to
optionalDependencies
(at least in theory)?