Compare commits
No commits in common. "d583db5450ea3140ba4104caf121f247b7902026" and "733c0410fefd1771a722045122b6533ce110d95a" have entirely different histories.
d583db5450
...
733c0410fe
@ -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", Role: users.RoleAdmin}
|
user := &users.User{Username: "admin"}
|
||||||
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, "role", claims.Role)
|
s.Logger.Debugw("Request is authenticated.", "req_id", reqID, "username", claims.Subject)
|
||||||
|
|
||||||
next.ServeHTTP(w, withCtx)
|
next.ServeHTTP(w, withCtx)
|
||||||
}
|
}
|
||||||
|
@ -1,40 +1,74 @@
|
|||||||
package actions
|
package actions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.t-juice.club/torjus/gpaste/client"
|
"git.t-juice.club/torjus/gpaste/api"
|
||||||
"git.t-juice.club/torjus/gpaste/files"
|
"github.com/google/uuid"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ActionUpload(c *cli.Context) error {
|
func ActionUpload(c *cli.Context) error {
|
||||||
clnt := client.Client{
|
url := fmt.Sprintf("%s/api/file", c.String("url"))
|
||||||
BaseURL: c.String("url"),
|
client := &http.Client{}
|
||||||
}
|
// TODO: Change timeout
|
||||||
|
ctx, cancel := context.WithTimeout(c.Context, 10*time.Minute)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
mw := multipart.NewWriter(buf)
|
||||||
|
|
||||||
for _, arg := range c.Args().Slice() {
|
for _, arg := range c.Args().Slice() {
|
||||||
f, err := os.Open(arg)
|
f, err := os.Open(arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
file := &files.File{
|
fw, err := mw.CreateFormFile(uuid.Must(uuid.NewRandom()).String(), arg)
|
||||||
OriginalFilename: arg,
|
|
||||||
Body: f,
|
|
||||||
}
|
|
||||||
resp, err := clnt.Upload(c.Context, file)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errmsg := fmt.Sprintf("Error uploading file: %s", err)
|
return err
|
||||||
return cli.Exit(errmsg, 1)
|
|
||||||
}
|
}
|
||||||
fmt.Printf("Uploaded file %s - %s", file.OriginalFilename, resp[0].URL)
|
if _, err := io.Copy(fw, f); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mw.Close()
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Add("Content-Type", mw.FormDataContentType())
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
var expectedResp []struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(resp.Body)
|
||||||
|
if err := decoder.Decode(&expectedResp); err != nil {
|
||||||
|
return fmt.Errorf("error decoding response: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range expectedResp {
|
||||||
|
fmt.Printf("Uploaded file %s\n", r.ID)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -49,49 +83,92 @@ func ActionLogin(c *cli.Context) error {
|
|||||||
return fmt.Errorf("error reading password: %w", err)
|
return fmt.Errorf("error reading password: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
clnt := client.Client{
|
url := fmt.Sprintf("%s/api/login", c.String("url"))
|
||||||
BaseURL: c.String("url"),
|
client := &http.Client{}
|
||||||
|
// TODO: Change timeout
|
||||||
|
ctx, cancel := context.WithTimeout(c.Context, 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
body := new(bytes.Buffer)
|
||||||
|
requestData := struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}{
|
||||||
|
Username: username,
|
||||||
|
Password: password,
|
||||||
}
|
}
|
||||||
if err := clnt.Login(c.Context, username, password); err != nil {
|
encoder := json.NewEncoder(body)
|
||||||
errmsg := fmt.Sprintf("Error logging in: %s", err)
|
if err := encoder.Encode(&requestData); err != nil {
|
||||||
return cli.Exit(errmsg, 1)
|
return fmt.Errorf("error encoding response: %w", err)
|
||||||
}
|
}
|
||||||
// TODO: Store this somewhere, so we don't need to log in each time
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
|
||||||
fmt.Println("Successfully logged in.")
|
if err != nil {
|
||||||
|
return fmt.Errorf("error creating request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to perform request: %s", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return cli.Exit("got non-ok response from server", 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
responseData := struct {
|
||||||
|
Token string `json:"token"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(resp.Body)
|
||||||
|
if err := decoder.Decode(&responseData); err != nil {
|
||||||
|
return fmt.Errorf("unable to parse response: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Token: %s", responseData.Token)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
fmt.Println("Need to be logged in to create user")
|
username := c.Args().First()
|
||||||
username := readString("Enter username: ")
|
if 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
clnt := client.Client{
|
url := fmt.Sprintf("%s/api/user", c.String("url"))
|
||||||
BaseURL: c.String("url"),
|
client := &http.Client{}
|
||||||
}
|
// 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()
|
||||||
|
|
||||||
if err := clnt.Login(ctx, username, password); err != nil {
|
body := new(bytes.Buffer)
|
||||||
errmsg := fmt.Sprintf("Error logging in: %s", err)
|
requestData := &api.RequestAPIUserCreate{
|
||||||
return cli.Exit(errmsg, 1)
|
Username: username,
|
||||||
|
Password: password,
|
||||||
}
|
}
|
||||||
|
encoder := json.NewEncoder(body)
|
||||||
fmt.Println("User to create:")
|
if err := encoder.Encode(requestData); err != nil {
|
||||||
username = readString("Enter username: ")
|
return fmt.Errorf("error encoding response: %w", err)
|
||||||
password, err = readPassword()
|
}
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error reading password: %w", err)
|
return fmt.Errorf("error creating request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := clnt.UserCreate(ctx, username, password); err != nil {
|
resp, err := client.Do(req)
|
||||||
errmsg := fmt.Sprintf("Error creating user: %s", err)
|
if err != nil {
|
||||||
return cli.Exit(errmsg, 1)
|
return fmt.Errorf("unable to perform request: %s", err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusAccepted {
|
||||||
|
return cli.Exit("got non-ok response from server", 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Created user %s\n", username)
|
fmt.Printf("Created user %s\n", username)
|
||||||
@ -109,12 +186,3 @@ 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