Skip to content

Commit

Permalink
added jwt generator.
Browse files Browse the repository at this point in the history
Many thanks to Omri Baso and Fabien Aunay for the report! Great job guys!
  • Loading branch information
adubovikov committed Jan 7, 2022
1 parent 708f4c1 commit 7f92f3a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion auth/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/golang-jwt/jwt"
"github.com/sipcapture/homer-app/config"
"github.com/sipcapture/homer-app/model"
"github.com/sipcapture/homer-app/utils/logger"
)
Expand Down Expand Up @@ -49,7 +50,7 @@ func Token(user model.TableUser) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

// Generate encoded token and send it as response.
t, err := token.SignedString([]byte(JwtSecret))
t, err := token.SignedString([]byte(config.Setting.AUTH_SETTINGS.JwtSecret))
if err != nil {
return "", err
}
Expand Down
2 changes: 0 additions & 2 deletions auth/constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package auth

const JwtSecret = "167f0db2-f83e-4baa-9736-d56064a5b415"

/* our expire time */
var TokenExpiryTime = 1200

Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type HomerSettingServer struct {
ExternalHomeDashboard string `default:""`
}

AUTH_SETTINGS struct {
JwtSecret string `default:""`
}

OAUTH2_SETTINGS struct {
Enable bool `default:"false"`
ClientID string `default:"1234565"`
Expand Down
1 change: 1 addition & 0 deletions etc/webapp_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"auth_settings": {
"_comment": "The type param can be internal, ldap, http_auth",
"type": "internal",
"jwt_secret": "167f0db2-f83e-4baa-9736-d56064a5b415",
"gravatar": false,
"gravatar_url": "https://www.gravatar.com/avatar/%s.jpg",
"token_expire": 1200,
Expand Down
27 changes: 26 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ type CommandLineFlags struct {
APIPrefix *string `json:"api_prefix"`
WatchConfig *bool `json:"watch_config"`
ShowCurrentConfig *bool `json:"show_current_config"`
GenerateJwtSecret *bool `json:"generate_jwt_secret"`
}

//params for Services
Expand Down Expand Up @@ -195,6 +196,9 @@ func initFlags() {
appFlags.WatchConfig = flag.Bool("watch-config", false, "Watch the configuration for changes")
appFlags.ShowCurrentConfig = flag.Bool("show-current-config", false, "print out the current config and exit")

//Jwt
appFlags.GenerateJwtSecret = flag.Bool("generate-jwt-secret", false, "generate jwt secret")

flag.Parse()
}

Expand Down Expand Up @@ -249,6 +253,19 @@ func main() {
os.Exit(0)
}

if *appFlags.GenerateJwtSecret {
logger.Info("Generating jwt secret...")
config.Setting.AUTH_SETTINGS.JwtSecret = uuid.NewV4().String()
viper.Set("auth_settings.jwt_secret", config.Setting.AUTH_SETTINGS.JwtSecret)
err := viper.WriteConfig()
if err != nil {
fmt.Println("No configuration file loaded: ", err)
logger.Error("No configuration file loaded - using defaults")
}

os.Exit(0)
}

//http client
initHttpClient()

Expand Down Expand Up @@ -867,7 +884,7 @@ func performV1APIRouting(e *echo.Echo) {
// Configure middleware with the custom claims type
config := middleware.JWTConfig{
Claims: &auth.JwtUserClaim{},
SigningKey: []byte(auth.JwtSecret),
SigningKey: []byte(config.Setting.AUTH_SETTINGS.JwtSecret),
}

res.Use(middleware.JWTWithConfig(config))
Expand Down Expand Up @@ -1165,6 +1182,14 @@ func updateVersionApplication(configDBSession *gorm.DB) bool {
saveConfig = true
}

//generate JWT
config.Setting.AUTH_SETTINGS.JwtSecret = viper.GetString("auth_settings.jwt_secret")
if config.Setting.AUTH_SETTINGS.JwtSecret == "" {
config.Setting.AUTH_SETTINGS.JwtSecret = uuid.NewV4().String()
viper.Set("auth_settings.jwt_secret", config.Setting.AUTH_SETTINGS.JwtSecret)
saveConfig = true
}

if saveConfig {
err := viper.WriteConfig()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

//VERSION
var VERSION_APPLICATION = "1.4.27"
var VERSION_APPLICATION = "1.4.28"

//NAME
var NAME_APPLICATION = "homer-app"
Expand Down

0 comments on commit 7f92f3a

Please sign in to comment.