2fa.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package user
  2. import (
  3. "encoding/base64"
  4. "net/http"
  5. "strings"
  6. "time"
  7. "github.com/0xJacky/Nginx-UI/api"
  8. "github.com/0xJacky/Nginx-UI/internal/cache"
  9. "github.com/0xJacky/Nginx-UI/internal/passkey"
  10. "github.com/0xJacky/Nginx-UI/internal/user"
  11. "github.com/0xJacky/Nginx-UI/model"
  12. "github.com/0xJacky/Nginx-UI/query"
  13. "github.com/gin-gonic/gin"
  14. "github.com/go-webauthn/webauthn/webauthn"
  15. "github.com/google/uuid"
  16. "github.com/uozi-tech/cosy"
  17. )
  18. type Status2FA struct {
  19. Enabled bool `json:"enabled"`
  20. OTPStatus bool `json:"otp_status"`
  21. PasskeyStatus bool `json:"passkey_status"`
  22. RecoveryCodesGenerated bool `json:"recovery_codes_generated"`
  23. RecoveryCodesViewed bool `json:"recovery_codes_viewed"`
  24. }
  25. func get2FAStatus(c *gin.Context) (status Status2FA) {
  26. // when accessing the node from the main cluster, there is no user in the context
  27. u, ok := c.Get("user")
  28. if ok {
  29. userPtr := u.(*model.User)
  30. status.OTPStatus = userPtr.EnabledOTP()
  31. status.PasskeyStatus = userPtr.EnabledPasskey() && passkey.Enabled()
  32. status.Enabled = status.OTPStatus || status.PasskeyStatus
  33. status.RecoveryCodesGenerated = userPtr.RecoveryCodeGenerated()
  34. status.RecoveryCodesViewed = userPtr.RecoveryCodeViewed()
  35. }
  36. return
  37. }
  38. func Get2FAStatus(c *gin.Context) {
  39. c.JSON(http.StatusOK, get2FAStatus(c))
  40. }
  41. func SecureSessionStatus(c *gin.Context) {
  42. status2FA := get2FAStatus(c)
  43. if !status2FA.Enabled {
  44. c.JSON(http.StatusOK, gin.H{
  45. "status": false,
  46. })
  47. return
  48. }
  49. ssid := c.GetHeader("X-Secure-Session-ID")
  50. if ssid == "" {
  51. ssid = c.Query("X-Secure-Session-ID")
  52. }
  53. if ssid == "" {
  54. c.JSON(http.StatusOK, gin.H{
  55. "status": false,
  56. })
  57. return
  58. }
  59. u := api.CurrentUser(c)
  60. c.JSON(http.StatusOK, gin.H{
  61. "status": user.VerifySecureSessionID(ssid, u.ID),
  62. })
  63. }
  64. func Start2FASecureSessionByOTP(c *gin.Context) {
  65. var json struct {
  66. OTP string `json:"otp"`
  67. RecoveryCode string `json:"recovery_code"`
  68. }
  69. if !cosy.BindAndValid(c, &json) {
  70. return
  71. }
  72. u := api.CurrentUser(c)
  73. if !u.EnabledOTP() {
  74. api.ErrHandler(c, user.ErrUserNotEnabledOTPAs2FA)
  75. return
  76. }
  77. if json.OTP == "" && json.RecoveryCode == "" {
  78. api.ErrHandler(c, user.ErrOTPOrRecoveryCodeEmpty)
  79. return
  80. }
  81. if err := user.VerifyOTP(u, json.OTP, json.RecoveryCode); err != nil {
  82. api.ErrHandler(c, err)
  83. return
  84. }
  85. sessionId := user.SetSecureSessionID(u.ID)
  86. c.JSON(http.StatusOK, gin.H{
  87. "session_id": sessionId,
  88. })
  89. }
  90. func BeginStart2FASecureSessionByPasskey(c *gin.Context) {
  91. if !passkey.Enabled() {
  92. api.ErrHandler(c, user.ErrWebAuthnNotConfigured)
  93. return
  94. }
  95. webauthnInstance := passkey.GetInstance()
  96. u := api.CurrentUser(c)
  97. options, sessionData, err := webauthnInstance.BeginLogin(u)
  98. if err != nil {
  99. api.ErrHandler(c, err)
  100. return
  101. }
  102. passkeySessionID := uuid.NewString()
  103. cache.Set(passkeySessionID, sessionData, passkeyTimeout)
  104. c.JSON(http.StatusOK, gin.H{
  105. "session_id": passkeySessionID,
  106. "options": options,
  107. })
  108. }
  109. func FinishStart2FASecureSessionByPasskey(c *gin.Context) {
  110. if !passkey.Enabled() {
  111. api.ErrHandler(c, user.ErrWebAuthnNotConfigured)
  112. return
  113. }
  114. passkeySessionID := c.GetHeader("X-Passkey-Session-ID")
  115. sessionDataBytes, ok := cache.Get(passkeySessionID)
  116. if !ok {
  117. api.ErrHandler(c, user.ErrSessionNotFound)
  118. return
  119. }
  120. sessionData := sessionDataBytes.(*webauthn.SessionData)
  121. webauthnInstance := passkey.GetInstance()
  122. u := api.CurrentUser(c)
  123. credential, err := webauthnInstance.FinishLogin(u, *sessionData, c.Request)
  124. if err != nil {
  125. api.ErrHandler(c, err)
  126. return
  127. }
  128. rawID := strings.TrimRight(base64.StdEncoding.EncodeToString(credential.ID), "=")
  129. p := query.Passkey
  130. _, _ = p.Where(p.RawID.Eq(rawID)).Updates(&model.Passkey{
  131. LastUsedAt: time.Now().Unix(),
  132. })
  133. sessionId := user.SetSecureSessionID(u.ID)
  134. c.JSON(http.StatusOK, gin.H{
  135. "session_id": sessionId,
  136. })
  137. }