package main

import (
	"fmt"

	"golang.org/x/crypto/ssh"
)

type Admin struct {
	conf *ssh.ClientConfig
}

func sftpConnect(conf *ssh.ClientConfig) (ssh.Conn, error) {
	return ssh.Dial("tcp", "gitea-svr.piwalker.net:22", conf)
}

func sftpAuth(username string, password string) (*ssh.ClientConfig, error) {
	key, _, _, _, err := ssh.ParseAuthorizedKey([]byte("gitea-svr.piwalker.net ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILkyh7MDvubWw4OzTFbvsUz7gOmOzBq77i5Q86STKqja"));
	config := &ssh.ClientConfig{
		User: username,
		Auth: []ssh.AuthMethod{
			ssh.Password(password),
		},
		HostKeyCallback: ssh.FixedHostKey(key),
	}

	conn, err := sftpConnect(config)
	if err != nil {
		return nil, err
	}
	conn.Close()
	return config, nil
}

func (a *Admin) AdminAuth(username string, password string) bool {
	conf, err := sftpAuth(username, password)
	if err != nil {
		fmt.Println("SFTP error: ", err)
		a.conf = nil
		return false	
	}
	a.conf = conf
	return true
}
