Skip to content

Commit 5fa4288

Browse files
committed
Redmine:16027 ([NetKit] Enhancement of authenticationPage and authenticationErrorPage)
1 parent 3283aca commit 5fa4288

File tree

4 files changed

+73
-22
lines changed

4 files changed

+73
-22
lines changed

Project/Sources/Classes/OAuth2Authorization.4dm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Function getResponse($request : 4D.IncomingMessage) : 4D.OutgoingMessage
1919

2020
// If the response contains a redirect URL, we send a 302 Temporary Redirect
2121
If ((Value type($response.redirectURL)=Is text) && (Length($response.redirectURL)>0))
22-
$outgoingResponse.setStatus(302) // Temporary redirect
22+
$outgoingResponse.setStatus($response.status)
2323
$outgoingResponse.setHeader("Location"; String($response.redirectURL))
2424
Else
2525
$outgoingResponse.setStatus($response.status)

Project/Sources/Classes/OAuth2Provider.4dm

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ property clientSecret : Text // The application secret that you created in the
99
property token : Object // Any valid existing token
1010
property tokenExpiration : Text
1111
property timeout : Integer
12-
property authenticationPage : 4D.File
13-
property authenticationErrorPage : 4D.File
12+
property authenticationPage : Variant
13+
property authenticationErrorPage : Variant
1414
property accessType : Text
1515
property loginHint : Text
1616
property prompt : Text
@@ -125,15 +125,21 @@ Class constructor($inParams : Object)
125125
is received correctly in signed in mode
126126
If not present the default page is used
127127
*/
128-
This.authenticationPage:=cs.Tools.me.retainFileObject($inParams.authenticationPage)
129-
128+
If (cs.Tools.me.isValidURL(String($inParams.authenticationPage)))
129+
This.authenticationPage:=String($inParams.authenticationPage)
130+
Else
131+
This.authenticationPage:=cs.Tools.me.retainFileObject($inParams.authenticationPage)
132+
End if
130133
/*
131134
Path of the web page to display in the webbrowser when the authentication server
132135
returns an error in signed in mode
133136
If not present the default page is used
134137
*/
135-
This.authenticationErrorPage:=cs.Tools.me.retainFileObject($inParams.authenticationErrorPage)
136-
138+
If (cs.Tools.me.isValidURL(String($inParams.authenticationErrorPage)))
139+
This.authenticationErrorPage:=String($inParams.authenticationErrorPage)
140+
Else
141+
This.authenticationErrorPage:=cs.Tools.me.retainFileObject($inParams.authenticationErrorPage)
142+
End if
137143
/*
138144
Indicates whether your application can refresh access tokens when the user is not
139145
present at the browser. Valid parameter values are online, which is the default
@@ -489,7 +495,7 @@ Function _getToken_SignedIn($bUseRefreshToken : Boolean) : Object
489495
$options.port:=cs.Tools.me.getPortFromURL(This.redirectURI)
490496
$options.enableDebugLog:=This.enableDebugLog
491497
$options.useTLS:=(Position("https"; This.redirectURI)=1)
492-
If ((This.authenticationPage#Null) || (This.authenticationErrorPage#Null))
498+
If ((Value type(This.authenticationPage)=Is object) || (Value type(This.authenticationErrorPage)=Is object))
493499
var $file : Object:=(This.authenticationPage#Null) ? This.authenticationPage : This.authenticationErrorPage
494500
If (OB Instance of($file; 4D.File))
495501
$options.webFolder:=$file.parent

Project/Sources/Classes/Tools.4dm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,16 @@ Function isValidEmail($inEmail : Text) : Boolean
358358
// ----------------------------------------------------
359359

360360

361+
Function isValidURL($inURL : Text) : Boolean
362+
363+
var $URL : cs.URL:=cs.URL.new($inURL)
364+
365+
return (((Length($URL.scheme)>0) && ($URL.scheme="http@")) && (Length($URL.host)>0))
366+
367+
368+
// ----------------------------------------------------
369+
370+
361371
Function quoteString($inString : Text) : Text
362372

363373
var $result : Text:=$inString

Project/Sources/Methods/_authorize.4dm

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33

44
var $redirectURI : Text
55
var $URL : Text:=$inOptions.redirectURI
6-
var $customResponseFile; $customErrorFile : 4D.File
6+
var $authenticationPage; $authenticationErrorPage : Variant
77
var $state : Text:=String($inOptions.state)
8-
var $responseFile : 4D.File:=Folder(fk resources folder).file("responseTemplate.html")
98

109
If (OB Is defined(Storage.requests; $state))
1110
$redirectURI:=String(Storage.requests[$state].redirectURI)
1211
If (Length($redirectURI)>0)
1312
$redirectURI:=cs.Tools.me.getPathFromURL($redirectURI)+"@"
1413
End if
15-
$customResponseFile:=(Value type(Storage.requests[$state].authenticationPage)#Is undefined) ? Storage.requests[$state].authenticationPage : Null
16-
$customErrorFile:=(Value type(Storage.requests[$state].authenticationErrorPage)#Is undefined) ? Storage.requests[$state].authenticationErrorPage : Null
14+
$authenticationPage:=(Value type(Storage.requests[$state].authenticationPage)#Is undefined) ? Storage.requests[$state].authenticationPage : Null
15+
$authenticationErrorPage:=(Value type(Storage.requests[$state].authenticationErrorPage)#Is undefined) ? Storage.requests[$state].authenticationErrorPage : Null
1716
End if
1817

1918
If ($URL=$redirectURI)
2019

20+
var $responseFile : 4D.File:=Null
21+
var $responseRedirectURI : Text:=""
22+
var $customPageObject : Object:=Null
2123
var $pageTitle; $pageMessage; $pageDetails : Text
2224

2325
If (OB Is defined(Storage.requests; $state))
@@ -45,24 +47,57 @@ If ($URL=$redirectURI)
4547
End if
4648
$pageDetails:=Localized string("OAuth2_Response_Details")
4749

48-
$responseFile:=($customErrorFile#Null) ? $customErrorFile : $responseFile
50+
If (Value type($authenticationErrorPage)=Is text)
51+
$responseRedirectURI:=String($authenticationErrorPage)
52+
Else
53+
$customPageObject:=($authenticationErrorPage#Null) ? $authenticationErrorPage : Folder(fk resources folder).file("responseTemplate.html")
54+
If (OB Instance of($customPageObject; 4D.File))
55+
$responseFile:=$customPageObject
56+
End if
57+
End if
4958
Else
5059

5160
$pageTitle:=Localized string("OAuth2_Response_Title")
5261
$pageMessage:=Localized string("OAuth2_Response_Message")
5362
$pageDetails:=Localized string("OAuth2_Response_Details")
5463

55-
$responseFile:=($customResponseFile#Null) ? $customResponseFile : $responseFile
64+
If (Value type($authenticationPage)=Is text)
65+
$responseRedirectURI:=String($authenticationPage)
66+
Else
67+
$customPageObject:=($authenticationPage#Null) ? $authenticationPage : Folder(fk resources folder).file("responseTemplate.html")
68+
If (OB Instance of($customPageObject; 4D.File))
69+
$responseFile:=$customPageObject
70+
End if
71+
End if
5672
End if
5773

58-
var $responseFileContent : Text:=$responseFile.getText()
59-
var $outResponseBody : Text:=""
60-
61-
PROCESS 4D TAGS($responseFileContent; $outResponseBody; $pageTitle; $pageMessage; $pageDetails)
62-
63-
$outResponse.status:=200
64-
$outResponse.body:=$outResponseBody
65-
$outResponse.contentType:="text/html; charset=UTF-8"
74+
// If $responseFile is a 4D.File, we process it as a template
75+
Case of
76+
: ((Value type($responseRedirectURI)=Is text) && Length($responseRedirectURI)>0)
77+
// If we have a redirect URI, we just send a redirect to that URI
78+
$outResponse.status:=302 // Temporary redirect
79+
$outResponse.redirectURL:=String($responseRedirectURI)
80+
81+
: (OB Instance of($responseFile; 4D.File))
82+
If ($responseFile=Null)
83+
$responseFile:=Folder(fk resources folder).file("responseTemplate.html")
84+
End if
85+
86+
var $responseFileContent : Text:=$responseFile.getText()
87+
var $outResponseBody : Text:=""
88+
89+
PROCESS 4D TAGS($responseFileContent; $outResponseBody; $pageTitle; $pageMessage; $pageDetails)
90+
91+
$outResponse.status:=200
92+
$outResponse.body:=$outResponseBody
93+
$outResponse.contentType:="text/html; charset=UTF-8"
94+
Else
95+
96+
// If we don't have a redirect URI or a response file, we just send a 500 Internal Server Error response
97+
$outResponse.status:=500
98+
$outResponse.body:=cs.Tools.me.buildPageFromTemplate($pageTitle; "500 Internal Server Error"; $pageMessage)
99+
$outResponse.contentType:="text/html; charset=UTF-8"
100+
End case
66101

67102
return True
68103

0 commit comments

Comments
 (0)