<% Option Explicit %>
<!--#include virtual="/NR/Shared/ASPUtils.inc"-->
<%
'Authenticate.inc: Included in all template files.  Controls order and
'methods used to try to authenticate a user to view a requested item.

'WARNING: Changing this file will alter the behaviour of user access
'into the Resolution system.

Response.Buffer = True
'The following line can be uncommented if dynamic behaviour is important on every page. Pages will then be regenerated from server everytime they are requested. Please consider performance issue before uncommenting the line.
'Response.Expires = 0

Dim NORIGHTSTXT
Dim NOCOOKIETXT
Dim NOTVISIBLEURL
Dim AutoSession

NORIGHTSTXT = "You do not have rights to view the requested item."
NOCOOKIETXT = "Unable to login automatically.  Please ensure that cookies are enabled in your browser and try again."
NOTVISIBLEURL = "/NR/Shared/invalid.htm"

Function URLForThisPageRequest

	Dim queryString
	Dim GotoUrl

	GotoUrl = Request.QueryString("OriginalUrl")
	if GotoUrl = "" Then
		queryString = Request.ServerVariables("QUERY_STRING")

		If queryString <> "" Then
			queryString = "?" & queryString
		End If
	
		GotoUrl = Request.ServerVariables("SCRIPT_NAME") & queryString
	End If
	
	URLForThisPageRequest = GotoUrl
	
End Function

%>
<!--#include file="log.inc"-->
<%

WriteToLogFile( " " )
WriteToLogFile( "***********New entry***************" )
WriteToLogFile( " " )

WriteToLogFile( " ALL_RAW = " & Request.ServerVariables("ALL_RAW") )
WriteToLogFile( " AUTH_USER = " & Request.ServerVariables("AUTH_USER") )
WriteToLogFile( " AUTH_TYPE = " & Request.ServerVariables("AUTH_TYPE") )
WriteToLogFile( " AUTH_PASSWORD = " & Request.ServerVariables("AUTH_PASSWORD") )
WriteToLogFile( " HTTP_AUTHORIZATION = " & Request.ServerVariables("HTTP_AUTHORIZATION") )
WriteToLogFile( " SCRIPT_NAME = " & Request.ServerVariables("SCRIPT_NAME") )
WriteToLogFile( " QUERY_STRING = " & Request.ServerVariables("QUERY_STRING") )

set AutoSession = Server.CreateObject("ResolutionObjectModel.AutoSession3")

'Attempt to authenticate using token stored as a cookie or in query string.
If AutoSession.AuthenticateUsingToken Then
	WriteToLogFile( "<Authenticated using token>" )
	'Check if user has sufficient rights to browse the requested item.
	If Not AutoSession.UserHasRightToBrowse Then
		WriteToLogFile( "<No rights - attempting manual login>" )
		AutoSession.ManualLogin("(" + AutoSession.UserName + ") " + NORIGHTSTXT)
	ElseIf Not AutoSession.ThisIsVisible Then
		WriteToLogFile( "<not visible - redirecting to invalid.htm>" )
        ServerSideRedirect(NOTVISIBLEURL)
	End If

'If unsuccessful, and a token exists, then it is stale
ElseIf Request.Cookies("CMSAUTHTOKEN") <> "" Or _
	   Request.QueryString("CMSAUTHTOKEN") <> "" Then
			Autosession.ManualLogin("Your session has timed out.")
	
'If unsuccessful, try to authenticate using user info in query string or 
'Basic Authentication header.
ElseIf AutoSession.AuthenticateUsingUserInfo Then
	WriteToLogFile( "<Authenticated using user info>" )
	If Not AutoSession.UserHasRightToBrowse Then
		WriteToLogFile( "<No rights - attempting manual login>" )
		AutoSession.ManualLogin("(" + AutoSession.UserName + ") " + NORIGHTSTXT)
	ElseIf Not AutoSession.ThisIsVisible Then
		WriteToLogFile( "<not visible - redirecting to invalid.htm>" )
        ServerSideRedirect(NOTVISIBLEURL)
	End If

'If user info not passed and the request came from a web crawler, request basic authentication
'Change the token that identifies the web crawler for authenticate access. It can be either "ROBOT", "NCOMPASS' or "RESOLUTION"
Elseif InStr( UCASE(Request.ServerVariables("HTTP_USER_AGENT")), "RESOLUTION") > 0 Then
		WriteToLogFile( "<Web crawler access denied>" )
		Response.status = "401 unauthorized"
		Response.Write ("The user agent header indicated that this is a request " & _
					"from a web crawler, the web crawler must provide basic authentication.")
		Response.End

'If unsuccessful, try to log in as the default guest.
ElseIf AutoSession.AuthenticateAsDefaultGuest Then
	WriteToLogFile( "<Authenticated as default guest>" )
	If Not AutoSession.UserHasRightToBrowse Then
		If InStr(Request.QueryString, "SetCookie=True") <= 0 Then
			WriteToLogFile( "<No rights - attempting transparent login>" )
			AutoSession.AutoLogin
		Else
			WriteToLogFile( "<No rights - attempting manual login>" )
			AutoSession.ManualLogin("You are logged in as the Default Guest User.  " + NORIGHTSTXT + "  " + NOCOOKIETXT)
		End If
	ElseIf Not AutoSession.ThisIsVisible Then
		WriteToLogFile( "<not visible - redirecting to invalid.htm>" )
        ServerSideRedirect(NOTVISIBLEURL)
	End If

'Authentication using available info has failed, attempt transparent login.
'If transparent login has already been tried (setting the cookie failed) then
'try manual login.
Else
	If InStr(Request.QueryString, "SetCookie=True") <= 0 Then
		WriteToLogFile( "<attempting transparent login>" )
		AutoSession.AutoLogin
	Else
		WriteToLogFile( "<attempting manual login>" )
		AutoSession.ManualLogin(NOCOOKIETXT)
	End If
End If

%>
