Skip to content

Commit

Permalink
Merge pull request #20 from alexferl/init_private_key
Browse files Browse the repository at this point in the history
load private key from init
  • Loading branch information
alexferl authored Mar 14, 2024
2 parents 43feec3 + 4ce22fe commit 4a39e29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
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 NewTestServer(userSvc handlers.UserService, patSvc handlers.PersonalAccessT
}

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 @@ func newServer(userSvc handlers.UserService, patSvc handlers.PersonalAccessToken
"/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 @@ func newServer(userSvc handlers.UserService, patSvc handlers.PersonalAccessToken
}
}

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

return nil
Expand All @@ -169,7 +164,7 @@ func newServer(userSvc handlers.UserService, patSvc handlers.PersonalAccessToken

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")
}

openAPIConfig := openapiMw.Config{
Expand All @@ -180,7 +175,6 @@ func newServer(userSvc handlers.UserService, patSvc handlers.PersonalAccessToken
"/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 @@ import (
"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)
}
PrivateKey = key
}

type Type int8

const (
Expand Down Expand Up @@ -57,11 +70,6 @@ func GeneratePersonalToken(sub string, expiry time.Duration, claims map[string]a
}

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 @@ func generateToken(typ Type, expiry time.Duration, sub string, claims map[string
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 generateToken(typ Type, expiry time.Duration, sub string, claims map[string
}

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

0 comments on commit 4a39e29

Please sign in to comment.