<commit_before>package ufront.web.result;

import ufront.web.result.ActionResult;
import ufront.web.context.*;
import ufront.web.HttpError;
import tink.CoreApi;
import ufront.core.Sync;


/**
	A result that requires a certain username and password to be provided before executing an action.
**/
class HttpAuthResult extends ActionResult {

	var message:String;
	var failureMessage:String;

	/**
		@param username The expected username.
		@param password The expected password.
		@param message (optional) The message to show in the popup box. Default is "Please login";
		@param failureMessage (optional) The HTML to show in the browser if the login is cancelled. Default is to re-use `message`.s
		@param fn The function to execute if authentication is correct. Must return a `FutureActionOutcome`, such as from using `executeSubController( AdminController )`.
		@return A FutureActionOutcome, either the result of `fn()` or a `HttpAuthResult` that displays a login box.
	**/
	public static function require( context:HttpContext, username:String, password:String, ?message:String, ?failureMessage:String, fn:Void->FutureActionOutcome ):FutureActionOutcome {
		var auth = context.request.authorization;
		if ( auth!=null && auth.user==username && auth.pass==password ) {
			return fn();
		}
		else {
			var result:ActionResult = new HttpAuthResult(message,failureMessage);
			return Future.sync( Success(result) );
		}
	}

	function new( message:String, failureMessage:Null<String> ) {
		this.message = message;
		this.failureMessage = failureMessage!=null ? failureMessage : message;
	}

	override public function executeResult( actionContext:ActionContext ) {
		actionContext.httpContext.response.requireAuthentication( message );
		actionContext.httpContext.response.write( failureMessage );
		return Sync.success();
	}
}<commit_msg>Rename fn to successFn for clarity<commit_after>package ufront.web.result;

import ufront.web.result.ActionResult;
import ufront.web.context.*;
import ufront.web.HttpError;
import tink.CoreApi;
import ufront.core.Sync;


/**
	A result that requires a certain username and password to be provided before executing an action.
**/
class HttpAuthResult extends ActionResult {

	var message:String;
	var failureMessage:String;

	/**
		@param username The expected username.
		@param password The expected password.
		@param message (optional) The message to show in the popup box. Default is "Please login";
		@param failureMessage (optional) The HTML to show in the browser if the login is cancelled. Default is to re-use `message`.s
		@param successFn The function to execute if authentication is correct. Must return a `FutureActionOutcome`, such as from using `executeSubController( AdminController )`.
		@return A FutureActionOutcome, either the result of `fn()` or a `HttpAuthResult` that displays a login box.
	**/
	public static function requireAuth( context:HttpContext, username:String, password:String, ?message:String, ?failureMessage:String, successFn:Void->FutureActionOutcome ):FutureActionOutcome {
		var auth = context.request.authorization;
		if ( auth!=null && auth.user==username && auth.pass==password ) {
			return successFn();
		}
		else {
			var result:ActionResult = new HttpAuthResult(message,failureMessage);
			return Future.sync( Success(result) );
		}
	}

	function new( message:String, failureMessage:Null<String> ) {
		this.message = message;
		this.failureMessage = failureMessage!=null ? failureMessage : message;
	}

	override public function executeResult( actionContext:ActionContext ) {
		actionContext.httpContext.response.requireAuthentication( message );
		actionContext.httpContext.response.write( failureMessage );
		return Sync.success();
	}
}
{'fraction_non_alphanumeric': 0.09521247982786445, 'fraction_numerical': 0.0, 'mean_word_length': 5.177740863787376, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4138917', 'n_tokens_mistral': 1048, 'n_tokens_neox': 992, 'n_words': 337}