gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package forms
  5. import (
  6. "mime/multipart"
  7. "net/http"
  8. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/modules/web/middleware"
  11. "code.gitea.io/gitea/services/context"
  12. "gitea.com/go-chi/binding"
  13. )
  14. // InstallForm form for installation page
  15. type InstallForm struct {
  16. DbType string `binding:"Required"`
  17. DbHost string
  18. DbUser string
  19. DbPasswd string
  20. DbName string
  21. SSLMode string
  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. SMTPAddr string
  34. SMTPPort string
  35. SMTPFrom string
  36. SMTPUser string `binding:"OmitEmpty;MaxSize(254)" locale:"install.mailer_user"`
  37. SMTPPasswd string
  38. RegisterConfirm bool
  39. MailNotify bool
  40. OfflineMode bool
  41. DisableGravatar bool
  42. EnableFederatedAvatar bool
  43. EnableOpenIDSignIn bool
  44. EnableOpenIDSignUp bool
  45. DisableRegistration bool
  46. AllowOnlyExternalRegistration bool
  47. EnableCaptcha bool
  48. RequireSignInView bool
  49. DefaultKeepEmailPrivate bool
  50. DefaultAllowCreateOrganization bool
  51. DefaultEnableTimetracking bool
  52. EnableUpdateChecker bool
  53. NoReplyAddress string
  54. PasswordAlgorithm string
  55. AdminName string `binding:"OmitEmpty;Username;MaxSize(30)" locale:"install.admin_name"`
  56. AdminPasswd string `binding:"OmitEmpty;MaxSize(255)" locale:"install.admin_password"`
  57. AdminConfirmPasswd string
  58. AdminEmail string `binding:"OmitEmpty;MinSize(3);MaxSize(254);Include(@)" locale:"install.admin_email"`
  59. // ReinstallConfirmFirst we can not use 1/2/3 or A/B/C here, there is a framework bug, can not parse "reinstall_confirm_1" or "reinstall_confirm_a"
  60. ReinstallConfirmFirst bool
  61. ReinstallConfirmSecond bool
  62. ReinstallConfirmThird bool
  63. }
  64. // Validate validates the fields
  65. func (f *InstallForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  66. ctx := context.GetValidateContext(req)
  67. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  68. }
  69. // _____ ____ _________________ ___
  70. // / _ \ | | \__ ___/ | \
  71. // / /_\ \| | / | | / ~ \
  72. // / | \ | / | | \ Y /
  73. // \____|__ /______/ |____| \___|_ /
  74. // \/ \/
  75. // RegisterForm form for registering
  76. type RegisterForm struct {
  77. UserName string `binding:"Required;Username;MaxSize(40)"`
  78. Email string `binding:"Required;MaxSize(254)"`
  79. Password string `binding:"MaxSize(255)"`
  80. Retype string
  81. }
  82. // Validate validates the fields
  83. func (f *RegisterForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  84. ctx := context.GetValidateContext(req)
  85. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  86. }
  87. // IsEmailDomainAllowed validates that the email address
  88. // provided by the user matches what has been configured .
  89. // The email is marked as allowed if it matches any of the
  90. // domains in the whitelist or if it doesn't match any of
  91. // domains in the blocklist, if any such list is not empty.
  92. func (f *RegisterForm) IsEmailDomainAllowed() bool {
  93. return user_model.IsEmailDomainAllowed(f.Email)
  94. }
  95. // MustChangePasswordForm form for updating your password after account creation
  96. // by an admin
  97. type MustChangePasswordForm struct {
  98. Password string `binding:"Required;MaxSize(255)"`
  99. Retype string
  100. }
  101. // Validate validates the fields
  102. func (f *MustChangePasswordForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  103. ctx := context.GetValidateContext(req)
  104. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  105. }
  106. // SignInForm form for signing in with user/password
  107. type SignInForm struct {
  108. UserName string `binding:"Required;MaxSize(254)"`
  109. // TODO remove required from password for SecondFactorAuthentication
  110. Password string `binding:"Required;MaxSize(255)"`
  111. Remember bool
  112. }
  113. // Validate validates the fields
  114. func (f *SignInForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  115. ctx := context.GetValidateContext(req)
  116. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  117. }
  118. // AuthorizationForm form for authorizing oauth2 clients
  119. type AuthorizationForm struct {
  120. ResponseType string `binding:"Required;In(code)"`
  121. ClientID string `binding:"Required"`
  122. RedirectURI string
  123. State string
  124. Scope string
  125. Nonce string
  126. // PKCE support
  127. CodeChallengeMethod string // S256, plain
  128. CodeChallenge string
  129. }
  130. // Validate validates the fields
  131. func (f *AuthorizationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  132. ctx := context.GetValidateContext(req)
  133. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  134. }
  135. // GrantApplicationForm form for authorizing oauth2 clients
  136. type GrantApplicationForm struct {
  137. ClientID string `binding:"Required"`
  138. Granted bool
  139. RedirectURI string
  140. State string
  141. Scope string
  142. Nonce string
  143. }
  144. // Validate validates the fields
  145. func (f *GrantApplicationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  146. ctx := context.GetValidateContext(req)
  147. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  148. }
  149. // AccessTokenForm for issuing access tokens from authorization codes or refresh tokens
  150. type AccessTokenForm struct {
  151. GrantType string `json:"grant_type"`
  152. ClientID string `json:"client_id"`
  153. ClientSecret string `json:"client_secret"`
  154. RedirectURI string `json:"redirect_uri"`
  155. Code string `json:"code"`
  156. RefreshToken string `json:"refresh_token"`
  157. // PKCE support
  158. CodeVerifier string `json:"code_verifier"`
  159. }
  160. // Validate validates the fields
  161. func (f *AccessTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  162. ctx := context.GetValidateContext(req)
  163. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  164. }
  165. // IntrospectTokenForm for introspecting tokens
  166. type IntrospectTokenForm struct {
  167. Token string `json:"token"`
  168. }
  169. // Validate validates the fields
  170. func (f *IntrospectTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  171. ctx := context.GetValidateContext(req)
  172. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  173. }
  174. // __________________________________________.___ _______ ________ _________
  175. // / _____/\_ _____/\__ ___/\__ ___/| |\ \ / _____/ / _____/
  176. // \_____ \ | __)_ | | | | | |/ | \/ \ ___ \_____ \
  177. // / \ | \ | | | | | / | \ \_\ \/ \
  178. // /_______ //_______ / |____| |____| |___\____|__ /\______ /_______ /
  179. // \/ \/ \/ \/ \/
  180. // UpdateProfileForm form for updating profile
  181. type UpdateProfileForm struct {
  182. Name string `binding:"Username;MaxSize(40)"`
  183. FullName string `binding:"MaxSize(100)"`
  184. KeepEmailPrivate bool
  185. Website string `binding:"ValidSiteUrl;MaxSize(255)"`
  186. Location string `binding:"MaxSize(50)"`
  187. Description string `binding:"MaxSize(255)"`
  188. Visibility structs.VisibleType
  189. KeepActivityPrivate bool
  190. }
  191. // Validate validates the fields
  192. func (f *UpdateProfileForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  193. ctx := context.GetValidateContext(req)
  194. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  195. }
  196. // UpdateLanguageForm form for updating profile
  197. type UpdateLanguageForm struct {
  198. Language string
  199. }
  200. // Validate validates the fields
  201. func (f *UpdateLanguageForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  202. ctx := context.GetValidateContext(req)
  203. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  204. }
  205. // Avatar types
  206. const (
  207. AvatarLocal string = "local"
  208. AvatarByMail string = "bymail"
  209. )
  210. // AvatarForm form for changing avatar
  211. type AvatarForm struct {
  212. Source string
  213. Avatar *multipart.FileHeader
  214. Gravatar string `binding:"OmitEmpty;Email;MaxSize(254)"`
  215. Federavatar bool
  216. }
  217. // Validate validates the fields
  218. func (f *AvatarForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  219. ctx := context.GetValidateContext(req)
  220. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  221. }
  222. // AddEmailForm form for adding new email
  223. type AddEmailForm struct {
  224. Email string `binding:"Required;Email;MaxSize(254)"`
  225. }
  226. // Validate validates the fields
  227. func (f *AddEmailForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  228. ctx := context.GetValidateContext(req)
  229. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  230. }
  231. // UpdateThemeForm form for updating a users' theme
  232. type UpdateThemeForm struct {
  233. Theme string `binding:"Required;MaxSize(255)"`
  234. }
  235. // Validate validates the field
  236. func (f *UpdateThemeForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  237. ctx := context.GetValidateContext(req)
  238. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  239. }
  240. // ChangePasswordForm form for changing password
  241. type ChangePasswordForm struct {
  242. OldPassword string `form:"old_password" binding:"MaxSize(255)"`
  243. Password string `form:"password" binding:"Required;MaxSize(255)"`
  244. Retype string `form:"retype"`
  245. }
  246. // Validate validates the fields
  247. func (f *ChangePasswordForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  248. ctx := context.GetValidateContext(req)
  249. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  250. }
  251. // AddOpenIDForm is for changing openid uri
  252. type AddOpenIDForm struct {
  253. Openid string `binding:"Required;MaxSize(256)"`
  254. }
  255. // Validate validates the fields
  256. func (f *AddOpenIDForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  257. ctx := context.GetValidateContext(req)
  258. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  259. }
  260. // AddKeyForm form for adding SSH/GPG key
  261. type AddKeyForm struct {
  262. Type string `binding:"OmitEmpty"`
  263. Title string `binding:"Required;MaxSize(50)"`
  264. Content string `binding:"Required"`
  265. Signature string `binding:"OmitEmpty"`
  266. KeyID string `binding:"OmitEmpty"`
  267. Fingerprint string `binding:"OmitEmpty"`
  268. IsWritable bool
  269. }
  270. // Validate validates the fields
  271. func (f *AddKeyForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  272. ctx := context.GetValidateContext(req)
  273. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  274. }
  275. // AddSecretForm for adding secrets
  276. type AddSecretForm struct {
  277. Name string `binding:"Required;MaxSize(255)"`
  278. Data string `binding:"Required;MaxSize(65535)"`
  279. Description string `binding:"MaxSize(65535)"`
  280. }
  281. // Validate validates the fields
  282. func (f *AddSecretForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  283. ctx := context.GetValidateContext(req)
  284. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  285. }
  286. type EditVariableForm struct {
  287. Name string `binding:"Required;MaxSize(255)"`
  288. Data string `binding:"Required;MaxSize(65535)"`
  289. Description string `binding:"MaxSize(65535)"`
  290. }
  291. func (f *EditVariableForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  292. ctx := context.GetValidateContext(req)
  293. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  294. }
  295. // NewAccessTokenForm form for creating access token
  296. type NewAccessTokenForm struct {
  297. Name string `binding:"Required;MaxSize(255)" locale:"settings.token_name"`
  298. }
  299. // Validate validates the fields
  300. func (f *NewAccessTokenForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  301. ctx := context.GetValidateContext(req)
  302. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  303. }
  304. // EditOAuth2ApplicationForm form for editing oauth2 applications
  305. type EditOAuth2ApplicationForm struct {
  306. Name string `binding:"Required;MaxSize(255)" form:"application_name"`
  307. RedirectURIs string `binding:"Required;ValidUrlList" form:"redirect_uris"`
  308. ConfidentialClient bool `form:"confidential_client"`
  309. SkipSecondaryAuthorization bool `form:"skip_secondary_authorization"`
  310. }
  311. // Validate validates the fields
  312. func (f *EditOAuth2ApplicationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  313. ctx := context.GetValidateContext(req)
  314. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  315. }
  316. // TwoFactorAuthForm for logging in with 2FA token.
  317. type TwoFactorAuthForm struct {
  318. Passcode string `binding:"Required"`
  319. }
  320. // Validate validates the fields
  321. func (f *TwoFactorAuthForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  322. ctx := context.GetValidateContext(req)
  323. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  324. }
  325. // TwoFactorScratchAuthForm for logging in with 2FA scratch token.
  326. type TwoFactorScratchAuthForm struct {
  327. Token string `binding:"Required"`
  328. }
  329. // Validate validates the fields
  330. func (f *TwoFactorScratchAuthForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  331. ctx := context.GetValidateContext(req)
  332. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  333. }
  334. // WebauthnRegistrationForm for reserving an WebAuthn name
  335. type WebauthnRegistrationForm struct {
  336. Name string `binding:"Required"`
  337. }
  338. // Validate validates the fields
  339. func (f *WebauthnRegistrationForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  340. ctx := context.GetValidateContext(req)
  341. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  342. }
  343. // WebauthnDeleteForm for deleting WebAuthn keys
  344. type WebauthnDeleteForm struct {
  345. ID int64 `binding:"Required"`
  346. }
  347. // Validate validates the fields
  348. func (f *WebauthnDeleteForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  349. ctx := context.GetValidateContext(req)
  350. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  351. }
  352. // PackageSettingForm form for package settings
  353. type PackageSettingForm struct {
  354. Action string
  355. RepoName string `form:"repo_name"`
  356. }
  357. // Validate validates the fields
  358. func (f *PackageSettingForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  359. ctx := context.GetValidateContext(req)
  360. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  361. }
  362. type BlockUserForm struct {
  363. Action string `binding:"Required;In(block,unblock,note)"`
  364. Blockee string `binding:"Required"`
  365. Note string
  366. }
  367. func (f *BlockUserForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
  368. ctx := context.GetValidateContext(req)
  369. return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
  370. }