|
| 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/query" |
| 12 | + "github.com/0xJacky/Nginx-UI/settings" |
| 13 | + "github.com/gin-gonic/gin" |
| 14 | + "github.com/pquerna/otp" |
| 15 | + "github.com/pquerna/otp/totp" |
| 16 | + "image/jpeg" |
| 17 | + "net/http" |
| 18 | + "strings" |
| 19 | +) |
| 20 | + |
| 21 | +func GenerateTOTP(c *gin.Context) { |
| 22 | + user := api.CurrentUser(c) |
| 23 | + |
| 24 | + issuer := fmt.Sprintf("Nginx UI %s", settings.ServerSettings.Name) |
| 25 | + issuer = strings.TrimSpace(issuer) |
| 26 | + |
| 27 | + otpOpts := totp.GenerateOpts{ |
| 28 | + Issuer: issuer, |
| 29 | + AccountName: user.Name, |
| 30 | + Period: 30, // seconds |
| 31 | + Digits: otp.DigitsSix, |
| 32 | + Algorithm: otp.AlgorithmSHA1, |
| 33 | + } |
| 34 | + otpKey, err := totp.Generate(otpOpts) |
| 35 | + if err != nil { |
| 36 | + api.ErrHandler(c, err) |
| 37 | + return |
| 38 | + } |
| 39 | + ciphertext, err := crypto.AesEncrypt([]byte(otpKey.Secret())) |
| 40 | + if err != nil { |
| 41 | + api.ErrHandler(c, err) |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + qrCode, err := otpKey.Image(512, 512) |
| 46 | + if err != nil { |
| 47 | + api.ErrHandler(c, err) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + // Encode the image to a buffer |
| 52 | + var buf []byte |
| 53 | + buffer := bytes.NewBuffer(buf) |
| 54 | + err = jpeg.Encode(buffer, qrCode, nil) |
| 55 | + if err != nil { |
| 56 | + fmt.Println("Error encoding image:", err) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + // Convert the buffer to a base64 string |
| 61 | + base64Str := "data:image/jpeg;base64," + base64.StdEncoding.EncodeToString(buffer.Bytes()) |
| 62 | + |
| 63 | + c.JSON(http.StatusOK, gin.H{ |
| 64 | + "secret": base64.StdEncoding.EncodeToString(ciphertext), |
| 65 | + "qr_code": base64Str, |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +func EnrollTOTP(c *gin.Context) { |
| 70 | + user := api.CurrentUser(c) |
| 71 | + if len(user.OTPSecret) > 0 { |
| 72 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 73 | + "message": "User already enrolled", |
| 74 | + }) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + var json struct { |
| 79 | + Secret string `json:"secret" binding:"required"` |
| 80 | + Passcode string `json:"passcode" binding:"required"` |
| 81 | + } |
| 82 | + if !api.BindAndValid(c, &json) { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + secret, err := base64.StdEncoding.DecodeString(json.Secret) |
| 87 | + if err != nil { |
| 88 | + api.ErrHandler(c, err) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + decrypted, err := crypto.AesDecrypt(secret) |
| 93 | + if err != nil { |
| 94 | + api.ErrHandler(c, err) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + if ok := totp.Validate(json.Passcode, string(decrypted)); !ok { |
| 99 | + c.JSON(http.StatusNotAcceptable, gin.H{ |
| 100 | + "message": "Invalid passcode", |
| 101 | + }) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + ciphertext, err := crypto.AesEncrypt(decrypted) |
| 106 | + if err != nil { |
| 107 | + api.ErrHandler(c, err) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + u := query.Auth |
| 112 | + _, err = u.Where(u.ID.Eq(user.ID)).Update(u.OTPSecret, ciphertext) |
| 113 | + if err != nil { |
| 114 | + api.ErrHandler(c, err) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + recoveryCode := sha1.Sum(ciphertext) |
| 119 | + |
| 120 | + c.JSON(http.StatusOK, gin.H{ |
| 121 | + "message": "ok", |
| 122 | + "recovery_code": hex.EncodeToString(recoveryCode[:]), |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +func ResetOTP(c *gin.Context) { |
| 127 | + var json struct { |
| 128 | + RecoveryCode string `json:"recovery_code"` |
| 129 | + } |
| 130 | + if !api.BindAndValid(c, &json) { |
| 131 | + return |
| 132 | + } |
| 133 | + recoverCode, err := hex.DecodeString(json.RecoveryCode) |
| 134 | + if err != nil { |
| 135 | + api.ErrHandler(c, err) |
| 136 | + return |
| 137 | + } |
| 138 | + user := api.CurrentUser(c) |
| 139 | + k := sha1.Sum(user.OTPSecret) |
| 140 | + if !bytes.Equal(k[:], recoverCode) { |
| 141 | + c.JSON(http.StatusBadRequest, gin.H{ |
| 142 | + "message": "Invalid recovery code", |
| 143 | + }) |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + u := query.Auth |
| 148 | + _, err = u.Where(u.ID.Eq(user.ID)).UpdateSimple(u.OTPSecret.Null()) |
| 149 | + if err != nil { |
| 150 | + api.ErrHandler(c, err) |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + c.JSON(http.StatusOK, gin.H{ |
| 155 | + "message": "ok", |
| 156 | + }) |
| 157 | +} |
| 158 | + |
| 159 | +func OTPStatus(c *gin.Context) { |
| 160 | + c.JSON(http.StatusOK, gin.H{ |
| 161 | + "status": len(api.CurrentUser(c).OTPSecret) > 0, |
| 162 | + }) |
| 163 | +} |
0 commit comments