security-keys.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { TwoFactorConfigurationPlugin } from '#shared/entities/two-factor/types.ts'
  3. import { EnumTwoFactorAuthenticationMethod } from '#shared/graphql/types.ts'
  4. import TwoFactorConfigurationSecurityKeys from '#desktop/components/TwoFactor/TwoFactorConfiguration/TwoFactorConfigurationSecurityKeys.vue'
  5. import type { CredentialCreationOptionsJSON } from '@github/webauthn-json'
  6. export default {
  7. name: EnumTwoFactorAuthenticationMethod.SecurityKeys,
  8. editable: true,
  9. component: TwoFactorConfigurationSecurityKeys,
  10. actionButtonA11yLabel: __('Action menu button for security keys'),
  11. getActionA11yLabel(type) {
  12. switch (type) {
  13. case 'setup':
  14. return __('Set up security keys')
  15. case 'default':
  16. return __('Set security keys as default')
  17. case 'edit':
  18. return __('Edit security keys')
  19. case 'remove':
  20. return __('Remove security keys')
  21. default:
  22. return ''
  23. }
  24. },
  25. async setup(
  26. publicKey: NonNullable<CredentialCreationOptionsJSON['publicKey']>,
  27. ) {
  28. if (!window.isSecureContext) {
  29. return {
  30. success: false,
  31. retry: false,
  32. error: __('The application is not running in a secure context.'),
  33. }
  34. }
  35. try {
  36. const { create } = await import('@github/webauthn-json')
  37. const publicKeyCredential = await create({ publicKey })
  38. return {
  39. success: true,
  40. payload: {
  41. challenge: publicKey.challenge,
  42. credential: publicKeyCredential,
  43. },
  44. }
  45. } catch {
  46. return {
  47. success: false,
  48. retry: true,
  49. error: __('Security key setup failed.'),
  50. }
  51. }
  52. },
  53. } satisfies TwoFactorConfigurationPlugin