|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "code.gitea.io/gitea/models/db" |
| 7 | + user_model "code.gitea.io/gitea/models/user" |
| 8 | + "code.gitea.io/gitea/modules/context" |
| 9 | + "code.gitea.io/gitea/modules/convert" |
| 10 | + "code.gitea.io/gitea/modules/log" |
| 11 | + "code.gitea.io/gitea/modules/password" |
| 12 | + "code.gitea.io/gitea/modules/setting" |
| 13 | + "code.gitea.io/gitea/modules/web" |
| 14 | + "code.gitea.io/gitea/services/auth" |
| 15 | + "code.gitea.io/gitea/services/forms" |
| 16 | + "code.gitea.io/gitea/services/mailer" |
| 17 | +) |
| 18 | + |
| 19 | +// KitspaceSignUp custom sign-up compatible with Kitspace architecture |
| 20 | +func KitspaceSignUp(ctx *context.Context) { |
| 21 | + // swagger:operation POST /user/kitspace/sign_up |
| 22 | + // --- |
| 23 | + // summary: Create a user |
| 24 | + // consumes: |
| 25 | + // - application/json |
| 26 | + // produces: |
| 27 | + // - application/json |
| 28 | + // parameters: |
| 29 | + // - name: body |
| 30 | + // in: body |
| 31 | + // schema: |
| 32 | + // "$ref": "#/definitions/RegisterForm" |
| 33 | + // responses: |
| 34 | + // "201": |
| 35 | + // "$ref": "#/responses/User" |
| 36 | + // "400": |
| 37 | + // "$ref": "#/responses/error" |
| 38 | + // "409": |
| 39 | + // "$ref": "#/response/error |
| 40 | + // "422": |
| 41 | + // "$ref": "#/responses/validationError" |
| 42 | + response := make(map[string]interface{}) |
| 43 | + form := web.GetForm(ctx).(*forms.RegisterForm) |
| 44 | + |
| 45 | + if len(form.Password) < setting.MinPasswordLength { |
| 46 | + response["error"] = "UnprocessableEntity" |
| 47 | + response["message"] = "Password is too short." |
| 48 | + |
| 49 | + ctx.JSON(http.StatusUnprocessableEntity, response) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + if !password.IsComplexEnough(form.Password) { |
| 54 | + response["error"] = "UnprocessableEntity" |
| 55 | + response["message"] = "Password isn't complex enough." |
| 56 | + |
| 57 | + ctx.JSON(http.StatusUnprocessableEntity, response) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + u := &user_model.User{ |
| 62 | + Name: form.UserName, |
| 63 | + Email: form.Email, |
| 64 | + Passwd: form.Password, |
| 65 | + IsActive: !setting.Service.RegisterEmailConfirm, |
| 66 | + } |
| 67 | + |
| 68 | + if err := user_model.CreateUser(u); err != nil { |
| 69 | + switch { |
| 70 | + case user_model.IsErrUserAlreadyExist(err): |
| 71 | + response["error"] = "Conflict" |
| 72 | + response["message"] = "User already exists." |
| 73 | + |
| 74 | + ctx.JSON(http.StatusConflict, response) |
| 75 | + case user_model.IsErrEmailAlreadyUsed(err): |
| 76 | + response["error"] = "Conflict" |
| 77 | + response["message"] = "Email is already used." |
| 78 | + |
| 79 | + ctx.JSON(http.StatusConflict, response) |
| 80 | + case db.IsErrNameReserved(err): |
| 81 | + response["error"] = "Conflict" |
| 82 | + response["message"] = "Name is reserved." |
| 83 | + |
| 84 | + ctx.JSON(http.StatusConflict, response) |
| 85 | + case db.IsErrNamePatternNotAllowed(err): |
| 86 | + response["error"] = "UnprocessableEntity" |
| 87 | + response["message"] = "This name pattern isn't allowed." |
| 88 | + |
| 89 | + ctx.JSON(http.StatusUnprocessableEntity, response) |
| 90 | + default: |
| 91 | + ctx.ServerError("Signup", err) |
| 92 | + } |
| 93 | + return |
| 94 | + } else { |
| 95 | + log.Trace("Account created: %s", u.Name) |
| 96 | + } |
| 97 | + |
| 98 | + // Send confirmation email |
| 99 | + // The mailing service works only in production during development no mails are sent |
| 100 | + if setting.Service.RegisterEmailConfirm && u.ID > 1 { |
| 101 | + mailer.SendActivateAccountMail(ctx.Locale, u) |
| 102 | + |
| 103 | + if err := ctx.Cache.Put("MailResendLimit_"+u.LowerName, u.LowerName, 180); err != nil { |
| 104 | + log.Error("Set cache(MailResendLimit) fail: %v", err) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + handleSignInFull(ctx, u, true, false) |
| 109 | + |
| 110 | + // Return the success response with user details |
| 111 | + response["user"] = convert.ToUser(u, u) |
| 112 | + |
| 113 | + ctx.JSON(http.StatusCreated, response) |
| 114 | +} |
| 115 | + |
| 116 | +// KitspaceSignIn custom sign-in compatible with Kitspace architecture |
| 117 | +func KitspaceSignIn(ctx *context.Context) { |
| 118 | + // swagger:operation POST /user/kitspace/sign_in |
| 119 | + // --- |
| 120 | + // summary: login a user |
| 121 | + // consumes: |
| 122 | + // - application/json |
| 123 | + // produces: |
| 124 | + // - application/json |
| 125 | + // parameters: |
| 126 | + // - name: body |
| 127 | + // in: body |
| 128 | + // schema: |
| 129 | + // "$ref": "#/definitions/SignInForm" |
| 130 | + // responses: |
| 131 | + // "200": |
| 132 | + // "$ref": "success" |
| 133 | + // "404": |
| 134 | + // "$ref": "#/response/forbidden" |
| 135 | + // "404": |
| 136 | + // "$ref": "#/responses/notFound" |
| 137 | + // "409": |
| 138 | + // "$ref": "#/response/error |
| 139 | + // "422": |
| 140 | + // "$ref": "#/responses/validationError" |
| 141 | + |
| 142 | + form := web.GetForm(ctx).(*forms.SignInForm) |
| 143 | + u, _, err := auth.UserSignIn(form.UserName, form.Password) |
| 144 | + |
| 145 | + response := make(map[string]interface{}) |
| 146 | + if err != nil { |
| 147 | + switch { |
| 148 | + case user_model.IsErrUserNotExist(err): |
| 149 | + response["error"] = "Not Found" |
| 150 | + response["message"] = "Wrong username or password." |
| 151 | + |
| 152 | + ctx.JSON(http.StatusNotFound, response) |
| 153 | + log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr()) |
| 154 | + case user_model.IsErrEmailAlreadyUsed(err): |
| 155 | + response["error"] = "Conflict" |
| 156 | + response["message"] = "This email has already been used." |
| 157 | + |
| 158 | + ctx.JSON(http.StatusConflict, response) |
| 159 | + log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr()) |
| 160 | + case user_model.IsErrUserProhibitLogin(err): |
| 161 | + response["error"] = "Prohibited" |
| 162 | + response["message"] = "Prohibited login." |
| 163 | + |
| 164 | + ctx.JSON(http.StatusForbidden, response) |
| 165 | + log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr()) |
| 166 | + case user_model.IsErrUserInactive(err): |
| 167 | + if setting.Service.RegisterEmailConfirm { |
| 168 | + response["error"] = "ActivationRequired" |
| 169 | + response["message"] = "Activate your account." |
| 170 | + |
| 171 | + ctx.JSON(http.StatusOK, response) |
| 172 | + } else { |
| 173 | + response["error"] = "Prohibited" |
| 174 | + response["message"] = "Prohibited login" |
| 175 | + |
| 176 | + ctx.JSON(http.StatusForbidden, response) |
| 177 | + log.Info("Failed authentication attempt for %s from %s", form.UserName, ctx.RemoteAddr()) |
| 178 | + } |
| 179 | + default: |
| 180 | + ctx.ServerError("KitspaceSignIn", err) |
| 181 | + } |
| 182 | + return |
| 183 | + } |
| 184 | + handleSignInFull(ctx, u, form.Remember, false) |
| 185 | + |
| 186 | + response["user"] = convert.ToUser(u, u) |
| 187 | + |
| 188 | + ctx.JSON(http.StatusOK, response) |
| 189 | +} |
0 commit comments