code.gitea.io/gitea@v1.22.3/services/forms/user_form.go (about)

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