Skip to content

Commit

Permalink
Merge pull request #28 from 09577/master
Browse files Browse the repository at this point in the history
[FIX] fix remove users and [ADD] The jumpers support public key login
  • Loading branch information
elfgzp authored Oct 30, 2020
2 parents 7a4f544 + 1cd1fc5 commit 28af985
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type User struct {
Username string `yaml:"username"`
HashPasswd string `yaml:"hashPasswd"`
Admin bool `yaml:"admin"`
PublicKey string `yaml:"publickey"`
}

// Server server
Expand Down Expand Up @@ -100,12 +101,13 @@ func (c *Config) SaveTo(path string) error {
}

// AddUser add user to config
func (c *Config) AddUser(username string, password string, IsAdmin bool) (string, *User) {
func (c *Config) AddUser(username string, password string, IsAdmin bool, pubKey string) (string, *User) {
// Todo Add sha256 password
user := &User{
Username: username,
HashPasswd: password,
Admin: IsAdmin,
PublicKey: pubKey,
}
userAmount := len(*c.Users) + 1
log.Printf("Add user, user amount: %d", userAmount)
Expand Down
2 changes: 1 addition & 1 deletion core/pui/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func init() {
Label: "Delete user",
GetSubMenu: GetUsersMenu(
func(index int, menuItem *MenuItem, sess *ssh.Session, selectedChain []*MenuItem) error {
userKey := fmt.Sprintf("user%d", index+1)
userKey := fmt.Sprintf("users%d", index+1)
user := (*config.Conf.Users)[userKey]
if user == nil {
return fmt.Errorf("Key '%s' of user not existed. ", userKey)
Expand Down
29 changes: 28 additions & 1 deletion core/pui/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"sort"
"io/ioutil"
"encoding/base64"

"github.com/TNK-Studio/gortal/config"
"github.com/TNK-Studio/gortal/utils"
Expand Down Expand Up @@ -78,6 +80,31 @@ func CreateUser(showAdminSelect bool, isAdmin bool, sess *ssh.Session) (*string,
return nil, nil, err
}

publicKeyPui := promptui.Prompt{
Label: "Your publicKey's position: ",
Validate: MultiValidate([]func(string) error{
func(input string) error {
if !utils.FileExited(input) {
return errors.New("File not found")
}
return nil
},
}),
Stdin: stdio,
Stdout: stdio,
}

publicKeyPos, err := publicKeyPui.Run()
if err != nil {
return nil, nil, err
}
publicKeyFile, err := ioutil.ReadFile(utils.FilePath(publicKeyPos))
if err != nil {
logger.Logger.Warningf("Error reading publicKey file: %s\n", err)
return nil, nil, err
}
publicKeyBase64:= base64.StdEncoding.EncodeToString(publicKeyFile)

IsAdminString := ""
if showAdminSelect && !isAdmin {
adminPui := promptui.Prompt{
Expand All @@ -97,7 +124,7 @@ func CreateUser(showAdminSelect bool, isAdmin bool, sess *ssh.Session) (*string,
if isAdmin {
logger.Logger.Info("Create a admin user")
}
key, user := config.Conf.AddUser(username, passwd, isAdmin)
key, user := config.Conf.AddUser(username, passwd, isAdmin, publicKeyBase64)
return &key, user, nil
}

Expand Down
18 changes: 18 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"time"
"encoding/base64"

"github.com/TNK-Studio/gortal/config"
"github.com/TNK-Studio/gortal/core/jump"
Expand Down Expand Up @@ -40,6 +41,22 @@ func passwordAuth(ctx ssh.Context, pass string) bool {
return success
}

func publickKeyAuth(ctx ssh.Context, key ssh.PublicKey) bool {
var pub string

config.Conf.ReadFrom(*config.ConfPath)
username := ctx.User()
for _, user := range *config.Conf.Users {
if user.Username == username {
pub = user.PublicKey
}
}
decodeBytes, _ := base64.StdEncoding.DecodeString(pub)
allowed, _, _, _, _ := ssh.ParseAuthorizedKey(decodeBytes)

return ssh.KeysEqual(key, allowed)
}

func sessionHandler(sess *ssh.Session) {
defer func() {
(*sess).Close()
Expand Down Expand Up @@ -90,6 +107,7 @@ func main() {
fmt.Sprintf(":%d", *Port),
nil,
ssh.PasswordAuth(passwordAuth),
ssh.PublicKeyAuth(publickKeyAuth),
ssh.HostKeyFile(utils.FilePath(*hostKeyFile)),
),
)
Expand Down

0 comments on commit 28af985

Please sign in to comment.