本站源代码
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

372 lines
11KB

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