#!/bin/bash source exprash.sh; redirect_stdout 'log'; use_session use_body username='admin' password='password' # Authorization middleware function is_authorized() { if [ "$(session 'authorized')" != "1" ]; then next 'unauthorized' return 1 fi } get '/' && { if [ "$(session 'authorized')" == "1" ]; then html="

Welcome $username

" html+="Click Here For Secrets

" html+="Logout" else html="

Welcome

" html+="

You must login to learn secrets

" html+="Login" fi printf '%s\n' "$html" | send } get '/admin' && is_authorized && { html='

Here are all of my secrets:

' html+="" html+="Go Home" printf '%s' "$html" | send } get '/login' && { html="

Login:

" html+='
' html+=' ' html+=' ' html+=' ' html+='
' printf '%s\n' "$html" | send } get '/incorrect-password' && { if [ "$(session 'authorized')" == "1" ]; then redirect '.' else html="

Incorrect Password

" html+="

Try again:

" html+="Login" printf '%s\n' "$html" | send fi } post '/login' && { post_user=$(body 'username') post_pass=$(body 'password') if [ "$post_user" == "$username" ] && [ "$post_pass" == "$password" ]; then session 'authorized' 1 redirect '.' else redirect 'incorrect-password' fi } get '/logout' && { session 'authorized' 0 redirect '.' } (use || use_error) && { if [ "$(get_error_message)" == "unauthorized" ]; then html='

Error: Access Denied

' html+='Click here to login' printf '%s' "$html" | send else status '404' printf '

404

' | send fi }