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