155 lines
3.5 KiB
Go
155 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
/*
|
|
func authMain(args []string) {
|
|
authCmd := flag.NewFlagSet("auth", flag.ExitOnError)
|
|
token := authCmd.String("t", "", "Auth token")
|
|
verbose := authCmd.Bool("v", false, "Verbose output")
|
|
|
|
authCmd.Parse(args)
|
|
|
|
fmt.Println("Auth command")
|
|
fmt.Println("Token:", *token)
|
|
fmt.Println("Verbose:", *verbose)
|
|
}
|
|
*/
|
|
|
|
var authCmd *flag.FlagSet
|
|
|
|
type AuthenticateRequest struct {
|
|
Accountid string `json:"accountid"`
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
func authLocalGetToken() {
|
|
|
|
}
|
|
|
|
func authLocalAuthenticate(args []string) {
|
|
|
|
var authSource string
|
|
var authUsername string
|
|
var authPassword string
|
|
var authAccountid string
|
|
var outputFormat string
|
|
|
|
authCmd.StringVar(&authSource, "s", "", "Source (required)")
|
|
authCmd.StringVar(&authSource, "source", "", "Source (required)")
|
|
authCmd.StringVar(&authUsername, "u", "", "Username (required)")
|
|
authCmd.StringVar(&authUsername, "username", "", "Username (required)")
|
|
authCmd.StringVar(&authPassword, "p", "", "Password (required)")
|
|
authCmd.StringVar(&authPassword, "password", "", "Password (required)")
|
|
authCmd.StringVar(&authAccountid, "a", "", "Account ID (required)")
|
|
authCmd.StringVar(&authAccountid, "account", "", "Account ID (required)")
|
|
authCmd.StringVar(&outputFormat, "o", "text", "Output format (text or json)")
|
|
|
|
// verbose := authCmd.Bool("v", false, "Verbose output")
|
|
|
|
outputFormat = "text"
|
|
|
|
authCmd.Parse(args)
|
|
|
|
if authSource == "" {
|
|
fmt.Println("Error: either -s or --source is required")
|
|
//authCmd.Usage()
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Enforce required flag
|
|
if authSource == "" {
|
|
fmt.Println("Error: either -s or --source is required")
|
|
//authCmd.Usage()
|
|
os.Exit(1)
|
|
}
|
|
|
|
if authSource != "local" {
|
|
fmt.Println("Error: invalid source. Allowed values: local")
|
|
//authCmd.Usage()
|
|
os.Exit(1)
|
|
}
|
|
|
|
if authAccountid == "" {
|
|
fmt.Print("Account ID: ")
|
|
fmt.Scanln(&authAccountid)
|
|
}
|
|
|
|
if authUsername == "" {
|
|
fmt.Print("Username: ")
|
|
fmt.Scanln(&authUsername)
|
|
}
|
|
if authPassword == "" {
|
|
fmt.Print("Password: ")
|
|
fmt.Scanln(&authPassword)
|
|
}
|
|
|
|
var authenticaterequest AuthenticateRequest
|
|
|
|
authenticaterequest.Accountid = authAccountid
|
|
authenticaterequest.Username = authUsername
|
|
authenticaterequest.Password = authPassword
|
|
|
|
authenticaterequestpostdata, err := json.Marshal(authenticaterequest)
|
|
if err != nil {
|
|
fmt.Println("Error encoding JSON:", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
url := endpoint + "/identity/authenticate"
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(authenticaterequestpostdata))
|
|
if err != nil {
|
|
log.Fatal("Error creating request:", err)
|
|
}
|
|
|
|
// Set headers
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Fatal("Error making request:", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
log.Fatal("Authentication failed with status:", resp.Status)
|
|
}
|
|
|
|
var result map[string]interface{}
|
|
err = json.NewDecoder(resp.Body).Decode(&result)
|
|
if err != nil {
|
|
log.Fatal("Error decoding response:", err)
|
|
}
|
|
|
|
if outputFormat == "json" {
|
|
jsonOutput, err := json.MarshalIndent(result, "", " ")
|
|
if err != nil {
|
|
log.Fatal("Error encoding JSON output:", err)
|
|
}
|
|
fmt.Println(string(jsonOutput))
|
|
return
|
|
} else {
|
|
for key, value := range result {
|
|
fmt.Printf("%s: %v\n", key, value)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func authenticateMain(args []string) {
|
|
authCmd = flag.NewFlagSet("auth", flag.ExitOnError)
|
|
|
|
authLocalAuthenticate(args)
|
|
//fmt.Println("Verbose:", *verbose)
|
|
}
|