Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ssh_key_principals.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, loginSourceID int64) (*PublicKey, error) {
  24. ctx, committer, err := db.TxContext()
  25. if err != nil {
  26. return nil, err
  27. }
  28. defer committer.Close()
  29. sess := db.GetEngine(ctx)
  30. // Principals cannot be duplicated.
  31. has, err := sess.
  32. Where("content = ? AND type = ?", content, KeyTypePrincipal).
  33. Get(new(PublicKey))
  34. if err != nil {
  35. return nil, err
  36. } else if has {
  37. return nil, ErrKeyAlreadyExist{0, "", content}
  38. }
  39. key := &PublicKey{
  40. OwnerID: ownerID,
  41. Name: content,
  42. Content: content,
  43. Mode: perm.AccessModeWrite,
  44. Type: KeyTypePrincipal,
  45. LoginSourceID: loginSourceID,
  46. }
  47. if err = addPrincipalKey(sess, key); err != nil {
  48. return nil, fmt.Errorf("addKey: %v", err)
  49. }
  50. if err = committer.Commit(); err != nil {
  51. return nil, err
  52. }
  53. committer.Close()
  54. return key, RewriteAllPrincipalKeys()
  55. }
  56. func addPrincipalKey(e db.Engine, key *PublicKey) (err error) {
  57. // Save Key representing a principal.
  58. if _, err = e.Insert(key); err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. // CheckPrincipalKeyString strips spaces and returns an error if the given principal contains newlines
  64. func CheckPrincipalKeyString(user *user_model.User, content string) (_ string, err error) {
  65. if setting.SSH.Disabled {
  66. return "", db.ErrSSHDisabled{}
  67. }
  68. content = strings.TrimSpace(content)
  69. if strings.ContainsAny(content, "\r\n") {
  70. return "", errors.New("only a single line with a single principal please")
  71. }
  72. // check all the allowed principals, email, username or anything
  73. // if any matches, return ok
  74. for _, v := range setting.SSH.AuthorizedPrincipalsAllow {
  75. switch v {
  76. case "anything":
  77. return content, nil
  78. case "email":
  79. emails, err := user_model.GetEmailAddresses(user.ID)
  80. if err != nil {
  81. return "", err
  82. }
  83. for _, email := range emails {
  84. if !email.IsActivated {
  85. continue
  86. }
  87. if content == email.Email {
  88. return content, nil
  89. }
  90. }
  91. case "username":
  92. if content == user.Name {
  93. return content, nil
  94. }
  95. }
  96. }
  97. return "", fmt.Errorf("didn't match allowed principals: %s", setting.SSH.AuthorizedPrincipalsAllow)
  98. }
  99. // ListPrincipalKeys returns a list of principals belongs to given user.
  100. func ListPrincipalKeys(uid int64, listOptions db.ListOptions) ([]*PublicKey, error) {
  101. sess := db.GetEngine(db.DefaultContext).Where("owner_id = ? AND type = ?", uid, KeyTypePrincipal)
  102. if listOptions.Page != 0 {
  103. sess = db.SetSessionPagination(sess, &listOptions)
  104. keys := make([]*PublicKey, 0, listOptions.PageSize)
  105. return keys, sess.Find(&keys)
  106. }
  107. keys := make([]*PublicKey, 0, 5)
  108. return keys, sess.Find(&keys)
  109. }