Moving files and adding auth

This commit is contained in:
2018-11-07 11:10:51 +01:00
parent 728ac72dff
commit 1c16b54802
19 changed files with 1175 additions and 309 deletions

View File

@ -0,0 +1,56 @@
package services
import (
"crypto/rand"
"fmt"
jwt "github.com/dgrijalva/jwt-go"
)
var hmacSecret []byte
func GenerateSecret() []byte {
b := make([]byte, 32)
rand.Read(b)
return b
}
func InitAuthService() {
hmacSecret = GenerateSecret()
// TODO: This needs to be replaced with reading rsa keys, there needs to be a automatic generation of these if they do not exist
}
func Validate(Token string) (bool, jwt.Token) {
token, err := jwt.Parse(Token, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return hmacSecret, nil
})
if err == nil && token.Valid {
fmt.Println("Token is valid")
return true, *token
}
fmt.Println("Token Validation failed")
return false, *token
}
func CreateToken(Claims jwt.MapClaims) string {
// Create a new token object, specifying signing method and the claims
// you would like it to contain.
token := jwt.NewWithClaims(jwt.SigningMethodHS256, Claims)
// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString(hmacSecret)
if err != nil {
fmt.Println(err.Error())
}
return tokenString
}

View File

@ -0,0 +1,64 @@
package services
import (
"database/sql"
"fmt"
"os"
"github.com/astaxie/beego/orm"
)
var dbs map[string]*sql.DB
// InitCompanyService Init companydb service and open system db connection
func InitCompanyService() {
fmt.Println("Hello from init") // test if init gets called from each orm
dbs := make(map[string]*sql.DB)
orm.RegisterDataBase("default", "postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre sslmode=disable")
systemDB, err := sql.Open("postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=company5 sslmode=disable")
if err != nil {
fmt.Println("Fatal: could not connect to db, exiting... Error:", err)
os.Exit(1)
}
dbs["system"] = systemDB
}
// GetDatabase Get orm and user information
func GetDatabase(token string) {
// validate token
// retrieve correct user/database
// check if open first
// try to open second
// return error otherwise
// return db with orm or error
}
// CreateDatabase Create a database by copying the template
func CreateDatabase(token string) {
/*
db, err = sql.Open("postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=company5 sslmode=disable")
if err != nil {
log.Fatal(err)
}
or, err := orm.NewOrmWithDB("postgres", "temp", db)
*/
}
// DeleteDatabase Delete an entire database, this is very very dangerous :-)
func DeleteDatabase(token string) {
/*
db.Close()
fmt.Println("Closed company5")
//}
res, err := o.Raw("DROP DATABASE company5;").Exec()
if err == nil {
num, _ := res.RowsAffected()
fmt.Println("mysql row affected number: ", num)
}
*/
}