From 08b14a71a96520eab573c975f9dc5a144c3ae1f9 Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 10 Oct 2017 10:40:36 +0200 Subject: [PATCH] added config file Signed-off-by: kolaente --- config.ini | 8 ++++++++ main.go | 8 ++++++++ models/config.go | 38 ++++++++++++++++++++++++++++++++++++ routes/api/v1/token_check.go | 16 +++++++++++++++ routes/login.go | 5 +++-- routes/routes.go | 10 +++------- vendor/vendor.json | 16 +++++++++++++++ 7 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 config.ini create mode 100644 models/config.go create mode 100644 routes/api/v1/token_check.go diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..5df0744 --- /dev/null +++ b/config.ini @@ -0,0 +1,8 @@ +[General] +JWTSecret = blablaGEHEMIN§)!§ + +[Database] +User = root +Password = jup2000 +Host = 127.0.0.1 +Database = library2 diff --git a/main.go b/main.go index 8f57ff3..ee44798 100644 --- a/main.go +++ b/main.go @@ -9,11 +9,19 @@ import ( func main(){ + // Set Engine err := models.SetEngine() if err != nil { fmt.Println(err) } + // Init Config + err = models.SetConfig() + if err != nil { + fmt.Println(err) + } + + // Start the webserver e := routes.NewEcho() routes.RegisterRoutes(e) e.Start(":8082") diff --git a/models/config.go b/models/config.go new file mode 100644 index 0000000..bedbab4 --- /dev/null +++ b/models/config.go @@ -0,0 +1,38 @@ +package models + +import ( + "github.com/go-ini/ini" + "os" +) + +var Config struct{ + Database struct{ + Host string + User string + Password string + Database string + } + + JWTLoginSecret []byte +} + +func SetConfig() error{ + + // File Checks + if _, err := os.Stat("config.ini"); os.IsNotExist(err) { + return err + } + + cfg, err := ini.Load("config.ini") + if err != nil { + return err + } + + // Database + cfg.Section("Database").MapTo(Config.Database) + + // JWT secret + Config.JWTLoginSecret = []byte(cfg.Section("General").Key("JWTSecret").String()) + + return nil +} \ No newline at end of file diff --git a/routes/api/v1/token_check.go b/routes/api/v1/token_check.go new file mode 100644 index 0000000..36abc30 --- /dev/null +++ b/routes/api/v1/token_check.go @@ -0,0 +1,16 @@ +package v1 + +import ( + "github.com/labstack/echo" + "github.com/dgrijalva/jwt-go" + "fmt" +) + +func CheckToken(c echo.Context) error { + + user := c.Get("user").(*jwt.Token) + + fmt.Println(user.Valid) + + return nil +} \ No newline at end of file diff --git a/routes/login.go b/routes/login.go index 61e0935..33a2e50 100644 --- a/routes/login.go +++ b/routes/login.go @@ -5,6 +5,7 @@ import ( "github.com/dgrijalva/jwt-go" "time" "net/http" + "git.mowie.cc/konrad/Library/models" ) func Login(c echo.Context) error { @@ -18,14 +19,14 @@ func Login(c echo.Context) error { // Set claims claims := token.Claims.(jwt.MapClaims) claims["name"] = "Jon Snow" - claims["admin"] = true claims["exp"] = time.Now().Add(time.Hour * 72).Unix() // Generate encoded token and send it as response. - t, err := token.SignedString([]byte("secret")) + t, err := token.SignedString(models.Config.JWTLoginSecret) if err != nil { return err } + return c.JSON(http.StatusOK, map[string]string{ "token": t, }) diff --git a/routes/routes.go b/routes/routes.go index d3d5e19..bd4a351 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -5,9 +5,7 @@ import ( "github.com/labstack/echo/middleware" apiv1 "git.mowie.cc/konrad/Library/routes/api/v1" - "github.com/gorilla/sessions" - "github.com/labstack/echo-contrib/session" - "github.com/dgrijalva/jwt-go" + "git.mowie.cc/konrad/Library/models" ) func NewEcho() *echo.Echo { @@ -18,9 +16,6 @@ func NewEcho() *echo.Echo { Format: "${time_rfc3339}: ${remote_ip} ${method} ${status} ${uri} - ${user_agent}\n", })) - // Sessions - e.Use(session.Middleware(sessions.NewFilesystemStore("./session.store", []byte("secret")))) - // Static Content e.Static("/assets", "assets") @@ -44,7 +39,8 @@ func RegisterRoutes(e *echo.Echo) { // ===== Routes with Authetification ===== // Authetification - a.Use(middleware.JWT([]byte("secret"))) + a.Use(middleware.JWT(models.Config.JWTLoginSecret)) + a.POST("/tokenTest", apiv1.CheckToken) // Manage Books a.POST("/books/add", apiv1.Add) diff --git a/vendor/vendor.json b/vendor/vendor.json index 44f6023..7a25d44 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -12,6 +12,18 @@ "revision": "a539ee1a749a2b895533f979515ac7e6e0f5b650", "revisionTime": "2017-06-08T00:51:49Z" }, + { + "checksumSHA1": "87LEfpY9cOk9CP7pWyIbmQ/6enU=", + "path": "github.com/go-ini/ini", + "revision": "20b96f641a5ea98f2f8619ff4f3e061cff4833bd", + "revisionTime": "2017-08-13T05:15:16Z", + "version": "v1.28.2", + "versionExact": "v1.28.2" + }, + { + "path": "github.com/go-ini/iniv1.28.2", + "revision": "" + }, { "checksumSHA1": "fSlTr0rwLji0DfsUOKYLkGUkiWo=", "path": "github.com/go-sql-driver/mysql", @@ -141,6 +153,10 @@ "path": "golang.org/x/sys/unix", "revision": "314a259e304ff91bd6985da2a7149bbf91237993", "revisionTime": "2017-07-19T03:44:26Z" + }, + { + "path": "gopkg.in/ini.v1.28.2", + "revision": "" } ], "rootPath": "git.mowie.cc/konrad/Library"