<commit_before>@prefix sys "http://www.genyris.org/lang/system#"

def authenticate(request)
   var headers (request(.getHeaders))
   cond
      (not (headers (.hasKey 'authorization'))) nil
      (equal? (headers(.lookup 'authorization')) 'Basic Zm9vOmJhcg==') true
      else nil

df httpd-serve (request)
   print request
   cond
      (authenticate request)
         list 200 'text/html'
            template
                html()
                   head()
                      title() "Basic Auth demo"
                   body()
                      p() "Authorized"
                      ,(request (.toHTML))
      else
           login request

def login(request)
   var response
      list 401 ^(('Content-Type' = "text/plain")('WWW-Authenticate' = 'Basic realm="Secure Area"')) "Login Required"
   print response
   response
<commit_msg>Updated basic auth example.<commit_after>@prefix sys "http://www.genyris.org/lang/system#"

var *user* 'foo'
var *password* '<PASSWORD>'

def authenticate(request)
   #  Check the username and password
   var userpass (request (.getBasicUsernamePassword))
   cond
        (and (equal? userpass!username *user*) (equal? userpass!password *password*)) 
           userpass
        else nil

df httpd-serve (request)
   # Serve web page, catch an errors and report with backtrace.
   var response nil
   catch errors
        response = (raw-serve request)
   cond
       (not errors) response
       else
           backtrace request errors

def backtrace (request errors)
   # Error page
   var bt (sys:backtrace)
   list 500 'text/html'
       template
            html()
               head()
                      title() "Internal error"
               body()
                      p() "*** Error - " ,errors
                      p() ,bt
                      div() ,(request (.toHTML))

def raw-serve (request)
   # basic page
   var userpass (authenticate request)
   cond
      userpass
         echo request userpass!username
      else
           login request

def echo(request username)
   # success page
   list 200 'text/html'
            template
                html()
                   head()
                      title() "Basic Auth demo"
                   body()
                      p() ,('Welcome %s, you are authorized.' (.format username))
                      ,(request (.toHTML))

def login(request)
   # Needs auth page
   var response
      list 401 ^(('Content-Type' = "text/html")('WWW-Authenticate' = 'Basic realm="Secure Area"')) "401 Login Required"
   response

# Run using:
# var server (httpd 80 'examples/www-basic-auth.g')
#
# web:get 'http://localhost/' (list (cons 'authorization' ('Basic %a' (.format ('foo:bar'(.toBase64))))))
#
{'fraction_non_alphanumeric': 0.11397058823529412, 'fraction_numerical': 0.008455882352941176, 'mean_word_length': 1.8977635782747604, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2195153', 'n_tokens_mistral': 791, 'n_tokens_neox': 737, 'n_words': 231}