You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package auth
  5. import (
  6. "net/http"
  7. "reflect"
  8. "strings"
  9. "github.com/go-martini/martini"
  10. "github.com/gogits/binding"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. )
  14. type AddSSHKeyForm struct {
  15. KeyName string `form:"keyname" binding:"Required"`
  16. KeyContent string `form:"key_content" binding:"Required"`
  17. }
  18. func (f *AddSSHKeyForm) Name(field string) string {
  19. names := map[string]string{
  20. "KeyName": "SSH key name",
  21. "KeyContent": "SSH key content",
  22. }
  23. return names[field]
  24. }
  25. func (f *AddSSHKeyForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  26. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  27. AssignForm(f, data)
  28. if req.Method == "GET" || errors.Count() == 0 {
  29. if req.Method == "POST" &&
  30. (len(f.KeyContent) < 100 || !strings.HasPrefix(f.KeyContent, "ssh-rsa")) {
  31. data["HasError"] = true
  32. data["ErrorMsg"] = "SSH key content is not valid"
  33. }
  34. return
  35. }
  36. data["HasError"] = true
  37. if len(errors.Overall) > 0 {
  38. for _, err := range errors.Overall {
  39. log.Error("AddSSHKeyForm.Validate: %v", err)
  40. }
  41. return
  42. }
  43. validate(errors, data, f)
  44. }