<%
Option Explicit
%>
<!--#include virtual="/NR/Shared/ASPUtils.inc"-->
<!--#include virtual="/NR/system/access/IISAuthentication.inc"-->
<!--#include virtual="/NR/system/access/ManualLogin.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 = "Please ensure that cookies are enabled in your browser and try again."
NOTVISIBLEURL = "/NR/Shared/invalid.htm"


' ---- [Public] Checks if the user agent string indicates that it is a robot
' This function is not used by authenticate.inc, but is called by the WBC.
Function IsUserAgentRobot()
	Dim strUserAgent
	strUserAgent = UCASE( Request.ServerVariables( "HTTP_USER_AGENT" ) )
		
	' WebCrawler string can be either "ROBOT", "NCOMPASS' or "RESOLUTION"
	IsUserAgentRobot = ((InStr( strUserAgent, "ROBOT" ) > 0) Or _
						(InStr( strUserAgent, "NCOMPASS" ) > 0) Or _
						(InStr( strUserAgent, "RESOLUTION" ) > 0) )
End Function



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



' If user agent indicates that IIS authentication has been used before, but authentication
' data is not sent in this request, re-request authentication.
If ( Request.Cookies( CSTR_COOKIE_USING_IIS ) <> "") And _
   ( Request.ServerVariables( "LOGON_USER" ) = "" ) Then

		RequestIISAuthentication("Session previously authenticated using IIS authentication, and credentials have not been sent for this request. Re-authentication required")

End If


' If integrated IIS Security Context login is enabled then try to login
' as the current security context user.
If IISSecurityContextLogin Then

	' Set cookie indicating that a successful IIS context login has occurred
	' This is used in future requests to re-request credentials if they are 
	' not passed. This may happen if anonymous access is enabled on the 
	' NR directory, and the user-agent drops down to the most efficient access protocol.
	if ( Request.Cookies( CSTR_COOKIE_USING_IIS ) = "") Then
		Response.Cookies( CSTR_COOKIE_USING_IIS ) = "True"
		Response.Cookies( CSTR_COOKIE_USING_IIS ).Path = "/"
	End If

	If Not AutoSession.UserHasRightToBrowse Then
		RequestIISAuthentication( "(" & AutoSession.UserName & ") " & NORIGHTSTXT )
	ElseIf Not AutoSession.ThisIsVisible Then
		ServerSideRedirect( NOTVISIBLEURL )
	End If

'Attempt to authenticate using token stored as a cookie or in query string.
ElseIf AutoSession.AuthenticateUsingToken Then

	'Check if user has sufficient rights to browse the requested item.
	If Not AutoSession.UserHasRightToBrowse Then
		ServerSideRedirect( GetManualLoginRedirectUrl( "(" + AutoSession.UserName + ") " + NORIGHTSTXT, "" ) )
	ElseIf Not AutoSession.ThisIsVisible Then
		ServerSideRedirect( NOTVISIBLEURL )
	End If
	
	If Request.Cookies( "CMSAUTHTOKEN" ) <> "" Then
		Dim strNewToken
		If AutoSession.Session.ShouldRenewCookie Then
			strNewToken = AutoSession.Session.LoginCookie
			Response.Cookies( "CMSAUTHTOKEN" ) = strNewToken
			Response.Cookies( "CMSAUTHTOKEN" ).Path = "/"		
		End If
	End If
	
'If unsuccessful, and a token exists, then it is stale
ElseIf Request.Cookies( "CMSAUTHTOKEN" ) <> "" Or _
	   Request.QueryString( "CMSAUTHTOKEN" ) <> "" Then
			ServerSideRedirect( GetManualLoginRedirectUrl( "Your session has timed out.", "" ) )
	
'Try to authenticate using user info in query string or AUTH_USER, AUTH_PASSWORD 
'server variables (set if using Basic Authentication). This case remains here 
'for Web Crawler authentication when IsUsingIISAuthentication returns false, but 
'authenticated crawling is desired. If IsUsingIISAuthentication is True
'then the web crawler will be authenticated during the IISSecurityContextLogin
'In that case 'integrated Windows authentication' can be used.
'
ElseIf AutoSession.AuthenticateUsingUserInfo Then

	If Not AutoSession.UserHasRightToBrowse Then
		ServerSideRedirect( GetManualLoginRedirectUrl( "(" + AutoSession.UserName + ") " + NORIGHTSTXT, "" ) )
	ElseIf Not AutoSession.ThisIsVisible Then
		ServerSideRedirect( NOTVISIBLEURL )
	End If


'Authenticated access is requested if the HTTP user agent string contains the word 
'RESOLUTION. This is used to allow internal web crawlers authenticated access
'to the site using IIS authentication protocols. The actual authentication protocol is 
'chosen by IIS based on the virtual directory settings. IIS sends WWW-Authenticate 
'headers corresponding to the protocol chosen.
'
Elseif InStr( UCASE(Request.ServerVariables( "HTTP_USER_AGENT" ) ), "RESOLUTION" ) > 0 Then
		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

	If Not AutoSession.UserHasRightToBrowse Then
		If IsUsingIISAuthentication Then
			RequestIISAuthentication( "You are logged in as the Default Guest User.  " + NORIGHTSTXT )
		Else
			ServerSideRedirect( GetManualLoginRedirectUrl( "You are logged in as the Default Guest User.  " + NORIGHTSTXT + "  " + NOCOOKIETXT, "" ) )
		End If
	ElseIf Not AutoSession.ThisIsVisible Then
		ServerSideRedirect( GetManualLoginRedirectUrl( 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 IsUsingIISAuthentication Then
		RequestIISAuthentication( NORIGHTSTXT )
	Else
		ServerSideRedirect( GetManualLoginRedirectUrl( NOCOOKIETXT, "" ) )
	End If
End If

%>
