Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load private key from init #20

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,8 @@
}

func newServer(userSvc handlers.UserService, patSvc handlers.PersonalAccessTokenService, handler ...handlers.Handler) *server.Server {
key, err := jwt.LoadPrivateKey()
if err != nil {
log.Panic().Err(err).Msg("failed loading private key")
}

jwtConfig := jwtMw.Config{
Key: key,
Key: jwt.PrivateKey,
UseRefreshToken: true,
ExemptRoutes: map[string][]string{
"/": {http.MethodGet},
Expand All @@ -90,7 +85,6 @@
"/openapi/*": {http.MethodGet},
"/auth/login": {http.MethodPost},
"/auth/signup": {http.MethodPost},
"/google": {http.MethodGet},
"/oauth2/google/callback": {http.MethodGet},
"/oauth2/google/login": {http.MethodGet},
},
Expand Down Expand Up @@ -161,6 +155,7 @@
}
}

// set token_id globally
log.Logger = log.Logger.With().Str("token_id", t.Subject()).Logger()

return nil
Expand All @@ -169,7 +164,7 @@

enforcer, err := casbin.NewEnforcer(viper.GetString(config.CasbinModel), viper.GetString(config.CasbinPolicy))
if err != nil {
panic(err)
log.Panic().Err(err).Msg("failed creating enforcer")

Check warning on line 167 in server/server.go

View check run for this annotation

Codecov / codecov/patch

server/server.go#L167

Added line #L167 was not covered by tests
}

openAPIConfig := openapiMw.Config{
Expand All @@ -180,7 +175,6 @@
"/livez": {http.MethodGet},
"/docs": {http.MethodGet},
"/openapi/*": {http.MethodGet},
"/google": {http.MethodGet},
"/oauth2/google/callback": {http.MethodGet},
"/oauth2/google/login": {http.MethodGet},
},
Expand Down
29 changes: 16 additions & 13 deletions util/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
"github.com/alexferl/echo-boilerplate/config"
)

var PrivateKey *rsa.PrivateKey = nil

func init() {
c := config.New()
c.BindFlags()

key, err := loadPrivateKey()
if err != nil {
panic(err)

Check warning on line 27 in util/jwt/jwt.go

View check run for this annotation

Codecov / codecov/patch

util/jwt/jwt.go#L27

Added line #L27 was not covered by tests
}
PrivateKey = key
}

type Type int8

const (
Expand Down Expand Up @@ -57,11 +70,6 @@
}

func generateToken(typ Type, expiry time.Duration, sub string, claims map[string]any) ([]byte, error) {
key, err := LoadPrivateKey()
if err != nil {
return nil, err
}

builder := jwx.NewBuilder().
Subject(sub).
Issuer(viper.GetString(config.JWTIssuer)).
Expand All @@ -81,7 +89,7 @@
return nil, fmt.Errorf("failed to build %s token: %v\n", typ.String(), err)
}

signed, err := jwx.Sign(token, jwx.WithKey(jwa.RS256, key))
signed, err := jwx.Sign(token, jwx.WithKey(jwa.RS256, PrivateKey))
if err != nil {
return nil, fmt.Errorf("failed to sign %s token: %v\n", typ.String(), err)
}
Expand All @@ -90,20 +98,15 @@
}

func ParseEncoded(encodedToken []byte) (jwx.Token, error) {
key, err := LoadPrivateKey()
if err != nil {
return nil, err
}

token, err := jwx.Parse(encodedToken, jwx.WithValidate(true), jwx.WithKey(jwa.RS256, key))
token, err := jwx.Parse(encodedToken, jwx.WithValidate(true), jwx.WithKey(jwa.RS256, PrivateKey))
if err != nil {
return nil, err
}

return token, nil
}

func LoadPrivateKey() (*rsa.PrivateKey, error) {
func loadPrivateKey() (*rsa.PrivateKey, error) {
f, err := os.Open(viper.GetString(config.JWTPrivateKey))
if err != nil {
return nil, fmt.Errorf("failed to open private key: %v", err)
Expand Down
Loading