mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +00:00 
			
		
		
		
	Add check if public key name has been used
This commit is contained in:
		@@ -67,11 +67,23 @@ type PublicKey struct {
 | 
				
			|||||||
	Updated     time.Time `xorm:"updated"`
 | 
						Updated     time.Time `xorm:"updated"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						ErrKeyAlreadyExist = errors.New("Public key already exist")
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GenAuthorizedKey(keyId int64, key string) string {
 | 
					func GenAuthorizedKey(keyId int64, key string) string {
 | 
				
			||||||
	return fmt.Sprintf(tmplPublicKey, appPath, keyId, key)
 | 
						return fmt.Sprintf(tmplPublicKey, appPath, keyId, key)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func AddPublicKey(key *PublicKey) (err error) {
 | 
					func AddPublicKey(key *PublicKey) (err error) {
 | 
				
			||||||
 | 
						// Check if public key name has been used.
 | 
				
			||||||
 | 
						has, err := orm.Get(key)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						} else if has {
 | 
				
			||||||
 | 
							return ErrKeyAlreadyExist
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Calculate fingerprint.
 | 
						// Calculate fingerprint.
 | 
				
			||||||
	tmpPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()),
 | 
						tmpPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()),
 | 
				
			||||||
		"id_rsa.pub")
 | 
							"id_rsa.pub")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -128,6 +128,10 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := models.AddPublicKey(k); err != nil {
 | 
							if err := models.AddPublicKey(k); err != nil {
 | 
				
			||||||
 | 
								if err.Error() == models.ErrKeyAlreadyExist.Error() {
 | 
				
			||||||
 | 
									ctx.RenderWithErr("Public key name has been used", "user/publickey", &form)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			ctx.Handle(200, "ssh.AddPublicKey", err)
 | 
								ctx.Handle(200, "ssh.AddPublicKey", err)
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user