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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 validates 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 validates 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 validates 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 validates 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 validates 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 validates 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. KeepActivityPrivate bool
  176. }
  177. // Validate validates the fields
  178. func (f *UpdateProfileForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  179. return validate(errs, ctx.Data, f, ctx.Locale)
  180. }
  181. // Avatar types
  182. const (
  183. AvatarLocal string = "local"
  184. AvatarByMail string = "bymail"
  185. )
  186. // AvatarForm form for changing avatar
  187. type AvatarForm struct {
  188. Source string
  189. Avatar *multipart.FileHeader
  190. Gravatar string `binding:"OmitEmpty;Email;MaxSize(254)"`
  191. Federavatar bool
  192. }
  193. // Validate validates the fields
  194. func (f *AvatarForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  195. return validate(errs, ctx.Data, f, ctx.Locale)
  196. }
  197. // AddEmailForm form for adding new email
  198. type AddEmailForm struct {
  199. Email string `binding:"Required;Email;MaxSize(254)"`
  200. }
  201. // Validate validates the fields
  202. func (f *AddEmailForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  203. return validate(errs, ctx.Data, f, ctx.Locale)
  204. }
  205. // UpdateThemeForm form for updating a users' theme
  206. type UpdateThemeForm struct {
  207. Theme string `binding:"Required;MaxSize(30)"`
  208. }
  209. // Validate validates the field
  210. func (f *UpdateThemeForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  211. return validate(errs, ctx.Data, f, ctx.Locale)
  212. }
  213. // IsThemeExists checks if the theme is a theme available in the config.
  214. func (f UpdateThemeForm) IsThemeExists() bool {
  215. var exists bool
  216. for _, v := range setting.UI.Themes {
  217. if strings.EqualFold(v, f.Theme) {
  218. exists = true
  219. break
  220. }
  221. }
  222. return exists
  223. }
  224. // ChangePasswordForm form for changing password
  225. type ChangePasswordForm struct {
  226. OldPassword string `form:"old_password" binding:"MaxSize(255)"`
  227. Password string `form:"password" binding:"Required;MaxSize(255)"`
  228. Retype string `form:"retype"`
  229. }
  230. // Validate validates the fields
  231. func (f *ChangePasswordForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  232. return validate(errs, ctx.Data, f, ctx.Locale)
  233. }
  234. // AddOpenIDForm is for changing openid uri
  235. type AddOpenIDForm struct {
  236. Openid string `binding:"Required;MaxSize(256)"`
  237. }
  238. // Validate validates the fields
  239. func (f *AddOpenIDForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  240. return validate(errs, ctx.Data, f, ctx.Locale)
  241. }
  242. // AddKeyForm form for adding SSH/GPG key
  243. type AddKeyForm struct {
  244. Type string `binding:"OmitEmpty"`
  245. Title string `binding:"Required;MaxSize(50)"`
  246. Content string `binding:"Required"`
  247. IsWritable bool
  248. }
  249. // Validate validates the fields
  250. func (f *AddKeyForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  251. return validate(errs, ctx.Data, f, ctx.Locale)
  252. }
  253. // NewAccessTokenForm form for creating access token
  254. type NewAccessTokenForm struct {
  255. Name string `binding:"Required;MaxSize(255)"`
  256. }
  257. // Validate validates the fields
  258. func (f *NewAccessTokenForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  259. return validate(errs, ctx.Data, f, ctx.Locale)
  260. }
  261. // EditOAuth2ApplicationForm form for editing oauth2 applications
  262. type EditOAuth2ApplicationForm struct {
  263. Name string `binding:"Required;MaxSize(255)" form:"application_name"`
  264. RedirectURI string `binding:"Required" form:"redirect_uri"`
  265. }
  266. // Validate validates the fields
  267. func (f *EditOAuth2ApplicationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  268. return validate(errs, ctx.Data, f, ctx.Locale)
  269. }
  270. // TwoFactorAuthForm for logging in with 2FA token.
  271. type TwoFactorAuthForm struct {
  272. Passcode string `binding:"Required"`
  273. }
  274. // Validate validates the fields
  275. func (f *TwoFactorAuthForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  276. return validate(errs, ctx.Data, f, ctx.Locale)
  277. }
  278. // TwoFactorScratchAuthForm for logging in with 2FA scratch token.
  279. type TwoFactorScratchAuthForm struct {
  280. Token string `binding:"Required"`
  281. }
  282. // Validate validates the fields
  283. func (f *TwoFactorScratchAuthForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  284. return validate(errs, ctx.Data, f, ctx.Locale)
  285. }
  286. // U2FRegistrationForm for reserving an U2F name
  287. type U2FRegistrationForm struct {
  288. Name string `binding:"Required"`
  289. }
  290. // Validate validates the fields
  291. func (f *U2FRegistrationForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  292. return validate(errs, ctx.Data, f, ctx.Locale)
  293. }
  294. // U2FDeleteForm for deleting U2F keys
  295. type U2FDeleteForm struct {
  296. ID int64 `binding:"Required"`
  297. }
  298. // Validate validates the fields
  299. func (f *U2FDeleteForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  300. return validate(errs, ctx.Data, f, ctx.Locale)
  301. }