Basic Authentication with aFleX

Quick and dirty way of authenticating users for specific URLs on a VIP.

\#\#\# START \#\#\#
when RULE\_INIT \{

# Set the REALM
  set ::REALM “Password Required”
  # List of URLs you need to authenticate for
  array set ::LISTURL {
   “/exchange” “1”
   “/exchange/” “1”
   “/sharepoint” “1”
   “/sharepoint/” “1”
  }
  # List of users (with passwords) that are allowed to authenticate
  array set ::DOTPASSWD {
   “randomuser1” “thiswillbeacleartextpassword”
   “randomuser2” “thiswillbeacleartextpassword”

  \}
\}
when HTTP\_REQUEST \{
  set URI [HTTP::uri]
  if \{ [info exists ::LISTURL($URI)] \} \{  
    if \{ [HTTP::header exists "Authorization"] \} \{
      set encoded\_header [HTTP::header "Authorization"]
      regexp -nocase \{Basic (.\*)\} $encoded\_header tmpmatch encoded\_string
      set decoded\_string [b64decode $encoded\_string]
      regexp -nocase \{(.\*):(.\*)\} $decoded\_string tmpmatch auth\_user auth\_passwd
      if \{ [info exists ::DOTPASSWD($auth\_user)] \} \{
        set stored\_passwd $::DOTPASSWD($auth\_user)
        if \{ $auth\_passwd ne $stored\_passwd \} \{
          HTTP::respond 401 WWW-Authenticate "Basic realm=\\"$::REALM\\""
        \}
      \} else \{
        HTTP::respond 401 WWW-Authenticate "Basic realm=\\"$::REALM\\""
      \}
    \} else \{
      HTTP::respond 401 WWW-Authenticate "Basic realm=\\"$::REALM\\""
    \}
  \}
\}
\#\#\# END \#\#\#