Skip to content

Commit

Permalink
Merge pull request #771 from gravitl/v0.10.0
Browse files Browse the repository at this point in the history
V0.10.0
  • Loading branch information
0xdcarns authored Feb 15, 2022
2 parents 51e2bbe + 3d4f44e commit 21abd76
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
23 changes: 22 additions & 1 deletion logic/jwts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@ package logic

import (
"errors"
"fmt"
"time"

"github.com/golang-jwt/jwt/v4"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/servercfg"
)

var jwtSecretKey = []byte("(BytesOverTheWire)")
var jwtSecretKey []byte

// SetJWTSecret - sets the jwt secret on server startup
func SetJWTSecret() {
currentSecret, jwtErr := FetchJWTSecret()
if jwtErr != nil {
jwtSecretKey = []byte(RandomString(64)) // 512 bit random password
if err := StoreJWTSecret(string(jwtSecretKey)); err != nil {
logger.FatalLog("something went wrong when configuring JWT authentication")
}
} else {
jwtSecretKey = []byte(currentSecret)
}
}

// CreateJWT func will used to create the JWT while signing in and signing out
func CreateJWT(uuid string, macAddress string, network string) (response string, err error) {
Expand All @@ -19,6 +34,9 @@ func CreateJWT(uuid string, macAddress string, network string) (response string,
Network: network,
MacAddress: macAddress,
StandardClaims: jwt.StandardClaims{
Issuer: "Netmaker",
Subject: fmt.Sprintf("node|%s", uuid),
IssuedAt: time.Now().Unix(),
ExpiresAt: expirationTime.Unix(),
},
}
Expand All @@ -39,6 +57,9 @@ func CreateUserJWT(username string, networks []string, isadmin bool) (response s
Networks: networks,
IsAdmin: isadmin,
StandardClaims: jwt.StandardClaims{
Issuer: "Netmaker",
IssuedAt: time.Now().Unix(),
Subject: fmt.Sprintf("user|%s", username),
ExpiresAt: expirationTime.Unix(),
},
}
Expand Down
29 changes: 29 additions & 0 deletions logic/serverconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,32 @@ func FetchPrivKey(serverID string) (string, error) {
func RemovePrivKey(serverID string) error {
return database.DeleteRecord(database.SERVERCONF_TABLE_NAME, serverID)
}

// FetchJWTSecret - fetches jwt secret from db
func FetchJWTSecret() (string, error) {
var dbData string
var err error
var fetchedData = serverData{}
dbData, err = database.FetchRecord(database.SERVERCONF_TABLE_NAME, "nm-jwt-secret")
if err != nil {
return "", err
}
err = json.Unmarshal([]byte(dbData), &fetchedData)
if err != nil {
return "", err
}
return fetchedData.PrivateKey, nil
}

// StoreJWTSecret - stores server jwt secret if needed
func StoreJWTSecret(privateKey string) error {
var newData = serverData{}
var err error
var data []byte
newData.PrivateKey = privateKey
data, err = json.Marshal(&newData)
if err != nil {
return err
}
return database.Insert("nm-jwt-secret", string(data), database.SERVERCONF_TABLE_NAME)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func main() {

func initialize() { // Client Mode Prereq Check
var err error

if servercfg.GetNodeID() == "" {
logger.FatalLog("error: must set NODE_ID, currently blank")
}
Expand All @@ -49,6 +48,7 @@ func initialize() { // Client Mode Prereq Check
logger.FatalLog("Error connecting to database")
}
logger.Log(0, "database successfully connected")
logic.SetJWTSecret()

err = logic.TimerCheckpoint()
if err != nil {
Expand Down

0 comments on commit 21abd76

Please sign in to comment.