Home > Enterprise >  lua if condition with "does not contain" sting
lua if condition with "does not contain" sting

Time:11-04

I have a nginx configuration where with Lua code I want to block all domain except one(example.com). Since I am using Azure, I could not get just domain value from the IDToken so I am using emails([email protected] since email also contains domain name).

Requirement is to block all access if the user email does not contain the string example.com This is what I have so far but it does not have does not contain string

if res.id_token.email ~= 'example.com' then
  ngx.exit(ngx.HTTP_FORBIDDEN)
end

it blocks all emails with example.com

since email can be anything and its a string that should use "CONTAINS", so its failing. What will be the solution for if condition with does not contain example.com

CodePudding user response:

This is the length of @example.com

#'@example.com'

Get the suffix of email with the length of @example.com

res.id_token.email:sub(-#'@example.com')

Compare with @example.com

res.id_token.email:sub(-#'@example.com') ~= '@example.com'

CodePudding user response:

The following worked:

if not string.match(res.id_token.email, "@example.com") then
    ngx.exit(ngx.HTTP_FORBIDDEN)
end

Found that string.match can find a string to match and I have added a not before it.

  • Related