DB Caching mechanism working and final service structure

This commit is contained in:
Lukas Bachschwell 2018-11-08 11:21:06 +01:00
parent 11eed92c15
commit df822d6ff9
6 changed files with 35 additions and 47 deletions

View File

@ -2,9 +2,9 @@ package controllers
import ( import (
"multitenantStack/models" "multitenantStack/models"
companydb "multitenantStack/services/companydbservice" companydb "multitenantStack/services/companydb"
jwtservice "multitenantStack/services/jwtservice" tokenTools "multitenantStack/services/tokenTools"
"time" "time"
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
@ -36,8 +36,9 @@ func (c *AuthController) URLMapping() {
func (c *AuthController) Login() { func (c *AuthController) Login() {
type AuthResponse struct { type AuthResponse struct {
Status int Status int `json:"status"`
Jwt string Jwt string `json:"jwt"`
User models.CompanyUser `json:"user"`
} }
if c.Ctx.Input.Method() != "POST" { if c.Ctx.Input.Method() != "POST" {
@ -47,7 +48,7 @@ func (c *AuthController) Login() {
tokenHeader := c.Ctx.Request.Header.Get("X-JWTtoken") tokenHeader := c.Ctx.Request.Header.Get("X-JWTtoken")
if tokenHeader != "" { if tokenHeader != "" {
valid, _ := jwtservice.Validate(tokenHeader) valid, _ := tokenTools.Validate(tokenHeader)
if valid { if valid {
c.ServeJSONError("You are already logged in") c.ServeJSONError("You are already logged in")
return return
@ -61,21 +62,18 @@ func (c *AuthController) Login() {
c.ServeJSONError("Email/Password missing") c.ServeJSONError("Email/Password missing")
return return
} }
/*
systemdb := companydb.GetSystemDatabase()
if systemdb == nil { systemdb := companydb.GetSystemDatabase()
c.ServeJSONError("Error retrieving User")
return if systemdb == nil {
} c.ServeJSONError("Error retrieving User")
o, err := orm.NewOrmWithDB("postgres", "default", systemdb) return
if err != nil { }
c.ServeJSONError("Error retrieving User") o, err := orm.NewOrmWithDB("postgres", "default", systemdb)
return if err != nil {
} c.ServeJSONError("Error retrieving User")
*/ return
o := orm.NewOrm() }
o.Using("system") //TODO: Replace this with something cleverer (manager) once implemented
userCompanyMapping, err := models.GetUserCompanyMapByEmail(o, email) userCompanyMapping, err := models.GetUserCompanyMapByEmail(o, email)
if err != nil { if err != nil {
@ -109,20 +107,13 @@ func (c *AuthController) Login() {
return return
} }
//TODO: if found query the company database to get roleID, and name
name := companyUser.Name
roleID := companyUser.Role
tokenString := "" tokenString := ""
if email == "admin@admin.at" && password == "my password" { if email == "admin@admin.at" && password == "my password" {
// The jwtClaims are our trusted clientside session // The jwtClaims are our trusted clientside session
tokenString = jwtservice.CreateToken(jwt.MapClaims{ tokenString = tokenTools.CreateToken(jwt.MapClaims{
"email": email, "email": email,
"companyName": companyName, "companyName": companyName,
"companyUserID": companyUserID, "companyUserID": companyUserID,
"name": name,
"roleID": roleID,
"exp": time.Now().Unix() + 3600, "exp": time.Now().Unix() + 3600,
}) })
} else { } else {
@ -130,7 +121,7 @@ func (c *AuthController) Login() {
return return
} }
json := AuthResponse{200, tokenString} json := AuthResponse{200, tokenString, *companyUser}
c.Data["json"] = &json c.Data["json"] = &json
c.ServeJSON() c.ServeJSON()

View File

@ -3,7 +3,7 @@ package controllers
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
companydb "multitenantStack/services/companydbservice" companydb "multitenantStack/services/companydb"
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"

View File

@ -1 +1 @@
{"/Users/LB/go/src/multitenantStack/controllers":1541629384273036460} {"/Users/LB/go/src/multitenantStack/controllers":1541672200497550656}

View File

@ -2,8 +2,8 @@ package main
import ( import (
_ "multitenantStack/routers" _ "multitenantStack/routers"
companydb "multitenantStack/services/companydbservice" companydb "multitenantStack/services/companydb"
jwt "multitenantStack/services/jwtservice" tokenTools "multitenantStack/services/tokenTools"
"time" "time"
"github.com/astaxie/beego" "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("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") orm.RegisterDataBase("system", "postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=system sslmode=disable")
jwt.InitJWTService() tokenTools.InitTokenToolsService()
companydb.InitCompanyService() companydb.InitCompanyDBService()
orm.DefaultTimeLoc = time.UTC orm.DefaultTimeLoc = time.UTC
if beego.BConfig.RunMode == "dev" { if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true beego.BConfig.WebConfig.DirectoryIndex = true

View File

@ -1,10 +1,10 @@
package companydbservice package companydb
import ( import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"multitenantStack/services/jwtservice" tokenTools "multitenantStack/services/tokenTools"
"os" "os"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
@ -12,10 +12,9 @@ import (
var dbs map[string]*sql.DB var dbs map[string]*sql.DB
// InitCompanyService Init companydb service and open system db connection // InitCompanyDBService Init companydb service and open system db connection
func InitCompanyService() { func InitCompanyDBService() {
fmt.Println("Hello from init") // test if init gets called from each orm dbs = make(map[string]*sql.DB)
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") systemDB, err := sql.Open("postgres", "host=127.0.0.1 port=5435 user=postgres password=postgre dbname=system sslmode=disable")
if err != nil { if err != nil {
@ -23,30 +22,28 @@ func InitCompanyService() {
os.Exit(1) os.Exit(1)
} }
dbs["system"] = systemDB dbs["system"] = systemDB
} }
// GetSystemDatabase returns system db // GetSystemDatabase returns system db
func GetSystemDatabase() *sql.DB { func GetSystemDatabase() *sql.DB {
fmt.Println(dbs)
return dbs["system"] return dbs["system"]
} }
// GetDatabase Get orm and user information // GetDatabase Get orm and user information
func GetDatabaseWithName(companyName string) (*sql.DB, error) { func GetDatabaseWithName(companyName string) (*sql.DB, error) {
if dbs[companyName] != nil { if dbs[companyName] != nil {
fmt.Println("DB Already open") fmt.Println("DB Already open")
return dbs[companyName], nil return dbs[companyName], nil
} }
conStr := fmt.Sprintf("host=127.0.0.1 port=5435 user=postgres password=postgre dbname=%s sslmode=disable", companyName) 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) db, err := sql.Open("postgres", conStr)
dbs[companyName] = db
if err != nil { if err != nil {
return nil, err return nil, err
} }
// return db with orm or error
return db, nil return db, nil
} }
@ -54,7 +51,7 @@ func GetDatabaseWithName(companyName string) (*sql.DB, error) {
// GetDatabase Get orm and user information // GetDatabase Get orm and user information
func GetDatabase(tokenString string) (jwt.MapClaims, *sql.DB, error) { func GetDatabase(tokenString string) (jwt.MapClaims, *sql.DB, error) {
// validate token // validate token
valid, token := jwtservice.Validate(tokenString) valid, token := tokenTools.Validate(tokenString)
if !valid { if !valid {
return nil, nil, errors.New("Token is invalid") return nil, nil, errors.New("Token is invalid")
} }

View File

@ -1,4 +1,4 @@
package jwtservice package tokenTools
import ( import (
"crypto/rand" "crypto/rand"
@ -17,7 +17,7 @@ func GenerateSecret() []byte {
} }
// InitJWTService generate the secret to verify JWTs and store it in memory // InitJWTService generate the secret to verify JWTs and store it in memory
func InitJWTService() { func InitTokenToolsService() {
hmacSecret = GenerateSecret() hmacSecret = GenerateSecret()
fmt.Println("InitJWTService", hmacSecret) 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 // TODO: This needs to be replaced with reading rsa keys, there needs to be a automatic generation of these if they do not exist