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

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