Skip to content

Commit

Permalink
Include JSON formatted RT in validator output (#179)
Browse files Browse the repository at this point in the history
* Include JSON formatted RT in validator output

* JwtUseEmailAsId
  • Loading branch information
irees authored Oct 9, 2023
1 parent 92330a1 commit a97d995
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 10 deletions.
13 changes: 12 additions & 1 deletion actions/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func ValidateUpload(ctx context.Context, cfg config.Config, src io.Reader, feedU
}
}
rturls = rturlsok
if len(rturls) > 3 {
rturls = rturls[0:3]
}
if feedURL == nil || !checkurl(*feedURL) {
feedURL = nil
}
Expand Down Expand Up @@ -74,7 +77,9 @@ func ValidateUpload(ctx context.Context, cfg config.Config, src io.Reader, feedU
IncludeServiceLevels: true,
IncludeRouteGeometries: true,
IncludeEntities: true,
IncludeEntitiesLimit: 10000,
IncludeRealtimeJson: true,
IncludeEntitiesLimit: 10_000,
MaxRTMessageSize: 10_000_000,
ValidateRealtimeMessages: rturls,
}
if cfg.ValidateLargeFiles {
Expand Down Expand Up @@ -164,6 +169,12 @@ func ValidateUpload(ctx context.Context, cfg config.Config, src io.Reader, feedU
for _, v := range r.Stops {
result.Stops = append(result.Stops, model.Stop{Stop: v})
}
for _, v := range r.Realtime {
result.Realtime = append(result.Realtime, model.ValidationRealtimeResult{
Url: v.Url,
Json: v.Json,
})
}
return &result, nil
}

Expand Down
19 changes: 14 additions & 5 deletions auth/ancheck/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

// JWTMiddleware checks and pulls user information from JWT in Authorization header.
func JWTMiddleware(jwtAudience string, jwtIssuer string, pubKeyPath string) (func(http.Handler) http.Handler, error) {
func JWTMiddleware(jwtAudience string, jwtIssuer string, pubKeyPath string, useEmailAsId bool) (func(http.Handler) http.Handler, error) {
var verifyKey *rsa.PublicKey
verifyBytes, err := ioutil.ReadFile(pubKeyPath)
if err != nil {
Expand All @@ -27,12 +27,22 @@ func JWTMiddleware(jwtAudience string, jwtIssuer string, pubKeyPath string) (fun
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tokenString := strings.Split(r.Header.Get("Authorization"), "Bearer "); len(tokenString) == 2 {
jwtUser, err := validateJwt(verifyKey, jwtAudience, jwtIssuer, tokenString[1])
claims, err := validateJwt(verifyKey, jwtAudience, jwtIssuer, tokenString[1])
if err != nil {
log.Error().Err(err).Msgf("invalid jwt token")
http.Error(w, util.MakeJsonError(http.StatusText(http.StatusUnauthorized)), http.StatusUnauthorized)
return
}
if claims == nil {
log.Error().Err(err).Msgf("no claims")
http.Error(w, util.MakeJsonError(http.StatusText(http.StatusUnauthorized)), http.StatusUnauthorized)
return
}
userId := claims.Subject
if useEmailAsId {
userId = claims.Email
}
jwtUser := authn.NewCtxUser(userId, claims.Subject, claims.Email)
r = r.WithContext(authn.WithUser(r.Context(), jwtUser))
}
next.ServeHTTP(w, r)
Expand All @@ -49,7 +59,7 @@ func (c *CustomClaimsExample) Valid() error {
return nil
}

func validateJwt(rsaPublicKey *rsa.PublicKey, jwtAudience string, jwtIssuer string, tokenString string) (authn.User, error) {
func validateJwt(rsaPublicKey *rsa.PublicKey, jwtAudience string, jwtIssuer string, tokenString string) (*CustomClaimsExample, error) {
// Parse the token
token, err := jwt.ParseWithClaims(tokenString, &CustomClaimsExample{}, func(token *jwt.Token) (interface{}, error) {
return rsaPublicKey, nil
Expand All @@ -64,6 +74,5 @@ func validateJwt(rsaPublicKey *rsa.PublicKey, jwtAudience string, jwtIssuer stri
if !claims.VerifyIssuer(jwtIssuer, true) {
return nil, errors.New("invalid issuer")
}
user := authn.NewCtxUser(claims.Email, claims.Subject, claims.Email)
return user, nil
return claims, nil
}
3 changes: 2 additions & 1 deletion auth/ancheck/mw.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type AuthConfig struct {
JwtAudience string
JwtIssuer string
JwtPublicKeyFile string
JwtUseEmailAsId bool
UserHeader string
}

Expand All @@ -32,7 +33,7 @@ func GetUserMiddleware(authType string, cfg AuthConfig, client *redis.Client) (M
case "user":
return UserDefaultMiddleware(cfg.DefaultUsername), nil
case "jwt":
return JWTMiddleware(cfg.JwtAudience, cfg.JwtIssuer, cfg.JwtPublicKeyFile)
return JWTMiddleware(cfg.JwtAudience, cfg.JwtIssuer, cfg.JwtPublicKeyFile, cfg.JwtUseEmailAsId)
case "header":
return UserHeaderMiddleware(cfg.UserHeader)
case "kong":
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551
github.com/graph-gophers/dataloader/v7 v7.1.0
github.com/hypirion/go-filecache v0.0.0-20160810125507-e3e6ef6981f0
github.com/interline-io/transitland-lib v0.12.1-0.20230707231304-e93d39aa84b9
github.com/interline-io/transitland-lib v0.13.1-0.20231009233939-3f7b51b007d9
github.com/jellydator/ttlcache/v2 v2.11.1
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.7
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,10 @@ github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6
github.com/iancoleman/orderedmap v0.2.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/interline-io/transitland-lib v0.12.1-0.20230707231304-e93d39aa84b9 h1:h9rkgNVrOfxdAQ6/3CL+dqU6sMCX5AdmsqccvEtG3kc=
github.com/interline-io/transitland-lib v0.12.1-0.20230707231304-e93d39aa84b9/go.mod h1:EnN1BuWqBAQzyIBOH+Ait7S8i4uC5fLqGzmX8FmQ2a8=
github.com/interline-io/transitland-lib v0.13.1-0.20231007011544-9b1f7cba6634 h1:ICaVmw8XTl9Idpzabg3HtwHASkGlhRdDlqDQHmKk4gU=
github.com/interline-io/transitland-lib v0.13.1-0.20231007011544-9b1f7cba6634/go.mod h1:EnN1BuWqBAQzyIBOH+Ait7S8i4uC5fLqGzmX8FmQ2a8=
github.com/interline-io/transitland-lib v0.13.1-0.20231009233939-3f7b51b007d9 h1:gRAYhdeSN7GsDLSZvKRMNzpmAdulMKMQz8T4LxbFFps=
github.com/interline-io/transitland-lib v0.13.1-0.20231009233939-3f7b51b007d9/go.mod h1:EnN1BuWqBAQzyIBOH+Ait7S8i4uC5fLqGzmX8FmQ2a8=
github.com/jarcoal/httpmock v1.3.0 h1:2RJ8GP0IIaWwcC9Fp2BmVi8Kog3v2Hn7VXM3fTd+nuc=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag=
Expand Down
Loading

0 comments on commit a97d995

Please sign in to comment.