auth.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "github.com/fatih/color"
  7. "github.com/porter-dev/porter/cli/cmd/api"
  8. "github.com/porter-dev/porter/cli/cmd/utils"
  9. "github.com/spf13/cobra"
  10. )
  11. var authCmd = &cobra.Command{
  12. Use: "auth",
  13. Short: "Commands for authenticating to a Porter server",
  14. }
  15. var loginCmd = &cobra.Command{
  16. Use: "login",
  17. Short: "Authorizes a user for a given Porter server",
  18. Run: func(cmd *cobra.Command, args []string) {
  19. err := login()
  20. if err != nil {
  21. color.Red("Error logging in:", err.Error())
  22. os.Exit(1)
  23. }
  24. },
  25. }
  26. var registerCmd = &cobra.Command{
  27. Use: "register",
  28. Short: "Creates a user for a given Porter server",
  29. Run: func(cmd *cobra.Command, args []string) {
  30. err := register()
  31. if err != nil {
  32. color.Red("Error registering:", err.Error())
  33. os.Exit(1)
  34. }
  35. },
  36. }
  37. var logoutCmd = &cobra.Command{
  38. Use: "logout",
  39. Short: "Logs a user out of a given Porter server",
  40. Run: func(cmd *cobra.Command, args []string) {
  41. err := checkLoginAndRun(args, logout)
  42. if err != nil {
  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. client := api.NewClient(getHost()+"/api", "cookie.json")
  61. user, _ := client.AuthCheck(context.Background())
  62. if user != nil {
  63. color.Yellow("You are already logged in. If you'd like to log out, run \"porter auth logout\".")
  64. return nil
  65. }
  66. var username, pw string
  67. fmt.Println("Please log in with an email and password:")
  68. username, err := utils.PromptPlaintext("Email: ")
  69. if err != nil {
  70. return err
  71. }
  72. pw, err = utils.PromptPassword("Password: ")
  73. if err != nil {
  74. return err
  75. }
  76. _user, err := client.Login(context.Background(), &api.LoginRequest{
  77. Email: username,
  78. Password: pw,
  79. })
  80. if err != nil {
  81. return err
  82. }
  83. color.New(color.FgGreen).Println("Successfully logged in!")
  84. // get a list of projects, and set the current project
  85. projects, err := client.ListUserProjects(context.Background(), _user.ID)
  86. if len(projects) > 0 {
  87. setProject(projects[0].ID)
  88. }
  89. return nil
  90. }
  91. func register() error {
  92. host := getHost()
  93. fmt.Println("Please register your admin account with an email and password:")
  94. username, err := utils.PromptPlaintext("Email: ")
  95. if err != nil {
  96. return err
  97. }
  98. pw, err := utils.PromptPasswordWithConfirmation()
  99. if err != nil {
  100. return err
  101. }
  102. client := api.NewClient(host+"/api", "cookie.json")
  103. resp, err := client.CreateUser(context.Background(), &api.CreateUserRequest{
  104. Email: username,
  105. Password: pw,
  106. })
  107. if err != nil {
  108. return err
  109. }
  110. color.New(color.FgGreen).Printf("Created user with email %s and id %d\n", username, resp.ID)
  111. return nil
  112. }
  113. func logout(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  114. err := client.Logout(context.Background())
  115. if err != nil {
  116. return err
  117. }
  118. color.Green("Successfully logged out")
  119. return nil
  120. }