update
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/cli/oauth/device"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -35,20 +34,7 @@ var generateTokenCmd = &cobra.Command{
|
||||
if clientID == "" {
|
||||
clientID = viper.GetViper().GetString("auth.clientID")
|
||||
}
|
||||
if grantType == "" {
|
||||
grantType = viper.GetViper().GetString("auth.grantType")
|
||||
}
|
||||
if url == "" {
|
||||
url = viper.GetViper().GetString("auth.url")
|
||||
}
|
||||
if username == "" {
|
||||
username = viper.GetViper().GetString("auth.username")
|
||||
}
|
||||
if password == "" {
|
||||
password = viper.GetViper().GetString("auth.password")
|
||||
}
|
||||
|
||||
generateToken(clientID, grantType, url, username, password)
|
||||
generateToken(clientID)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -57,55 +43,32 @@ func init() {
|
||||
generateTokenCmd.Flags().
|
||||
StringVarP(&grantType, "grantType", "g", "client_credentials", "The Grant Type")
|
||||
generateTokenCmd.Flags().StringVarP(&url, "url", "", "", "Token URL")
|
||||
generateTokenCmd.Flags().StringVarP(&username, "username", "u", "", "username")
|
||||
generateTokenCmd.Flags().StringVarP(&password, "password", "p", "", "password")
|
||||
generateTokenCmd.Flags().StringVarP(&scope, "scope", "s", "openid profile", "scope")
|
||||
|
||||
AuthCmd.AddCommand(generateTokenCmd)
|
||||
}
|
||||
|
||||
func generateToken(
|
||||
clientID string,
|
||||
grantType string,
|
||||
url string,
|
||||
username string,
|
||||
password string,
|
||||
) {
|
||||
formData := fmt.Sprintf("grant_type=%s&client_id=%s&username=%s&password=%s&scope=%s",
|
||||
grantType, clientID, username, password, scope)
|
||||
scopes := []string{"openid", "email", "profile"}
|
||||
httpClient := http.DefaultClient
|
||||
|
||||
client := &http.Client{}
|
||||
|
||||
req, err := http.NewRequest("POST", url, strings.NewReader(formData))
|
||||
code, err := device.RequestCode(httpClient, "https://authentik.durp.info/application/o/device/", clientID, scopes)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating request:", err)
|
||||
return
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
fmt.Printf("Copy code: %s\n", code.UserCode)
|
||||
fmt.Printf("then open: %s\n", code.VerificationURIComplete)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
accessToken, err := device.Wait(context.TODO(), httpClient, "https://authentik.durp.info/application/o/token/", device.WaitOptions{
|
||||
ClientID: clientID,
|
||||
DeviceCode: code,
|
||||
GrantType: "urn:ietf:params:oauth:grant-type:device_code",
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("Error sending request:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading response:", err)
|
||||
return
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var response accessTokenResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing response:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Access_Token:", response.AccessToken)
|
||||
fmt.Println("Token_Type:", response.TokenType)
|
||||
fmt.Println("Expires_In:", response.ExpiresIn)
|
||||
fmt.Println("ID_Token:", response.IDToken)
|
||||
fmt.Printf("Access token: %s\n", accessToken.Token)
|
||||
}
|
||||
|
||||
41
cmd/init.go
Normal file
41
cmd/init.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"gitlab.com/DeveloperDurp/DurpCLI/internal/tui"
|
||||
)
|
||||
|
||||
func initialize() *cobra.Command {
|
||||
init := &cobra.Command{
|
||||
Use: "initialize",
|
||||
Short: "init the rcl cfg.",
|
||||
Long: "init provision the rcl configuration file.",
|
||||
Example: "rkl init",
|
||||
Aliases: []string{"i", "init"},
|
||||
// used to overwrite/skip the parent commands persistentPreRunE func
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
// Bind Cobra flags with viper
|
||||
if err := viper.BindPFlags(cmd.Flags()); err != nil {
|
||||
return err
|
||||
}
|
||||
// Environment variables are expected to be ALL CAPS
|
||||
viper.AutomaticEnv()
|
||||
viper.SetEnvPrefix("rkl")
|
||||
return nil
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
p := tea.NewProgram(tui.InitialModel())
|
||||
if _, err := p.Run(); err != nil {
|
||||
fmt.Printf("Alas, there's been an error: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return init
|
||||
}
|
||||
18
cmd/root.go
18
cmd/root.go
@@ -21,11 +21,12 @@ var rootCmd = &cobra.Command{
|
||||
Long: ``,
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
func Execute() error {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setDefaults() {
|
||||
@@ -46,9 +47,21 @@ func init() {
|
||||
initConfig()
|
||||
loadConfig()
|
||||
|
||||
// styles := tui.DefaultStyles()
|
||||
// styles.Title.BorderForeground(lipgloss.AdaptiveColor{Light: `#E3BD2D`, Dark: `#E3BD2D`})
|
||||
// styles.Border.BorderForeground(lipgloss.AdaptiveColor{Light: `#E3BD2D`, Dark: `#E3BD2D`})
|
||||
// styles.SelectedItem.Foreground(lipgloss.AdaptiveColor{Light: `#353C3B`, Dark: `#353C3B`}).
|
||||
// Background(lipgloss.AdaptiveColor{Light: `#E3BD2D`, Dark: `#E3BD2D`})
|
||||
//
|
||||
// b := tui.New(tui.WithStyles(styles))
|
||||
|
||||
// rootCmd.SetUsageFunc(b.UsageFunc)
|
||||
// rootCmd.SetHelpFunc(b.HelpFunc)
|
||||
|
||||
rootCmd.AddCommand(net.NetCmd)
|
||||
rootCmd.AddCommand(auth.AuthCmd)
|
||||
rootCmd.AddCommand(cfg.Cfgcmd)
|
||||
rootCmd.AddCommand(initialize())
|
||||
rootCmd.PersistentFlags().
|
||||
StringVar(&cfgFile, "config", "", "config file (default is $HOME/.DurpCLI.yaml)")
|
||||
|
||||
@@ -67,6 +80,7 @@ func initConfig() {
|
||||
|
||||
func loadConfig() {
|
||||
viper.AutomaticEnv()
|
||||
viper.AddConfigPath("~/.durpcli.yaml")
|
||||
err := viper.ReadInConfig()
|
||||
if err != nil {
|
||||
fmt.Println("Config file not found. Creating a new one with defaults...")
|
||||
|
||||
Reference in New Issue
Block a user