-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
186 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package info | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/rusq/encio" | ||
"github.com/rusq/slackdump/v3/auth" | ||
"github.com/rusq/slackdump/v3/cmd/slackdump/internal/cfg" | ||
"github.com/rusq/slackdump/v3/internal/cache" | ||
) | ||
|
||
func CollectAuth(ctx context.Context, w io.Writer) error { | ||
// lg := logger.FromContext(ctx) | ||
fmt.Fprintln(os.Stderr, "To confirm the operation, please enter your OS password.") | ||
if err := osValidateUser(ctx, os.Stderr); err != nil { | ||
return err | ||
} | ||
m, err := cache.NewManager(cfg.CacheDir()) | ||
if err != nil { | ||
return fmt.Errorf("cache error: %w", err) | ||
} | ||
cur, err := m.Current() | ||
if err != nil { | ||
return fmt.Errorf("cache error: %w", err) | ||
} | ||
fi, err := m.FileInfo(cur) | ||
if err != nil { | ||
return fmt.Errorf("cache error: %w", err) | ||
} | ||
f, err := encio.Open(filepath.Join(cfg.CacheDir(), fi.Name())) | ||
if err != nil { | ||
return fmt.Errorf("cache error: %w", err) | ||
} | ||
defer f.Close() | ||
prov, err := auth.Load(f) | ||
if err != nil { | ||
return fmt.Errorf("cache error: %w", err) | ||
} | ||
if err := dumpCookiesMozilla(ctx, w, prov.Cookies()); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// dumpCookiesMozilla dumps cookies in Mozilla format. | ||
func dumpCookiesMozilla(_ context.Context, w io.Writer, cookies []*http.Cookie) error { | ||
tw := tabwriter.NewWriter(w, 0, 8, 0, '\t', 0) | ||
defer tw.Flush() | ||
fmt.Fprintf(tw, "# name@domain\tvalue_len\tflag\tpath\tsecure\texpiration\n") | ||
for _, c := range cookies { | ||
fmt.Fprintf(tw, "%s\t%9d\t%s\t%s\t%s\t%d\n", | ||
c.Name+"@"+c.Domain, len(c.Value), "TRUE", c.Path, strings.ToUpper(fmt.Sprintf("%v", c.Secure)), c.Expires.Unix()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build !windows | ||
|
||
package info | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
) | ||
|
||
func osValidateUser(ctx context.Context, w io.Writer) error { | ||
cmd := exec.CommandContext(ctx, "sudo", "-v") | ||
cmd.Stdin = os.Stdin | ||
cmd.Stdout = w | ||
cmd.Stderr = os.Stderr | ||
err := cmd.Run() | ||
if err != nil { | ||
return fmt.Errorf("authentication failed: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//go:build windows | ||
|
||
package info | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"syscall" | ||
"unsafe" | ||
|
||
"golang.org/x/term" | ||
) | ||
|
||
var ( | ||
advapi32 = syscall.NewLazyDLL("advapi32.dll") | ||
procLogonUserW = advapi32.NewProc("LogonUserW") | ||
) | ||
|
||
func must[T any](t T, err error) T { | ||
if err != nil { | ||
panic(err) | ||
} | ||
return t | ||
} | ||
|
||
// Untested | ||
func logonUser(username, domain, password string) (bool, error) { | ||
var token syscall.Handle | ||
r1, _, err := procLogonUserW.Call( | ||
uintptr(unsafe.Pointer(must(syscall.UTF16PtrFromString(username)))), | ||
uintptr(unsafe.Pointer(must(syscall.UTF16PtrFromString(domain)))), | ||
uintptr(unsafe.Pointer(must(syscall.UTF16PtrFromString(password)))), | ||
uintptr(2), // LOGON32_LOGON_INTERACTIVE | ||
uintptr(0), // LOGON32_PROVIDER_DEFAULT | ||
uintptr(unsafe.Pointer(&token)), | ||
) | ||
if r1 == 0 { | ||
return false, err | ||
} | ||
defer syscall.CloseHandle(token) | ||
return true, nil | ||
} | ||
|
||
func osValidateUser(_ context.Context, w io.Writer) error { | ||
fmt.Fprint(w, "Enter username: ") | ||
var username string | ||
fmt.Scanln(&username) | ||
fmt.Fprintf(w, "Enter password for %s: ", username) | ||
password, err := term.ReadPassword(int(syscall.Stdin)) | ||
if err != nil { | ||
return fmt.Errorf("failed to read password: %w", err) | ||
} | ||
domain := "." // Use "." for local account | ||
ok, err := logonUser(username, domain, string(password)) | ||
if err != nil { | ||
return fmt.Errorf("authentication error: %w", err) | ||
} | ||
if !ok { | ||
return fmt.Errorf("authentication failed") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters