Skip to content

Commit

Permalink
feat: Add auth filter without subdomain validation (#78)
Browse files Browse the repository at this point in the history
feat: Add auth filter without subdomain validation
  • Loading branch information
thoriqsatriya authored Aug 15, 2024
1 parent b053376 commit dcfefeb
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion pkg/auth/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@ func FilterInitializationOptionsFromEnv() *FilterInitializationOptions {
//
// )
func (filter *Filter) Auth(opts ...FilterOption) restful.FilterFunction {
return filter.authFunc(false, opts...)
}

// AuthWithoutSubdomainValidation returns a filter that filters request with valid access token in auth header or cookie
// The difference with Auth() is this function won't check for subdomain validation even if it's active in configuration.
//
// The token's claims will be passed in the request.attributes["JWTClaims"] = *iam.JWTClaims{}
// This filter is expandable through FilterOption parameter
// Example:
// iam.AuthWithoutSubdomainValidation(
//
// WithValidUser(),
// WithPermission("ADMIN"),
//
// )
func (filter *Filter) AuthWithoutSubdomainValidation(opts ...FilterOption) restful.FilterFunction {
return filter.authFunc(true, opts...)
}

func (filter *Filter) authFunc(skipSubdomainValidation bool, opts ...FilterOption) restful.FilterFunction {
return func(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
token, tokenFrom, err := parseAccessToken(req)
if err != nil {
Expand Down Expand Up @@ -169,7 +189,7 @@ func (filter *Filter) Auth(opts ...FilterOption) restful.FilterFunction {
}
}

if filter.options.SubdomainValidationEnabled {
if filter.options.SubdomainValidationEnabled && !skipSubdomainValidation {
if valid := validateSubdomainAgainstNamespace(getHost(req.Request), claims.Namespace, filter.options.SubdomainValidationExcludedNamespaces); !valid {
logIfErr(resp.WriteHeaderAndJson(http.StatusNotFound, ErrorResponse{
ErrorCode: SubdomainMismatch,
Expand Down

0 comments on commit dcfefeb

Please sign in to comment.