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.

keys.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/modules/private"
  9. "github.com/urfave/cli"
  10. )
  11. // CmdKeys represents the available keys sub-command
  12. var CmdKeys = cli.Command{
  13. Name: "keys",
  14. Usage: "This command queries the Gitea database to get the authorized command for a given ssh key fingerprint",
  15. Action: runKeys,
  16. Flags: []cli.Flag{
  17. cli.StringFlag{
  18. Name: "expected, e",
  19. Value: "git",
  20. Usage: "Expected user for whom provide key commands",
  21. },
  22. cli.StringFlag{
  23. Name: "username, u",
  24. Value: "",
  25. Usage: "Username trying to log in by SSH",
  26. },
  27. cli.StringFlag{
  28. Name: "type, t",
  29. Value: "",
  30. Usage: "Type of the SSH key provided to the SSH Server (requires content to be provided too)",
  31. },
  32. cli.StringFlag{
  33. Name: "content, k",
  34. Value: "",
  35. Usage: "Base64 encoded content of the SSH key provided to the SSH Server (requires type to be provided too)",
  36. },
  37. },
  38. }
  39. func runKeys(c *cli.Context) error {
  40. if !c.IsSet("username") {
  41. return errors.New("No username provided")
  42. }
  43. // Check username matches the expected username
  44. if strings.TrimSpace(c.String("username")) != strings.TrimSpace(c.String("expected")) {
  45. return nil
  46. }
  47. content := ""
  48. if c.IsSet("type") && c.IsSet("content") {
  49. content = fmt.Sprintf("%s %s", strings.TrimSpace(c.String("type")), strings.TrimSpace(c.String("content")))
  50. }
  51. if content == "" {
  52. return errors.New("No key type and content provided")
  53. }
  54. ctx, cancel := installSignals()
  55. defer cancel()
  56. setup("keys.log", false)
  57. authorizedString, err := private.AuthorizedPublicKeyByContent(ctx, content)
  58. if err != nil {
  59. return err
  60. }
  61. fmt.Println(strings.TrimSpace(authorizedString))
  62. return nil
  63. }