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.

ssh_key_principals.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2021 The Gitea 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 asymkey
  5. import (
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/models/perm"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. // __________ .__ .__ .__
  15. // \______ _______|__| ____ ____ |_____________ | | ______
  16. // | ___\_ __ | |/ \_/ ___\| \____ \__ \ | | / ___/
  17. // | | | | \| | | \ \___| | |_> / __ \| |__\___ \
  18. // |____| |__| |__|___| /\___ |__| __(____ |____/____ >
  19. // \/ \/ |__| \/ \/
  20. //
  21. // This file contains functions related to principals
  22. // AddPrincipalKey adds new principal to database and authorized_principals file.
  23. func AddPrincipalKey(ownerID int64, content string, authSourceID int64) (*PublicKey, error) {
  24. ctx, committer, err := db.TxContext()
  25. if err != nil {
  26. return nil, err
  27. }
  28. defer committer.Close()
  29. // Principals cannot be duplicated.
  30. has, err := db.GetEngine(ctx).
  31. Where("content = ? AND type = ?", content, KeyTypePrincipal).
  32. Get(new(PublicKey))
  33. if err != nil {
  34. return nil, err
  35. } else if has {
  36. return nil, ErrKeyAlreadyExist{0, "", content}
  37. }
  38. key := &PublicKey{
  39. OwnerID: ownerID,
  40. Name: content,
  41. Content: content,
  42. Mode: perm.AccessModeWrite,
  43. Type: KeyTypePrincipal,
  44. LoginSourceID: authSourceID,
  45. }
  46. if err = db.Insert(ctx, key); err != nil {
  47. return nil, fmt.Errorf("addKey: %v", err)
  48. }
  49. if err = committer.Commit(); err != nil {
  50. return nil, err
  51. }
  52. committer.Close()
  53. return key, RewriteAllPrincipalKeys(db.DefaultContext)
  54. }
  55. // CheckPrincipalKeyString strips spaces and returns an error if the given principal contains newlines
  56. func CheckPrincipalKeyString(user *user_model.User, content string) (_ string, err error) {
  57. if setting.SSH.Disabled {
  58. return "", db.ErrSSHDisabled{}
  59. }
  60. content = strings.TrimSpace(content)
  61. if strings.ContainsAny(content, "\r\n") {
  62. return "", errors.New("only a single line with a single principal please")
  63. }
  64. // check all the allowed principals, email, username or anything
  65. // if any matches, return ok
  66. for _, v := range setting.SSH.AuthorizedPrincipalsAllow {
  67. switch v {
  68. case "anything":
  69. return content, nil
  70. case "email":
  71. emails, err := user_model.GetEmailAddresses(user.ID)
  72. if err != nil {
  73. return "", err
  74. }
  75. for _, email := range emails {
  76. if !email.IsActivated {
  77. continue
  78. }
  79. if content == email.Email {
  80. return content, nil
  81. }
  82. }
  83. case "username":
  84. if content == user.Name {
  85. return content, nil
  86. }
  87. }
  88. }
  89. return "", fmt.Errorf("didn't match allowed principals: %s", setting.SSH.AuthorizedPrincipalsAllow)
  90. }
  91. // ListPrincipalKeys returns a list of principals belongs to given user.
  92. func ListPrincipalKeys(uid int64, listOptions db.ListOptions) ([]*PublicKey, error) {
  93. sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type = ?", uid, KeyTypePrincipal)
  94. if listOptions.Page != 0 {
  95. sess = db.SetSessionPagination(sess, &listOptions)
  96. keys := make([]*PublicKey, 0, listOptions.PageSize)
  97. return keys, sess.Find(&keys)
  98. }
  99. keys := make([]*PublicKey, 0, 5)
  100. return keys, sess.Find(&keys)
  101. }