auth.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/porter-dev/porter/cli/cmd/api"
  7. "github.com/porter-dev/porter/cli/cmd/utils"
  8. "github.com/spf13/cobra"
  9. )
  10. var authCmd = &cobra.Command{
  11. Use: "auth",
  12. Short: "Commands for authenticating to a Porter server",
  13. }
  14. var loginCmd = &cobra.Command{
  15. Use: "login",
  16. Short: "Authorizes a user for a given Porter server",
  17. Run: func(cmd *cobra.Command, args []string) {
  18. err := login()
  19. if err != nil {
  20. fmt.Println("Error logging in:", err.Error())
  21. os.Exit(1)
  22. }
  23. },
  24. }
  25. var registerCmd = &cobra.Command{
  26. Use: "register",
  27. Short: "Creates a user for a given Porter server",
  28. Run: func(cmd *cobra.Command, args []string) {
  29. err := register()
  30. if err != nil {
  31. fmt.Println("Error registering:", err.Error())
  32. os.Exit(1)
  33. }
  34. },
  35. }
  36. var logoutCmd = &cobra.Command{
  37. Use: "logout",
  38. Short: "Logs a user out of a given Porter server",
  39. Run: func(cmd *cobra.Command, args []string) {
  40. err := logout()
  41. if err != nil {
  42. fmt.Println("Error logging out:", err.Error())
  43. os.Exit(1)
  44. }
  45. },
  46. }
  47. func init() {
  48. rootCmd.AddCommand(authCmd)
  49. authCmd.AddCommand(loginCmd)
  50. authCmd.AddCommand(registerCmd)
  51. authCmd.AddCommand(logoutCmd)
  52. authCmd.PersistentFlags().StringVar(
  53. &host,
  54. "host",
  55. getHost(),
  56. "host url of Porter instance",
  57. )
  58. }
  59. func login() error {
  60. host := getHost()
  61. var username, pw string
  62. fmt.Println("Please log in with an email and password:")
  63. username, err := utils.PromptPlaintext("Email: ")
  64. if err != nil {
  65. return err
  66. }
  67. pw, err = utils.PromptPassword("Password: ")
  68. if err != nil {
  69. return err
  70. }
  71. client := api.NewClient(host+"/api", "cookie.json")
  72. _, err = client.Login(context.Background(), &api.LoginRequest{
  73. Email: username,
  74. Password: pw,
  75. })
  76. if err != nil {
  77. return err
  78. }
  79. fmt.Println("Successfully logged in!")
  80. return nil
  81. }
  82. func register() error {
  83. host := getHost()
  84. fmt.Println("Please register your admin account with an email and password:")
  85. username, err := utils.PromptPlaintext("Email: ")
  86. if err != nil {
  87. return err
  88. }
  89. pw, err := utils.PromptPasswordWithConfirmation()
  90. if err != nil {
  91. return err
  92. }
  93. client := api.NewClient(host+"/api", "cookie.json")
  94. resp, err := client.CreateUser(context.Background(), &api.CreateUserRequest{
  95. Email: username,
  96. Password: pw,
  97. })
  98. if err != nil {
  99. return err
  100. }
  101. fmt.Printf("Created user with email %s and id %d\n", username, resp.ID)
  102. return nil
  103. }
  104. func logout() error {
  105. host := getHost()
  106. client := api.NewClient(host+"/api", "cookie.json")
  107. err := client.Logout(context.Background())
  108. if err != nil {
  109. return err
  110. }
  111. fmt.Println("Successfully logged out")
  112. return nil
  113. }