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.

user_form.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package auth
  6. import (
  7. "mime/multipart"
  8. "strings"
  9. "code.gitea.io/gitea/modules/setting"
  10. "gitea.com/macaron/binding"
  11. "gitea.com/macaron/macaron"
  12. )
  13. // InstallForm form for installation page
  14. type InstallForm struct {
  15. DbType string `binding:"Required"`
  16. DbHost string
  17. DbUser string
  18. DbPasswd string
  19. DbName string
  20. SSLMode string
  21. Charset string `binding:"Required;In(utf8,utf8mb4)"`
  22. DbPath string
  23. DbSchema string
  24. AppName string `binding:"Required" locale:"install.app_name"`
  25. RepoRootPath string `binding:"Required"`
  26. LFSRootPath string
  27. RunUser string `binding:"Required"`
  28. Domain string `binding:"Required"`
  29. SSHPort int
  30. HTTPPort string `binding:"Required"`
  31. AppURL string `binding:"Required"`
  32. LogRootPath string `binding:"Required"`
  33. SMTPHost string
  34. SMTPFrom string
  35. SMTPUser string `binding:"OmitEmpty;MaxSize(254)" locale:"install.mailer_user"`
  36. SMTPPasswd string
  37. RegisterConfirm bool
  38. MailNotify bool
  39. OfflineMode bool
  40. DisableGravatar bool
  41. EnableFederatedAvatar bool
  42. EnableOpenIDSignIn bool
  43. EnableOpenIDSignUp bool
  44. DisableRegistration bool
  45. AllowOnlyExternalRegistration bool
  46. EnableCaptcha bool
  47. RequireSignInView bool
  48. DefaultKeepEmailPrivate bool
  49. DefaultAllowCreateOrganization bool
  50. DefaultEnableTimetracking bool
  51. NoReplyAddress string
  52. AdminName string `binding:"OmitEmpty;AlphaDashDot;MaxSize(30)" locale:"install.admin_name"`
  53. AdminPasswd string `binding:"OmitEmpty;MaxSize(255)" locale:"install.admin_password"`
  54. AdminConfirmPasswd string
  55. AdminEmail string `binding:"OmitEmpty;MinSize(3);MaxSize(254);Include(@)" locale:"install.admin_email"`
  56. }
  57. // Validate validates the fields
  58. func (f *InstallForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  59. return validate(errs, ctx.Data, f, ctx.Locale)
  60. }
  61. // _____ ____ _________________ ___
  62. // / _ \ | | \__ ___/ | \
  63. // / /_\ \| | / | | / ~ \
  64. // / | \ | / | | \ Y /
  65. // \____|__ /______/ |____| \___|_ /
  66. // \/ \/
  67. // RegisterForm form for registering
  68. type RegisterForm struct {
  69. UserName string `binding:"Required;AlphaDashDot;MaxSize(40)"`
  70. Email string `binding:"Required;Email;MaxSize(254)"`
  71. Password string `binding:"MaxSize(255)"`
  72. Retype string
  73. GRecaptchaResponse string `form:"g-recaptcha-response"`
  74. }
  75. // Validate valideates the fields
  76. func (f *RegisterForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  77. return validate(errs, ctx.Data, f, ctx.Locale)
  78. }
  79. // IsEmailDomainWhitelisted validates that the email address
  80. // provided by the user matches what has been configured .
  81. // If the domain whitelist from the config is empty, it marks the
  82. // email as whitelisted
  83. func (f RegisterForm) IsEmailDomainWhitelisted() bool {
  84. if len(setting.Service.EmailDomainWhitelist) == 0 {
  85. return true
  86. }
  87. n := strings.LastIndex(f.Email, "@")
  88. if n <= 0 {
  89. return false
  90. }
  91. domain := strings.ToLower(f.Email[n+1:])
  92. for _, v := range setting.Service.EmailDomainWhitelist {
  93. if strings.ToLower(v) == domain {
  94. return true
  95. }
  96. }
  97. return false
  98. }
  99. // MustChangePasswordForm form for updating your password after account creation
  100. // by an admin
  101. type MustChangePasswordForm struct {
  102. Password string `binding:"Required;MaxSize(255)"`
  103. Retype string
  104. }
  105. // Validate valideates the fields
  106. func (f *MustChangePasswordForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  107. return validate(errs, ctx.Data, f, ctx.Locale)
  108. }
  109. // SignInForm form for signing in with user/password
  110. type SignInForm struct {
  111. UserName string `binding:"Required;MaxSize(254)"`
  112. // TODO remove required from password for SecondFactorAuthentication
  113. Password string `binding:"Required;MaxSize(255)"`
  114. Remember bool
  115. }
  116. // Validate valideates the fields
  117. func (f *SignInForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  118. return validate(errs, ctx.Data, f, ctx.Locale)
  119. }
  120. // AuthorizationForm form for authorizing oauth2 clients
  121. type AuthorizationForm struct {
  122. ResponseType string `binding:"Required;In(code)"`
  123. ClientID string `binding:"Required"`
  124. RedirectURI string
  125. State string
  126. // PKCE support
  127. CodeChallengeMethod string // S256, plain
  128. CodeChallenge string
  129. }
  130. // Validate valideates the fields
  131. func (f *AuthorizationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  132. return validate(errs, ctx.Data, f, ctx.Locale)
  133. }
  134. // GrantApplicationForm form for authorizing oauth2 clients
  135. type GrantApplicationForm struct {
  136. ClientID string `binding:"Required"`
  137. RedirectURI string
  138. State string
  139. }
  140. // Validate valideates the fields
  141. func (f *GrantApplicationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  142. return validate(errs, ctx.Data, f, ctx.Locale)
  143. }
  144. // AccessTokenForm for issuing access tokens from authorization codes or refresh tokens
  145. type AccessTokenForm struct {
  146. GrantType string `json:"grant_type"`
  147. ClientID string `json:"client_id"`
  148. ClientSecret string `json:"client_secret"`
  149. RedirectURI string `json:"redirect_uri"`
  150. Code string `json:"code"`
  151. RefreshToken string `json:"refresh_token"`
  152. // PKCE support
  153. CodeVerifier string `json:"code_verifier"`
  154. }
  155. // Validate valideates the fields
  156. func (f *AccessTokenForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  157. return validate(errs, ctx.Data, f, ctx.Locale)
  158. }
  159. // __________________________________________.___ _______ ________ _________
  160. // / _____/\_ _____/\__ ___/\__ ___/| |\ \ / _____/ / _____/
  161. // \_____ \ | __)_ | | | | | |/ | \/ \ ___ \_____ \
  162. // / \ | \ | | | | | / | \ \_\ \/ \
  163. // /_______ //_______ / |____| |____| |___\____|__ /\______ /_______ /
  164. // \/ \/ \/ \/ \/
  165. // UpdateProfileForm form for updating profile
  166. type UpdateProfileForm struct {
  167. Name string `binding:"AlphaDashDot;MaxSize(40)"`
  168. FullName string `binding:"MaxSize(100)"`
  169. Email string `binding:"Required;Email;MaxSize(254)"`
  170. KeepEmailPrivate bool
  171. Website string `binding:"ValidUrl;MaxSize(255)"`
  172. Location string `binding:"MaxSize(50)"`
  173. Language string `binding:"Size(5)"`
  174. Description string `binding:"MaxSize(255)"`
  175. }
  176. // Validate validates the fields
  177. func (f *UpdateProfileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  178. return validate(errs, ctx.Data, f, ctx.Locale)
  179. }
  180. // Avatar types
  181. const (
  182. AvatarLocal string = "local"
  183. AvatarByMail string = "bymail"
  184. )
  185. // AvatarForm form for changing avatar
  186. type AvatarForm struct {
  187. Source string
  188. Avatar *multipart.FileHeader
  189. Gravatar string `binding:"OmitEmpty;Email;MaxSize(254)"`
  190. Federavatar bool
  191. }
  192. // Validate validates the fields
  193. func (f *AvatarForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  194. return validate(errs, ctx.Data, f, ctx.Locale)
  195. }
  196. // AddEmailForm form for adding new email
  197. type AddEmailForm struct {
  198. Email string `binding:"Required;Email;MaxSize(254)"`
  199. }
  200. // Validate validates the fields
  201. func (f *AddEmailForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  202. return validate(errs, ctx.Data, f, ctx.Locale)
  203. }
  204. // UpdateThemeForm form for updating a users' theme
  205. type UpdateThemeForm struct {
  206. Theme string `binding:"Required;MaxSize(30)"`
  207. }
  208. // Validate validates the field
  209. func (f *UpdateThemeForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  210. return validate(errs, ctx.Data, f, ctx.Locale)
  211. }
  212. // IsThemeExists checks if the theme is a theme available in the config.
  213. func (f UpdateThemeForm) IsThemeExists() bool {
  214. var exists bool
  215. for _, v := range setting.UI.Themes {
  216. if strings.EqualFold(v, f.Theme) {
  217. exists = true
  218. break
  219. }
  220. }
  221. return exists
  222. }
  223. // ChangePasswordForm form for changing password
  224. type ChangePasswordForm struct {
  225. OldPassword string `form:"old_password" binding:"MaxSize(255)"`
  226. Password string `form:"password" binding:"Required;MaxSize(255)"`
  227. Retype string `form:"retype"`
  228. }
  229. // Validate validates the fields
  230. func (f *ChangePasswordForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  231. return validate(errs, ctx.Data, f, ctx.Locale)
  232. }
  233. // AddOpenIDForm is for changing openid uri
  234. type AddOpenIDForm struct {
  235. Openid string `binding:"Required;MaxSize(256)"`
  236. }
  237. // Validate validates the fields
  238. func (f *AddOpenIDForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  239. return validate(errs, ctx.Data, f, ctx.Locale)
  240. }
  241. // AddKeyForm form for adding SSH/GPG key
  242. type AddKeyForm struct {
  243. Type string `binding:"OmitEmpty"`
  244. Title string `binding:"Required;MaxSize(50)"`
  245. Content string `binding:"Required"`
  246. IsWritable bool
  247. }
  248. // Validate validates the fields
  249. func (f *AddKeyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  250. return validate(errs, ctx.Data, f, ctx.Locale)
  251. }
  252. // NewAccessTokenForm form for creating access token
  253. type NewAccessTokenForm struct {
  254. Name string `binding:"Required;MaxSize(255)"`
  255. }
  256. // Validate valideates the fields
  257. func (f *NewAccessTokenForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  258. return validate(errs, ctx.Data, f, ctx.Locale)
  259. }
  260. // EditOAuth2ApplicationForm form for editing oauth2 applications
  261. type EditOAuth2ApplicationForm struct {
  262. Name string `binding:"Required;MaxSize(255)" form:"application_name"`
  263. RedirectURI string `binding:"Required" form:"redirect_uri"`
  264. }
  265. // Validate valideates the fields
  266. func (f *EditOAuth2ApplicationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  267. return validate(errs, ctx.Data, f, ctx.Locale)
  268. }
  269. // TwoFactorAuthForm for logging in with 2FA token.
  270. type TwoFactorAuthForm struct {
  271. Passcode string `binding:"Required"`
  272. }
  273. // Validate validates the fields
  274. func (f *TwoFactorAuthForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  275. return validate(errs, ctx.Data, f, ctx.Locale)
  276. }
  277. // TwoFactorScratchAuthForm for logging in with 2FA scratch token.
  278. type TwoFactorScratchAuthForm struct {
  279. Token string `binding:"Required"`
  280. }
  281. // Validate valideates the fields
  282. func (f *TwoFactorScratchAuthForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  283. return validate(errs, ctx.Data, f, ctx.Locale)
  284. }
  285. // U2FRegistrationForm for reserving an U2F name
  286. type U2FRegistrationForm struct {
  287. Name string `binding:"Required"`
  288. }
  289. // Validate valideates the fields
  290. func (f *U2FRegistrationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  291. return validate(errs, ctx.Data, f, ctx.Locale)
  292. }
  293. // U2FDeleteForm for deleting U2F keys
  294. type U2FDeleteForm struct {
  295. ID int64 `binding:"Required"`
  296. }
  297. // Validate valideates the fields
  298. func (f *U2FDeleteForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  299. return validate(errs, ctx.Data, f, ctx.Locale)
  300. }