Skip to content

Commit

Permalink
feat(config): integrate JWT secret into AppConfig and adjust config p…
Browse files Browse the repository at this point in the history
…aths

- Update 'config' package to set the config path to the current directory.
- Refactor 'middlewares' to use JWT secret from 'config.GatekeeperConfig'.
- Enhance 'models.AppConfig' to include 'JWTSecret'.
- Add JWT secret key to the YAML configuration file.
  • Loading branch information
atanurdemir committed Jan 25, 2024
1 parent 83cfcd9 commit b66f5c4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ By configuring and customizing these gates, you can fine-tune how Gatekeeper han

1. **Setting up the configuration file:**

Modify the `gates.yaml` file in the src/config directory to define your access control rules. Here's an example configuration:
Modify the `settings.yaml` file in the root directory to define your access control rules. Here's an example configuration:

```yaml
gates:
Expand Down
1 change: 1 addition & 0 deletions src/config/gates.yaml → settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ gates:
path: "/post"
limit: 5
duration: 30
jwt_secret: "your-secret-key"
4 changes: 2 additions & 2 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
var GatekeeperConfig models.AppConfig

func SetupConfig() {
viper.SetConfigName("gates")
viper.SetConfigName("settings")
viper.SetConfigType("yaml")
viper.AddConfigPath("src/config")
viper.AddConfigPath(".")

if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion src/middlewares/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"context"
"net/http"

"github.com/atanurdemir/gatekeeper/src/config"
"github.com/atanurdemir/gatekeeper/src/types"
"github.com/golang-jwt/jwt/v5"
)

func AuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
secretKey := "your-secret-key"
secretKey := config.GatekeeperConfig.JWTSecret
accessToken := r.Header.Get("Authorization")
if accessToken == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
Expand Down
13 changes: 7 additions & 6 deletions src/models/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package models

type AppConfig struct {
Gates []Gate `yaml:"gates"`
JWTSecret string `mapstructure:"jwt_secret"`
Gates []Gate `mapstructure:"gates"`
}

type Gate struct {
Path string `yaml:"path"`
Method string `yaml:"method"`
Rules []Rule `yaml:"rules"`
Path string `mapstructure:"path"`
Method string `mapstructure:"method"`
Rules []Rule `mapstructure:"rules"`
}

type Rule struct {
Name string `yaml:"name"`
Config interface{} `yaml:"config"`
Name string `mapstructure:"name"`
Config interface{} `mapstructure:"config"`
}

0 comments on commit b66f5c4

Please sign in to comment.