From df822d6ff9bdead6fa8f7bfe37123aa51622d207 Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Thu, 8 Nov 2018 11:21:06 +0100 Subject: [PATCH] DB Caching mechanism working and final service structure --- controllers/auth.go | 47 ++++++++----------- controllers/baseAPI.go | 2 +- lastupdate.tmp | 2 +- main.go | 8 ++-- .../companydb.go | 19 ++++---- .../jwt.go => tokenTools/tokenTools.go} | 4 +- 6 files changed, 35 insertions(+), 47 deletions(-) rename services/{companydbservice => companydb}/companydb.go (86%) rename services/{jwtservice/jwt.go => tokenTools/tokenTools.go} (96%) diff --git a/controllers/auth.go b/controllers/auth.go index 5c49848..9ef4bce 100644 --- a/controllers/auth.go +++ b/controllers/auth.go @@ -2,9 +2,9 @@ package controllers import ( "multitenantStack/models" - companydb "multitenantStack/services/companydbservice" + companydb "multitenantStack/services/companydb" - jwtservice "multitenantStack/services/jwtservice" + tokenTools "multitenantStack/services/tokenTools" "time" "github.com/astaxie/beego/orm" @@ -36,8 +36,9 @@ func (c *AuthController) URLMapping() { func (c *AuthController) Login() { type AuthResponse struct { - Status int - Jwt string + Status int `json:"status"` + Jwt string `json:"jwt"` + User models.CompanyUser `json:"user"` } if c.Ctx.Input.Method() != "POST" { @@ -47,7 +48,7 @@ func (c *AuthController) Login() { tokenHeader := c.Ctx.Request.Header.Get("X-JWTtoken") if tokenHeader != "" { - valid, _ := jwtservice.Validate(tokenHeader) + valid, _ := tokenTools.Validate(tokenHeader) if valid { c.ServeJSONError("You are already logged in") return @@ -61,21 +62,18 @@ func (c *AuthController) Login() { c.ServeJSONError("Email/Password missing") return } - /* - systemdb := companydb.GetSystemDatabase() - if systemdb == nil { - c.ServeJSONError("Error retrieving User") - return - } - o, err := orm.NewOrmWithDB("postgres", "default", systemdb) - if err != nil { - c.ServeJSONError("Error retrieving User") - return - } - */ - o := orm.NewOrm() - o.Using("system") //TODO: Replace this with something cleverer (manager) once implemented + systemdb := companydb.GetSystemDatabase() + + if systemdb == nil { + c.ServeJSONError("Error retrieving User") + return + } + o, err := orm.NewOrmWithDB("postgres", "default", systemdb) + if err != nil { + c.ServeJSONError("Error retrieving User") + return + } userCompanyMapping, err := models.GetUserCompanyMapByEmail(o, email) if err != nil { @@ -109,20 +107,13 @@ func (c *AuthController) Login() { return } - //TODO: if found query the company database to get roleID, and name - - name := companyUser.Name - roleID := companyUser.Role - tokenString := "" if email == "admin@admin.at" && password == "my password" { // The jwtClaims are our trusted clientside session - tokenString = jwtservice.CreateToken(jwt.MapClaims{ + tokenString = tokenTools.CreateToken(jwt.MapClaims{ "email": email, "companyName": companyName, "companyUserID": companyUserID, - "name": name, - "roleID": roleID, "exp": time.Now().Unix() + 3600, }) } else { @@ -130,7 +121,7 @@ func (c *AuthController) Login() { return } - json := AuthResponse{200, tokenString} + json := AuthResponse{200, tokenString, *companyUser} c.Data["json"] = &json c.ServeJSON() diff --git a/controllers/baseAPI.go b/controllers/baseAPI.go index 308ae89..461b2c8 100644 --- a/controllers/baseAPI.go +++ b/controllers/baseAPI.go @@ -3,7 +3,7 @@ package controllers import ( "database/sql" "fmt" - companydb "multitenantStack/services/companydbservice" + companydb "multitenantStack/services/companydb" "github.com/astaxie/beego/orm" jwt "github.com/dgrijalva/jwt-go" diff --git a/lastupdate.tmp b/lastupdate.tmp index f9f866d..b6d890a 100755 --- a/lastupdate.tmp +++ b/lastupdate.tmp @@ -1 +1 @@ -{"/Users/LB/go/src/multitenantStack/controllers":1541629384273036460} \ No newline at end of file +{"/Users/LB/go/src/multitenantStack/controllers":1541672200497550656} \ No newline at end of file diff --git a/main.go b/main.go index cf29d39..f6809b4 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,8 @@ package main import ( _ "multitenantStack/routers" - companydb "multitenantStack/services/companydbservice" - jwt "multitenantStack/services/jwtservice" + companydb "multitenantStack/services/companydb" + tokenTools "multitenantStack/services/tokenTools" "time" "github.com/astaxie/beego" @@ -15,8 +15,8 @@ func init() { orm.RegisterDataBase("default", "postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre sslmode=disable") orm.RegisterDataBase("system", "postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=system sslmode=disable") - jwt.InitJWTService() - companydb.InitCompanyService() + tokenTools.InitTokenToolsService() + companydb.InitCompanyDBService() orm.DefaultTimeLoc = time.UTC if beego.BConfig.RunMode == "dev" { beego.BConfig.WebConfig.DirectoryIndex = true diff --git a/services/companydbservice/companydb.go b/services/companydb/companydb.go similarity index 86% rename from services/companydbservice/companydb.go rename to services/companydb/companydb.go index 57a411f..eddbb90 100644 --- a/services/companydbservice/companydb.go +++ b/services/companydb/companydb.go @@ -1,10 +1,10 @@ -package companydbservice +package companydb import ( "database/sql" "errors" "fmt" - "multitenantStack/services/jwtservice" + tokenTools "multitenantStack/services/tokenTools" "os" jwt "github.com/dgrijalva/jwt-go" @@ -12,10 +12,9 @@ import ( 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) +// InitCompanyDBService Init companydb service and open system db connection +func InitCompanyDBService() { + dbs = make(map[string]*sql.DB) systemDB, err := sql.Open("postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=system sslmode=disable") if err != nil { @@ -23,30 +22,28 @@ func InitCompanyService() { os.Exit(1) } dbs["system"] = systemDB - } // GetSystemDatabase returns system db func GetSystemDatabase() *sql.DB { + fmt.Println(dbs) return dbs["system"] } // GetDatabase Get orm and user information func GetDatabaseWithName(companyName string) (*sql.DB, error) { - if dbs[companyName] != nil { fmt.Println("DB Already open") return dbs[companyName], nil } conStr := fmt.Sprintf("host=127.0.0.1 port=5435 user=postgres password=postgre dbname=%s sslmode=disable", companyName) - fmt.Println(conStr) db, err := sql.Open("postgres", conStr) + dbs[companyName] = db if err != nil { return nil, err } - // return db with orm or error return db, nil } @@ -54,7 +51,7 @@ func GetDatabaseWithName(companyName string) (*sql.DB, error) { // GetDatabase Get orm and user information func GetDatabase(tokenString string) (jwt.MapClaims, *sql.DB, error) { // validate token - valid, token := jwtservice.Validate(tokenString) + valid, token := tokenTools.Validate(tokenString) if !valid { return nil, nil, errors.New("Token is invalid") } diff --git a/services/jwtservice/jwt.go b/services/tokenTools/tokenTools.go similarity index 96% rename from services/jwtservice/jwt.go rename to services/tokenTools/tokenTools.go index e354ccc..e883f6e 100644 --- a/services/jwtservice/jwt.go +++ b/services/tokenTools/tokenTools.go @@ -1,4 +1,4 @@ -package jwtservice +package tokenTools import ( "crypto/rand" @@ -17,7 +17,7 @@ func GenerateSecret() []byte { } // InitJWTService generate the secret to verify JWTs and store it in memory -func InitJWTService() { +func InitTokenToolsService() { hmacSecret = GenerateSecret() fmt.Println("InitJWTService", hmacSecret) // TODO: This needs to be replaced with reading rsa keys, there needs to be a automatic generation of these if they do not exist