use client for user create action
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
88d9a76785
commit
d583db5450
@ -37,7 +37,7 @@ func NewHTTPServer(cfg *gpaste.ServerConfig) *HTTPServer {
|
|||||||
|
|
||||||
// Create initial user
|
// Create initial user
|
||||||
// TODO: Do properly
|
// TODO: Do properly
|
||||||
user := &users.User{Username: "admin"}
|
user := &users.User{Username: "admin", Role: users.RoleAdmin}
|
||||||
user.SetPassword("admin")
|
user.SetPassword("admin")
|
||||||
srv.Users.Store(user)
|
srv.Users.Store(user)
|
||||||
|
|
||||||
|
@ -71,7 +71,7 @@ func (s *HTTPServer) MiddlewareAuthentication(next http.Handler) http.Handler {
|
|||||||
ctx = context.WithValue(ctx, authCtxAuthLevel, claims.Role)
|
ctx = context.WithValue(ctx, authCtxAuthLevel, claims.Role)
|
||||||
ctx = context.WithValue(ctx, authCtxClaims, claims)
|
ctx = context.WithValue(ctx, authCtxClaims, claims)
|
||||||
withCtx := r.WithContext(ctx)
|
withCtx := r.WithContext(ctx)
|
||||||
s.Logger.Debugw("Request is authenticated.", "req_id", reqID, "username", claims.Subject)
|
s.Logger.Debugw("Request is authenticated.", "req_id", reqID, "username", claims.Subject, "role", claims.Role)
|
||||||
|
|
||||||
next.ServeHTTP(w, withCtx)
|
next.ServeHTTP(w, withCtx)
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,14 @@
|
|||||||
package actions
|
package actions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.t-juice.club/torjus/gpaste/api"
|
|
||||||
"git.t-juice.club/torjus/gpaste/client"
|
"git.t-juice.club/torjus/gpaste/client"
|
||||||
"git.t-juice.club/torjus/gpaste/files"
|
"git.t-juice.club/torjus/gpaste/files"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@ -67,43 +64,34 @@ func ActionLogin(c *cli.Context) error {
|
|||||||
|
|
||||||
func ActionUserCreate(c *cli.Context) error {
|
func ActionUserCreate(c *cli.Context) error {
|
||||||
// TODO: Needs to supply auth token to actually work
|
// TODO: Needs to supply auth token to actually work
|
||||||
username := c.Args().First()
|
fmt.Println("Need to be logged in to create user")
|
||||||
if username == "" {
|
username := readString("Enter username: ")
|
||||||
return cli.Exit("USERNAME not supplied.", 1)
|
|
||||||
}
|
|
||||||
password, err := readPassword()
|
password, err := readPassword()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error reading password: %w", err)
|
return fmt.Errorf("error reading password: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/api/user", c.String("url"))
|
clnt := client.Client{
|
||||||
client := &http.Client{}
|
BaseURL: c.String("url"),
|
||||||
// TODO: Change timeout
|
}
|
||||||
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
body := new(bytes.Buffer)
|
if err := clnt.Login(ctx, username, password); err != nil {
|
||||||
requestData := &api.RequestAPIUserCreate{
|
errmsg := fmt.Sprintf("Error logging in: %s", err)
|
||||||
Username: username,
|
return cli.Exit(errmsg, 1)
|
||||||
Password: password,
|
|
||||||
}
|
|
||||||
encoder := json.NewEncoder(body)
|
|
||||||
if err := encoder.Encode(requestData); err != nil {
|
|
||||||
return fmt.Errorf("error encoding response: %w", err)
|
|
||||||
}
|
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error creating request: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
fmt.Println("User to create:")
|
||||||
|
username = readString("Enter username: ")
|
||||||
|
password, err = readPassword()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to perform request: %s", err)
|
return fmt.Errorf("error reading password: %w", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusAccepted {
|
if err := clnt.UserCreate(ctx, username, password); err != nil {
|
||||||
return cli.Exit("got non-ok response from server", 0)
|
errmsg := fmt.Sprintf("Error creating user: %s", err)
|
||||||
|
return cli.Exit(errmsg, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Created user %s\n", username)
|
fmt.Printf("Created user %s\n", username)
|
||||||
@ -121,3 +109,12 @@ func readPassword() (string, error) {
|
|||||||
password := string(bytePassword)
|
password := string(bytePassword)
|
||||||
return strings.TrimSpace(password), nil
|
return strings.TrimSpace(password), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readString(prompt string) string {
|
||||||
|
fmt.Print(prompt)
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
for scanner.Scan() {
|
||||||
|
return scanner.Text()
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user