ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

otp.ts 1.0 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Secret, TOTP } from 'otpauth'
  2. import { CONFIG } from '@server/initializers/config.js'
  3. import { WEBSERVER } from '@server/initializers/constants.js'
  4. import { decrypt } from './peertube-crypto.js'
  5. async function isOTPValid (options: {
  6. encryptedSecret: string
  7. token: string
  8. }) {
  9. const { token, encryptedSecret } = options
  10. const secret = await decrypt(encryptedSecret, CONFIG.SECRETS.PEERTUBE)
  11. const totp = new TOTP({
  12. ...baseOTPOptions(),
  13. secret
  14. })
  15. const delta = totp.validate({
  16. token,
  17. window: 1
  18. })
  19. if (delta === null) return false
  20. return true
  21. }
  22. function generateOTPSecret (email: string) {
  23. const totp = new TOTP({
  24. ...baseOTPOptions(),
  25. label: email,
  26. secret: new Secret()
  27. })
  28. return {
  29. secret: totp.secret.base32,
  30. uri: totp.toString()
  31. }
  32. }
  33. export {
  34. isOTPValid,
  35. generateOTPSecret
  36. }
  37. // ---------------------------------------------------------------------------
  38. function baseOTPOptions () {
  39. return {
  40. issuer: WEBSERVER.HOST,
  41. algorithm: 'SHA1',
  42. digits: 6,
  43. period: 30
  44. }
  45. }