<cfcomponent
output="false"
hint="I am a remote-access testing component.">
<cfset THIS.Credentials = {
Username = "Molly",
Password = "hottie"
} />
<cfset THIS.CheckAuthentication() />
<cffunction
name="Test"
access="remote"
returntype="string"
returnformat="json"
output="false"
hint="I am a remote-access test method.">
<cfreturn "Method access successful!" />
</cffunction>
<cffunction
name="CheckAuthentication"
access="public"
returntype="void"
output="false"
hint="I check to see if the request is authenticated. If not, then I return a 401 Unauthorized header and abort the page request.">
<cfif NOT THIS.CheckAuthorization()>
<cfheader
statuscode="401"
statustext="Unauthorized"
/>
<cfheader
name="WWW-Authenticate"
value="basic realm=""API"""
/>
<cfabort />
</cfif>
<cfreturn />
</cffunction>
<cffunction
name="CheckAuthorization"
access="public"
returntype="boolean"
output="false"
hint="I check to see if the given request credentials match the required credentials.">
<cfset var LOCAL = {} />
<cftry>
<cfset LOCAL.EncodedCredentials = ListLast(
GetHTTPRequestData().Headers.Authorization,
" "
) />
<cfset LOCAL.Credentials = ToString(
ToBinary( LOCAL.EncodedCredentials )
) />
<cfset LOCAL.Username = ListFirst( LOCAL.Credentials, ":" ) />
<cfset LOCAL.Password = ListLast( LOCAL.Credentials, ":" ) />
<cfif (
(LOCAL.Username EQ THIS.Credentials.Username) AND
(LOCAL.Password EQ THIS.Credentials.Password)
)>
<cfreturn true />
<cfelse>
<cfreturn false />
</cfif>
<cfcatch>
<cfreturn false />
</cfcatch>
</cftry>
</cffunction>
</cfcomponent>