auth.go 2.9 KB

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