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

jwt validation #171

Closed

Conversation

abel296
Copy link
Contributor

@abel296 abel296 commented Jan 5, 2024

Features:

  • Provide the system with the possibility of validating jwt
  • Jwt config: validation included paths, allowed scopes and jwks url config
  • Jwt config can be set per domain or in common config

@abel296 abel296 force-pushed the feature/jwt-validation branch from 6dd2956 to f205092 Compare January 5, 2024 13:21
@monwolf monwolf mentioned this pull request Jan 8, 2024
@abel296 abel296 force-pushed the feature/jwt-validation branch 6 times, most recently from 31b1471 to 502345e Compare January 8, 2024 14:41
@abel296
Copy link
Contributor Author

abel296 commented Jan 8, 2024

Hi @fabiocicerchia , I don't have access to see why the snyk test is not passing, can you give me access permit to check it out please?

@abel296 abel296 force-pushed the feature/jwt-validation branch from 502345e to 6bafe05 Compare January 9, 2024 07:35
@juansm90 juansm90 force-pushed the feature/jwt-validation branch from 6bafe05 to 291df62 Compare January 9, 2024 09:56
@abel296 abel296 force-pushed the feature/jwt-validation branch 4 times, most recently from a8bd311 to 8acec37 Compare January 11, 2024 10:57
@fabiocicerchia
Copy link
Owner

Hi @abel296, thank you for the PR.
I'll try to sort out the accesses to the project, possibly make it available publicly, in the meantime here are the errors raised by snyk:

@abel296 abel296 force-pushed the feature/jwt-validation branch 3 times, most recently from f326f4a to 7b4c1a4 Compare January 15, 2024 09:05
@abel296
Copy link
Contributor Author

abel296 commented Jan 15, 2024

Hi @fabiocicerchia , could you review the PR please?

config/config.go Outdated Show resolved Hide resolved
config/model.go Outdated Show resolved Hide resolved
server/jwt/jwt.go Outdated Show resolved Hide resolved
server/jwt/jwt.go Outdated Show resolved Hide resolved
server/jwt/jwt.go Outdated Show resolved Hide resolved
server/jwt/jwt_test.go Outdated Show resolved Hide resolved
server/jwt/jwt_utils.go Outdated Show resolved Hide resolved
@abel296 abel296 force-pushed the feature/jwt-validation branch from 7b4c1a4 to c398bcb Compare January 18, 2024 12:02
config/model.go Outdated Show resolved Hide resolved
server/jwt/jwt_functional_test.go Outdated Show resolved Hide resolved
server/jwt/jwt.go Outdated Show resolved Hide resolved
@abel296 abel296 force-pushed the feature/jwt-validation branch 5 times, most recently from ff0bc17 to 0b3995e Compare January 29, 2024 10:27
@abel296 abel296 force-pushed the feature/jwt-validation branch from dc4cc31 to dcf1a34 Compare February 21, 2024 09:26
@abel296 abel296 force-pushed the feature/jwt-validation branch 19 times, most recently from 23c7218 to 4e87b96 Compare March 12, 2024 13:45
@abel296
Copy link
Contributor Author

abel296 commented Mar 12, 2024

Hi @fabiocicerchia , these are the last updates:

  • excluded paths instead of included paths in JWT config
  • regex compatibility with the excluded paths config for excluding dynamic paths
  • improved JWT init and JWT handler
  • improved testing nginx dockerfile to avoid compatibility issues with otel_ngx_module.so
  • migration from github.com/lestrrat-go/jwx to github.com/lestrrat-go/jwx/v2
  • update dependencies

Signed-off-by: Abel Andrés <[email protected]>
@abel296 abel296 force-pushed the feature/jwt-validation branch from 4e87b96 to c12a5ce Compare March 15, 2024 11:55
Copy link
Owner

@fabiocicerchia fabiocicerchia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @abel296, apologies for not getting back to you sooner, recently I couldn't focus on the project properly.
I'll try spending some more time to test properly this MR in the next following days.

It would be really good not to push force and have a reviewable commit history, because as of now, it's really impossible to figure out what each change contributed to.

I made some more suggestions, but let me test it first before starting fixing, as I might add some more comments, and don't want to let you do too much back and forth.

Thank you for the patience and the effort for putting together this PR!

# - JWT_JWKS_URL enviroment variable

# Time in minutes that takes for JWKS to refresh automatically
# JWT_REFRESH_INTERVAL=
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the same example values from config.yml.dist:

Suggested change
# JWT_REFRESH_INTERVAL=
# JWT_REFRESH_INTERVAL=15


# --- JWT
# A list of space-separated paths.
# JWT_EXCLUDED_PATHS=
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the same example values from config.yml.dist:

Suggested change
# JWT_EXCLUDED_PATHS=
# JWT_EXCLUDED_PATHS=/

# JWT_EXCLUDED_PATHS=

# A list of space-separated scopes to be allowed.
# JWT_ALLOWED_SCOPES=
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the same example values from config.yml.dist:

Suggested change
# JWT_ALLOWED_SCOPES=
# JWT_ALLOWED_SCOPES=scope1, scope2

# # A list of space-separated paths.
# excluded_paths:
# - /
# # A list of space-separated scopes to be allowed.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space or comma separated?

// Jwt - Defines the config for the jwt validation.
type Jwt struct {
ExcludedPaths []string `yaml:"excluded_paths" envconfig:"JWT_EXCLUDED_PATHS" split_words:"true"`
AllowedScopes []string `yaml:"allowed_scopes" envconfig:"JWT_ALLOWED_SCOPES" split_words:"true"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scope1, scope2 will end up in having item 0 scope1, and item 1 scope2


h.ServeHTTP(rr, req)

assert.Equal(t, http.StatusMovedPermanently, rr.Code)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be useful to assert the Location header of this 301 respose.


h.ServeHTTP(rr, req)

assert.Equal(t, http.StatusMovedPermanently, rr.Code)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be useful to assert the Location header of this 301 respose.


h.ServeHTTP(rr, req)

assert.Equal(t, http.StatusMovedPermanently, rr.Code)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it correct that all the tests with a JWT are going to 301?


res := getScopes(token)

assert.ElementsMatch(t, res, []string{"scope1", "scope2", "scope3"}, "Scopes provided doesn't match")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.ElementsMatch(t, res, []string{"scope1", "scope2", "scope3"}, "Scopes provided doesn't match")
assert.ElementsMatch(t, res, []string{"scope1", "scope2", "scope3"}, "The provided scopes don't match")


res := getScopes(token)

assert.ElementsMatch(t, res, []string{"scope1", "scope2", "scope3"}, "Scopes provided doesn't match")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.ElementsMatch(t, res, []string{"scope1", "scope2", "scope3"}, "Scopes provided doesn't match")
assert.ElementsMatch(t, res, []string{"scope1", "scope2", "scope3"}, "The provided scopes don't match")

return logJWTErrorAndAbort(w, err)
}
if err := jwt.Validate(token); err != nil {
return logJWTErrorAndAbort(w, err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be good to increase the test coverage by adding a test case to cover this scenario as well (ie testing the failure of validation).

}

func TestRefreshKeySet(t *testing.T) {
t.Skip("To run this test, you should set refreshIntervalDuration (from config.go) to time.Second")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this test be executed automatically, without any manual changes?

@fabiocicerchia
Copy link
Owner

@abel296 please have a look at some suggestions made in #208

@fabiocicerchia
Copy link
Owner

merged all changes + improvements in #208

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants