|
| 1 | +package user |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha1" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/hex" |
| 8 | + "fmt" |
| 9 | + "github.com/0xJacky/Nginx-UI/api" |
| 10 | + "github.com/0xJacky/Nginx-UI/internal/crypto" |
| 11 | + "github.com/0xJacky/Nginx-UI/internal/user" |
| 12 | + "github.com/0xJacky/Nginx-UI/query" |
| 13 | + "github.com/0xJacky/Nginx-UI/settings" |
| 14 | + "github.com/gin-gonic/gin" |
| 15 | + "github.com/pquerna/otp" |
| 16 | + "github.com/pquerna/otp/totp" |
| 17 | + "image/jpeg" |
| 18 | + "net/http" |
| 19 | + "strings" |
| 20 | +) |
| 21 | + |
| 22 | +func GenerateTOTP(c *gin.Context) { |
| 23 | + u := api.CurrentUser(c) |
| 24 | + |
| 25 | + issuer := fmt.Sprintf("Nginx UI %s", settings.ServerSettings.Name) |
| 26 | + issuer = strings.TrimSpace(issuer) |
| 27 | + |
| 28 | + otpOpts := totp.GenerateOpts{ |
| 29 | + Issuer: issuer, |
| 30 | + AccountName: u.Name, |
| 31 | + Period: 30, // seconds |
| 32 | + Digits: otp.DigitsSix, |
| 33 | + Algorithm: otp.AlgorithmSHA1, |
| 34 | + } |
| 35 | + otpKey, err := totp.Generate(otpOpts) |
| 36 | + if err != nil { |
| 37 | + api.ErrHandler(c, err) |
| 38 | + return |
| 39 | + } |
| 40 | + ciphertext, err := crypto.AesEncrypt([]byte(otpKey.Secret())) |
| 41 | + if err != nil { |
| 42 | + api.ErrHandler(c, err) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + qrCode, err := otpKey.Image(512, 512) |
| 47 | + if err != nil { |
| 48 | + api.ErrHandler(c, err) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + // Encode the image to a buffer |
| 53 | + var buf []byte |
| 54 | + buffer := bytes.NewBuffer(buf) |
| 55 | + err = jpeg.Encode(buffer, qrCode, nil) |
| 56 | + if err != nil { |
| 57 | + fmt.Println("Error encoding image:", err) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + // Convert the buffer to a base64 string |
| 62 | + base64Str := "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(buffer.Bytes()) |
| 63 | + |
| 64 | + c.JSON(http.StatusOK, gin.H{ |
| 65 | + "secret": base64.StdEncoding.EncodeToString(ciphertext), |
| 66 | + "qr_code": base64Str, |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func EnrollTOTP(c *gin.Context) { |
| 71 | + cUser := api.CurrentUser(c) |
| 72 | + if cUser.EnabledOTP() { |
| 73 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 74 | + "message": "User already enrolled", |
| 75 | + }) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + if settings.ServerSettings.Demo { |
| 80 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 81 | + "message": "This feature is disabled in demo mode", |
| 82 | + }) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + var json struct { |
| 87 | + Secret string `json:"secret" binding:"required"` |
| 88 | + Passcode string `json:"passcode" binding:"required"` |
| 89 | + } |
| 90 | + if !api.BindAndValid(c, &json) { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + secret, err := base64.StdEncoding.DecodeString(json.Secret) |
| 95 | + if err != nil { |
| 96 | + api.ErrHandler(c, err) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + decrypted, err := crypto.AesDecrypt(secret) |
| 101 | + if err != nil { |
| 102 | + api.ErrHandler(c, err) |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + if ok := totp.Validate(json.Passcode, string(decrypted)); !ok { |
| 107 | + c.JSON(http.StatusNotAcceptable, gin.H{ |
| 108 | + "message": "Invalid passcode", |
| 109 | + }) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + ciphertext, err := crypto.AesEncrypt(decrypted) |
| 114 | + if err != nil { |
| 115 | + api.ErrHandler(c, err) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + u := query.Auth |
| 120 | + _, err = u.Where(u.ID.Eq(cUser.ID)).Update(u.OTPSecret, ciphertext) |
| 121 | + if err != nil { |
| 122 | + api.ErrHandler(c, err) |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + recoveryCode := sha1.Sum(ciphertext) |
| 127 | + |
| 128 | + c.JSON(http.StatusOK, gin.H{ |
| 129 | + "message": "ok", |
| 130 | + "recovery_code": hex.EncodeToString(recoveryCode[:]), |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +func ResetOTP(c *gin.Context) { |
| 135 | + var json struct { |
| 136 | + RecoveryCode string `json:"recovery_code"` |
| 137 | + } |
| 138 | + if !api.BindAndValid(c, &json) { |
| 139 | + return |
| 140 | + } |
| 141 | + recoverCode, err := hex.DecodeString(json.RecoveryCode) |
| 142 | + if err != nil { |
| 143 | + api.ErrHandler(c, err) |
| 144 | + return |
| 145 | + } |
| 146 | + cUser := api.CurrentUser(c) |
| 147 | + k := sha1.Sum(cUser.OTPSecret) |
| 148 | + if !bytes.Equal(k[:], recoverCode) { |
| 149 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 150 | + "message": "Invalid recovery code", |
| 151 | + }) |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + u := query.Auth |
| 156 | + _, err = u.Where(u.ID.Eq(cUser.ID)).UpdateSimple(u.OTPSecret.Null()) |
| 157 | + if err != nil { |
| 158 | + api.ErrHandler(c, err) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + c.JSON(http.StatusOK, gin.H{ |
| 163 | + "message": "ok", |
| 164 | + }) |
| 165 | +} |
| 166 | + |
| 167 | +func OTPStatus(c *gin.Context) { |
| 168 | + c.JSON(http.StatusOK, gin.H{ |
| 169 | + "status": len(api.CurrentUser(c).OTPSecret) > 0, |
| 170 | + }) |
| 171 | +} |
| 172 | + |
| 173 | +func StartSecure2FASession(c *gin.Context) { |
| 174 | + var json struct { |
| 175 | + OTP string `json:"otp"` |
| 176 | + RecoveryCode string `json:"recovery_code"` |
| 177 | + } |
| 178 | + if !api.BindAndValid(c, &json) { |
| 179 | + return |
| 180 | + } |
| 181 | + u := api.CurrentUser(c) |
| 182 | + if !u.EnabledOTP() { |
| 183 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 184 | + "message": "User not configured with 2FA", |
| 185 | + }) |
| 186 | + return |
| 187 | + } |
| 188 | + |
| 189 | + if json.OTP == "" && json.RecoveryCode == "" { |
| 190 | + c.JSON(http.StatusBadRequest, LoginResponse{ |
| 191 | + Message: "The user has enabled 2FA", |
| 192 | + }) |
| 193 | + return |
| 194 | + } |
| 195 | + |
| 196 | + if err := user.VerifyOTP(u, json.OTP, json.RecoveryCode); err != nil { |
| 197 | + c.JSON(http.StatusBadRequest, LoginResponse{ |
| 198 | + Message: "Invalid 2FA or recovery code", |
| 199 | + }) |
| 200 | + return |
| 201 | + } |
| 202 | + |
| 203 | + sessionId := user.SetSecureSessionID(u.ID) |
| 204 | + |
| 205 | + c.JSON(http.StatusOK, gin.H{ |
| 206 | + "session_id": sessionId, |
| 207 | + }) |
| 208 | +} |
0 commit comments