Skip to content

Commit

Permalink
feat(auth): implement all authentication related commands (#5)
Browse files Browse the repository at this point in the history
* feat(auth): add login function

* feat(auth): add jwt to config on login

* feat(auth): implement logout command

* feat(auth): implement account command

* feat(auth): enable users to choose a custom server for api calls

* test(auth): add test for Login()

* fix(auth): remove useless print in test

* fix(auth): remove trailing slash in server url if present
  • Loading branch information
noetarbouriech authored Jul 9, 2023
1 parent e6df997 commit d3f59f5
Show file tree
Hide file tree
Showing 9 changed files with 781 additions and 21 deletions.
29 changes: 24 additions & 5 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,39 @@ package cmd

import (
"fmt"
"time"

"github.com/paastech-cloud/cli/pkg/auth"
"github.com/paastech-cloud/cli/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var accountCmd = &cobra.Command{
GroupID: "account",
Use: "account",
Short: "Get infos about user account",
Long: "Get infos about user account",
RunE: func(cmd *cobra.Command, args []string) error {
status, err := auth.Status()
fmt.Println(status)
return err
jwt, err := config.ExtractJWTInfos()
if err != nil {
return err
}

fmt.Println("👤 You are logged in as: " + jwt.Username)
fmt.Println("🌐 Server: " + viper.GetString("server"))
timeDiff := jwt.ExpirationTime.Sub(time.Now())
if timeDiff > 0 {
fmt.Println(
"⌛ Current session expires in: " + fmt.Sprintf(
"%02dh%02dm%02ds",
int(timeDiff.Hours()),
int(timeDiff.Minutes())%60,
int(timeDiff.Seconds())%60,
),
)
} else {
fmt.Println("❌ Current session is expired. Please log back in.")
}
return nil
},
}

Expand Down
43 changes: 35 additions & 8 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cmd

import (
"errors"
"fmt"
"strings"

"github.com/paastech-cloud/cli/internal/config"
"github.com/paastech-cloud/cli/pkg/auth"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
Expand All @@ -11,39 +14,63 @@ import (
var (
email string
password string
server string
)

var loginCmd = &cobra.Command{
GroupID: "account",
Use: "login",
Short: "Log in to PaaSTech.cloud",
Long: "Log in to PaaSTech.cloud",
Short: "Log in to PaaSTech",
RunE: func(cmd *cobra.Command, args []string) error {
email, _ := cmd.Flags().GetString("email")
password, _ := cmd.Flags().GetString("password")

// Ask for email and password if not present in flags
if len(email) == 0 || len(password) == 0 {
fmt.Print("Email: ")
fmt.Scan(&email)
fmt.Scanln(&email)

fmt.Print("Password: ")
p, _ := terminal.ReadPassword(0)
password = string(p)
fmt.Print("\n")
}
if len(email) == 0 || len(password) == 0 {
return errors.New("Email and password cannot be empty. Please try again.")
}

// Use PaaSTech API server by default
if server == "" {
server = "https://api.paastech.cloud"
} else {
server = strings.TrimSuffix(server, "/")
}

fmt.Printf("🔐 Logging in as %s...\n", email)
err := auth.Login(email, password)
if err == nil {
fmt.Println("✅ Login successful")
// Send login request
fmt.Printf("🔐 Logging in as %s\n", email)
jwt, err := auth.Login(server, email, password)
if err != nil {
return err
}
return err

// Load auth config
err = config.LoadAuthConfig()
if err != nil {
return err
}

// Save jwt in auth conf
config.SetAuth(server, jwt)
fmt.Println("✅ Login successful")

return nil
},
}

func init() {
rootCmd.AddCommand(loginCmd)

loginCmd.Flags().StringVarP(&server, "server", "", "", "Server URL")
loginCmd.Flags().StringVarP(&email, "email", "e", "", "Email")
loginCmd.Flags().StringVarP(&password, "password", "p", "", "Password")
loginCmd.MarkFlagsRequiredTogether("email", "password")
Expand Down
13 changes: 11 additions & 2 deletions cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"

"github.com/paastech-cloud/cli/pkg/auth"
"github.com/paastech-cloud/cli/internal/config"
"github.com/spf13/cobra"
)

Expand All @@ -13,8 +13,17 @@ var logoutCmd = &cobra.Command{
Short: "Log out from PaaSTech.cloud",
Long: "Log out from PaaSTech.cloud",
RunE: func(cmd *cobra.Command, args []string) error {
// Load auth config
err := config.LoadAuthConfig()
if err != nil {
return err
}

// Save empty jwt in auth conf
config.SetAuth("", "")
fmt.Println("🚪 Logging out")
return auth.Logout()

return nil
},
}

Expand Down
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module github.com/paastech-cloud/cli
go 1.20

require (
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
golang.org/x/crypto v0.10.0
)

Expand All @@ -13,18 +15,30 @@ require (
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.4.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/skeema/knownhosts v1.1.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/text v0.10.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
Expand Down
Loading

0 comments on commit d3f59f5

Please sign in to comment.