Skip to content

Commit

Permalink
removed beego framework and added jwt authenticate
Browse files Browse the repository at this point in the history
  • Loading branch information
genshen committed Dec 11, 2017
1 parent 7efb523 commit 029db94
Show file tree
Hide file tree
Showing 26 changed files with 700 additions and 311 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
.glide/

*.exe~
dist/
temp/
static/
views/
.idea/
.idea/*
1 change: 1 addition & 0 deletions conf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.yaml
16 changes: 0 additions & 16 deletions conf/app.conf

This file was deleted.

13 changes: 13 additions & 0 deletions conf/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
site:
appname: webConsole
listen_addr: :2222
runmode: prod
static_dir: static/
views_dir: views/
ssh:
io_mode: 1 # the mode reading data from ssh server: channel mode (0) OR session mode (1)
jwt:
jwt_secret: secret.console.hpc.gensh.me
token_lifetime: 7200
issuer: issuer.ssh.gensh.me
query_token_key: _t
11 changes: 11 additions & 0 deletions conf/config.yaml.exampke
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
site:
appname: webConsole
listen_addr: :2222
runmode: prod
static_dir: /static/
ssh:
io_mode: 1 # the mode reading data from ssh server: channel mode (0) OR session mode (1)
jwt:
jwt_secret: secret.console.hpc.gensh.me
token_lifetime: 7200
issuer: issuer.ssh.gensh.me
1 change: 0 additions & 1 deletion conf/ssh_config.conf

This file was deleted.

11 changes: 5 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package main

import (
_ "github.com/genshen/webConsole/src/routers"
"github.com/astaxie/beego"
"os"
"log"
"golang.org/x/crypto/ssh"
"github.com/genshen/webConsole/src/utils"
"log"
"github.com/genshen/webConsole/src/routers"
)

func main() {
beego.Run()
routers.Run()
//setupSSH()
}

Expand All @@ -28,7 +27,7 @@ func setupSSH() {
},
}
_, err := sshEntity.Connect("genshen", "genshen1234")
check(err,"connect")
check(err, "connect")
defer sshEntity.Client.Close()

session, err := sshEntity.Client.NewSession()
Expand All @@ -52,4 +51,4 @@ func setupSSH() {

err = session.Wait()
check(err, "return")
}
}
50 changes: 50 additions & 0 deletions src/controllers/auth_base_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package controllers

import (
"log"
"strings"
"net/http"
"github.com/genshen/webConsole/src/utils"
)

type AfterAuthenticated interface {
// make sure token and session is not nil.
ServeAfterAuthenticated(w http.ResponseWriter, r *http.Request, token *utils.Claims, session *utils.Session)
}

func AuthPreChecker(i AfterAuthenticated) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var authHead = r.Header.Get("Authorization")
var token string
if authHead != "" {
lIndex := strings.LastIndex(authHead, " ")
if lIndex < 0 || lIndex+1 >= len(authHead) {
utils.Abort(w, "invalid token", 400)
log.Println("Error: invalid token", 400)
return
} else {
token = authHead[lIndex+1:]
}
} else {
if token = r.URL.Query().Get(utils.Config.Jwt.QueryTokenKey); token == "" {
utils.Abort(w, "invalid token", 400)
log.Println("Error: invalid token", 400)
return
} // else token != "", then passed and go on running
}

if claims, err := utils.JwtVerify(token); err != nil {
http.Error(w, "invalid token", 400)
log.Println("Error: Cannot setup WebSocket connection:", err)
} else { // check passed.
// check session.
if session, ok := utils.SessionStorage.Get(token); !ok {
utils.Abort(w, "Error: Cannot get Session data:", 400)
log.Println("Error: Cannot get Session data for token", token)
} else {
defer utils.SessionStorage.Delete(token) // clear session after ssh closed.
i.ServeAfterAuthenticated(w, r, claims, &session)
}
}
}
}
9 changes: 0 additions & 9 deletions src/controllers/base_controller.go

This file was deleted.

89 changes: 47 additions & 42 deletions src/controllers/main_controller.go
Original file line number Diff line number Diff line change
@@ -1,58 +1,63 @@
package controllers

import (
"strconv"
"net/http"
"github.com/genshen/webConsole/src/models"
"github.com/genshen/webConsole/src/utils"
)

type MainController struct {
BaseController
func Get(w http.ResponseWriter, r *http.Request) {
utils.ServeHTTPByName(w,r,"index.html")
}

func (this *MainController) Get() {
this.TplName = "index.html"
}

const (
SIGN_IN_FORM_TYPE_ERROR_VALID = iota
SIGN_IN_FORM_TYPE_ERROR_PASSWORD
SIGN_IN_FORM_TYPE_ERROR_TEST
)
func SignIn(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Invalid request method.", 405)
} else {
var err error
var errUnmarshal models.SignInFormValid
err = r.ParseForm()
if err != nil {
panic(err)
}
userinfo := models.UserInfo{}
userinfo.Host = r.Form.Get("host")
port := r.Form.Get("port")
userinfo.Username = r.Form.Get("username")
userinfo.Password = r.Form.Get("passwd")

func (this *MainController) SignIn() {
var err error;
userinfo := models.UserInfo{}
userinfo.Host = this.GetString("host")
userinfo.Port, err = this.GetInt("port", 22)
userinfo.Username = this.GetString("username")
userinfo.Password = this.GetString("passwd")
if err == nil && userinfo.Host != "" && userinfo.Username != "" {
//try to login ssh account
ssh := utils.SSH{}
ssh.Node.Host = userinfo.Host
ssh.Node.Port = userinfo.Port
_, err := ssh.Connect(userinfo.Username, userinfo.Password)
userinfo.Port, err = strconv.Atoi(port)
if err != nil {
errUnmarshal := models.SignInFormValid{HasError: true, Message: SIGN_IN_FORM_TYPE_ERROR_PASSWORD}
this.Data["json"] = &errUnmarshal
} else {
defer ssh.Close()
// create session
if session, err := ssh.Client.NewSession(); err == nil {
if err := session.Run("whoami"); err == nil {
this.SetSession("userinfo", userinfo)
errUnmarshal := models.SignInFormValid{HasError: false}
this.Data["json"] = &errUnmarshal
this.ServeJSON()
return
userinfo.Port = 22
}

if userinfo.Host != "" && userinfo.Username != "" {
//try to login ssh account
ssh := utils.SSH{}
ssh.Node.Host = userinfo.Host
ssh.Node.Port = userinfo.Port
_, err := ssh.Connect(userinfo.Username, userinfo.Password)
if err != nil {
errUnmarshal = models.SignInFormValid{HasError: true, Message: models.SIGN_IN_FORM_TYPE_ERROR_PASSWORD}
} else {
defer ssh.Close()
// create session
if session, err := ssh.Client.NewSession(); err == nil {
if err := session.Run("whoami"); err == nil {
if token, expireUnix, err := utils.JwtNewToken(userinfo.Connection, utils.Config.Jwt.Issuer); err == nil {
errUnmarshal = models.SignInFormValid{HasError: false, Addition: token}
utils.ServeJSON(w, errUnmarshal)
utils.SessionStorage.Put(token, expireUnix, userinfo)
return
}
}
}
errUnmarshal = models.SignInFormValid{HasError: true, Message: models.SIGN_IN_FORM_TYPE_ERROR_TEST}
}
errUnmarshal := models.SignInFormValid{HasError: true, Message: SIGN_IN_FORM_TYPE_ERROR_TEST}
this.Data["json"] = &errUnmarshal
} else {
errUnmarshal = models.SignInFormValid{HasError: true, Message: models.SIGN_IN_FORM_TYPE_ERROR_VALID}
}
} else {
errUnmarshal := models.SignInFormValid{HasError: true, Message: SIGN_IN_FORM_TYPE_ERROR_VALID}
this.Data["json"] = &errUnmarshal
utils.ServeJSON(w, errUnmarshal)
}
this.ServeJSON()
}
42 changes: 20 additions & 22 deletions src/controllers/upload_controller.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
package controllers

import (
"github.com/astaxie/beego"
"github.com/genshen/webConsole/src/models"
"log"
"net/http"
"github.com/genshen/webConsole/src/utils"
"github.com/genshen/webConsole/src/models"
)

type UploadController struct {
BaseController
}
type FileUpload struct{}

func (this *UploadController) UploadFile() {
file, header, err := this.GetFile("file")
func (f FileUpload) ServeAfterAuthenticated(w http.ResponseWriter, r *http.Request, claims *utils.Claims, session *utils.Session) {
//file, header, err := this.GetFile("file")

r.ParseMultipartForm(32 << 20)
file, header, err := r.FormFile("file")
if err != nil {
beego.Error("getfile err ", err)
this.Abort("503")
log.Println("Error: getfile err ", err)
utils.Abort(w, "error", 503)
return
}
defer file.Close()

user := session.Value.(models.UserInfo)
if err := utils.UploadFile(utils.SftpNode{Host: user.Host, Port: user.Port}, user.Username, user.Password, file, header); err != nil {
log.Println("Error: sftp error:", err)
utils.Abort(w, "message", 503)
} else {
v := this.GetSession("userinfo")
if v == nil {
beego.Error("Cannot get Session data:", err)
this.Abort("503")
} else {
user := v.(models.UserInfo)
if err := utils.UploadFile(user, file, header); err != nil {
beego.Error("sftp error:", err)
this.Abort("503")
} else {
this.Ctx.WriteString("sussess")
}
}
w.Write([]byte("sussess"))
}
}
Loading

0 comments on commit 029db94

Please sign in to comment.