Browse Source

fixed JWT token auth

Ubuntu 4 years ago
parent
commit
fa3faf778d
4 changed files with 7 additions and 5 deletions
  1. 1 1
      config/environments/dev.yaml
  2. 2 2
      controllers/networkHttpController.go
  3. 3 2
      functions/jwt.go
  4. 1 0
      main.go

+ 1 - 1
config/environments/dev.yaml

@@ -1,5 +1,5 @@
 server:
 server:
-  host: "localhost"
+  host: "3.231.219.63"
   apiport: "8081"
   apiport: "8081"
   grpcport: "50051"
   grpcport: "50051"
   masterkey: "secretkey"
   masterkey: "secretkey"

+ 2 - 2
controllers/networkHttpController.go

@@ -9,7 +9,7 @@ import (
 	"net/http"
 	"net/http"
 	"strings"
 	"strings"
 	"time"
 	"time"
-
+	"os"
 	"github.com/gorilla/mux"
 	"github.com/gorilla/mux"
 	"github.com/gravitl/netmaker/config"
 	"github.com/gravitl/netmaker/config"
 	"github.com/gravitl/netmaker/functions"
 	"github.com/gravitl/netmaker/functions"
@@ -83,7 +83,7 @@ func securityCheck(next http.Handler) http.HandlerFunc {
 
 
 //Consider a more secure way of setting master key
 //Consider a more secure way of setting master key
 func authenticateMaster(tokenString string) bool {
 func authenticateMaster(tokenString string) bool {
-	if tokenString == config.Config.Server.MasterKey {
+	if tokenString == config.Config.Server.MasterKey  || (tokenString == os.Getenv("MASTER_KEY") && tokenString != "") {
 		return true
 		return true
 	}
 	}
 	return false
 	return false

+ 3 - 2
functions/jwt.go

@@ -2,6 +2,7 @@ package functions
 
 
 import (
 import (
     "time"
     "time"
+    "os"
     "github.com/gravitl/netmaker/config"
     "github.com/gravitl/netmaker/config"
     "github.com/gravitl/netmaker/models"
     "github.com/gravitl/netmaker/models"
     "github.com/dgrijalva/jwt-go"
     "github.com/dgrijalva/jwt-go"
@@ -50,7 +51,7 @@ func CreateUserJWT(username string, isadmin bool) (response string, err error) {
 func VerifyUserToken(tokenString string) (username string, isadmin bool, err error) {
 func VerifyUserToken(tokenString string) (username string, isadmin bool, err error) {
     claims := &models.UserClaims{}
     claims := &models.UserClaims{}
 
 
-    if tokenString == config.Config.Server.MasterKey || os.Getenv("MASTER_KEY") {
+    if tokenString == config.Config.Server.MasterKey || (tokenString == os.Getenv("MASTER_KEY") && tokenString != "") {
         return "masteradministrator", true, nil
         return "masteradministrator", true, nil
     }
     }
 
 
@@ -70,7 +71,7 @@ func VerifyToken(tokenString string) (macaddress string, network string, err err
 
 
     //this may be a stupid way of serving up a master key
     //this may be a stupid way of serving up a master key
     //TODO: look into a different method. Encryption?
     //TODO: look into a different method. Encryption?
-    if tokenString == config.Config.Server.MasterKey || os.Getenv("MASTER_KEY") {
+    if tokenString == config.Config.Server.MasterKey || (tokenString == os.Getenv("MASTER_KEY") && tokenString != "") {
         return "mastermac", "", nil
         return "mastermac", "", nil
     }
     }
 
 

+ 1 - 0
main.go

@@ -38,6 +38,7 @@ var PortGRPC string
 //Start MongoDB Connection and start API Request Handler
 //Start MongoDB Connection and start API Request Handler
 func main() {
 func main() {
 
 
+
 	var clientmode string
 	var clientmode string
 	var defaultnet string
 	var defaultnet string
 	flag.StringVar(&clientmode, "clientmode", "on", "Have a client on the server")
 	flag.StringVar(&clientmode, "clientmode", "on", "Have a client on the server")