Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
fcc01902
Commit
fcc01902
authored
May 17, 2018
by
justcoding121
Committed by
GitHub
May 17, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #448 from SteveSyfuhs/feature/allow-proxy-auth-types
Added ability to inject a custom authentication mechanism
parents
49d94058
6b0d9305
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
17 deletions
+141
-17
ProxyAuthenticationContext.cs
Titanium.Web.Proxy/Models/ProxyAuthenticationContext.cs
+53
-0
ProxyAuthorizationHandler.cs
Titanium.Web.Proxy/ProxyAuthorizationHandler.cs
+76
-17
ProxyServer.cs
Titanium.Web.Proxy/ProxyServer.cs
+12
-0
No files found.
Titanium.Web.Proxy/Models/ProxyAuthenticationContext.cs
0 → 100644
View file @
fcc01902
namespace
Titanium.Web.Proxy.Models
{
public
enum
ProxyAuthenticationResult
{
/// <summary>
/// Indicates the authentication request was successful
/// </summary>
Success
,
/// <summary>
/// Indicates the authentication request failed
/// </summary>
Failure
,
/// <summary>
/// Indicates that this stage of the authentication request succeeded
/// And a second pass of the handshake needs to occur
/// </summary>
ContinuationNeeded
}
/// <summary>
/// A context container for authentication flows
/// </summary>
public
class
ProxyAuthenticationContext
{
/// <summary>
/// The result of the current authentication request
/// </summary>
public
ProxyAuthenticationResult
Result
{
get
;
set
;
}
/// <summary>
/// An optional continuation token to return to the caller if set
/// </summary>
public
string
Continuation
{
get
;
set
;
}
public
static
ProxyAuthenticationContext
Failed
()
{
return
new
ProxyAuthenticationContext
{
Result
=
ProxyAuthenticationResult
.
Failure
,
Continuation
=
null
};
}
public
static
ProxyAuthenticationContext
Succeeded
()
{
return
new
ProxyAuthenticationContext
{
Result
=
ProxyAuthenticationResult
.
Success
,
Continuation
=
null
};
}
}
}
Titanium.Web.Proxy/ProxyAuthorizationHandler.cs
View file @
fcc01902
...
@@ -21,7 +21,7 @@ namespace Titanium.Web.Proxy
...
@@ -21,7 +21,7 @@ namespace Titanium.Web.Proxy
private
async
Task
<
bool
>
checkAuthorization
(
SessionEventArgsBase
session
)
private
async
Task
<
bool
>
checkAuthorization
(
SessionEventArgsBase
session
)
{
{
// If we are not authorizing clients return true
// If we are not authorizing clients return true
if
(
AuthenticateUserFunc
==
null
)
if
(
AuthenticateUserFunc
==
null
&&
AuthenticateSchemeFunc
==
null
)
{
{
return
true
;
return
true
;
}
}
...
@@ -38,32 +38,34 @@ namespace Titanium.Web.Proxy
...
@@ -38,32 +38,34 @@ namespace Titanium.Web.Proxy
}
}
var
headerValueParts
=
header
.
Value
.
Split
(
ProxyConstants
.
SpaceSplit
);
var
headerValueParts
=
header
.
Value
.
Split
(
ProxyConstants
.
SpaceSplit
);
if
(
headerValueParts
.
Length
!=
2
||
!
headerValueParts
[
0
].
EqualsIgnoreCase
(
KnownHeaders
.
ProxyAuthorizationBasic
)
)
if
(
headerValueParts
.
Length
!=
2
)
{
{
// Return not authorized
// Return not authorized
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
return
false
;
}
}
string
decoded
=
Encoding
.
UTF8
.
GetString
(
Convert
.
FromBase64String
(
headerValueParts
[
1
]));
if
(
AuthenticateUserFunc
!=
null
)
int
colonIndex
=
decoded
.
IndexOf
(
':'
);
if
(
colonIndex
==
-
1
)
{
{
// Return not authorized
return
await
authenticateUserBasic
(
session
,
headerValueParts
);
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
}
}
string
username
=
decoded
.
Substring
(
0
,
colonIndex
);
if
(
AuthenticateSchemeFunc
!=
null
)
string
password
=
decoded
.
Substring
(
colonIndex
+
1
);
bool
authenticated
=
await
AuthenticateUserFunc
(
username
,
password
);
if
(!
authenticated
)
{
{
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
var
result
=
await
AuthenticateSchemeFunc
(
session
,
headerValueParts
[
0
],
headerValueParts
[
1
]);
if
(
result
.
Result
==
ProxyAuthenticationResult
.
ContinuationNeeded
)
{
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
,
result
.
Continuation
);
return
false
;
}
return
result
.
Result
==
ProxyAuthenticationResult
.
Success
;
}
}
return
authenticated
;
return
false
;
}
}
catch
(
Exception
e
)
catch
(
Exception
e
)
{
{
...
@@ -76,12 +78,41 @@ namespace Titanium.Web.Proxy
...
@@ -76,12 +78,41 @@ namespace Titanium.Web.Proxy
}
}
}
}
private
async
Task
<
bool
>
authenticateUserBasic
(
SessionEventArgsBase
session
,
string
[]
headerValueParts
)
{
if
(!
headerValueParts
[
0
].
EqualsIgnoreCase
(
KnownHeaders
.
ProxyAuthorizationBasic
))
{
// Return not authorized
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
}
string
decoded
=
Encoding
.
UTF8
.
GetString
(
Convert
.
FromBase64String
(
headerValueParts
[
1
]));
int
colonIndex
=
decoded
.
IndexOf
(
':'
);
if
(
colonIndex
==
-
1
)
{
// Return not authorized
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
}
string
username
=
decoded
.
Substring
(
0
,
colonIndex
);
string
password
=
decoded
.
Substring
(
colonIndex
+
1
);
bool
authenticated
=
await
AuthenticateUserFunc
(
username
,
password
);
if
(!
authenticated
)
{
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
}
return
authenticated
;
}
/// <summary>
/// <summary>
/// Create an authentication required response.
/// Create an authentication required response.
/// </summary>
/// </summary>
/// <param name="description">Response description.</param>
/// <param name="description">Response description.</param>
/// <returns></returns>
/// <returns></returns>
private
Response
createAuthentication407Response
(
string
description
)
private
Response
createAuthentication407Response
(
string
description
,
string
continuation
=
null
)
{
{
var
response
=
new
Response
var
response
=
new
Response
{
{
...
@@ -90,11 +121,39 @@ namespace Titanium.Web.Proxy
...
@@ -90,11 +121,39 @@ namespace Titanium.Web.Proxy
StatusDescription
=
description
StatusDescription
=
description
};
};
response
.
Headers
.
AddHeader
(
KnownHeaders
.
ProxyAuthenticate
,
$"Basic realm=\"
{
ProxyRealm
}
\""
);
if
(!
string
.
IsNullOrWhiteSpace
(
continuation
))
{
return
createContinuationResponse
(
response
,
continuation
);
}
if
(
AuthenticateUserFunc
!=
null
)
{
response
.
Headers
.
AddHeader
(
KnownHeaders
.
ProxyAuthenticate
,
$"Basic realm=\"
{
ProxyRealm
}
\""
);
}
if
(
AuthenticateSchemeFunc
!=
null
)
{
foreach
(
var
scheme
in
SupportedAuthenticationSchemes
)
{
response
.
Headers
.
AddHeader
(
KnownHeaders
.
ProxyAuthenticate
,
scheme
);
}
}
response
.
Headers
.
AddHeader
(
KnownHeaders
.
ProxyConnection
,
KnownHeaders
.
ProxyConnectionClose
);
response
.
Headers
.
AddHeader
(
KnownHeaders
.
ProxyConnection
,
KnownHeaders
.
ProxyConnectionClose
);
response
.
Headers
.
FixProxyHeaders
();
response
.
Headers
.
FixProxyHeaders
();
return
response
;
return
response
;
}
}
private
Response
createContinuationResponse
(
Response
response
,
string
continuation
)
{
response
.
Headers
.
AddHeader
(
KnownHeaders
.
ProxyAuthenticate
,
continuation
);
response
.
Headers
.
AddHeader
(
KnownHeaders
.
ProxyConnection
,
KnownHeaders
.
ConnectionKeepAlive
);
response
.
Headers
.
FixProxyHeaders
();
return
response
;
}
}
}
}
}
Titanium.Web.Proxy/ProxyServer.cs
View file @
fcc01902
...
@@ -269,6 +269,18 @@ namespace Titanium.Web.Proxy
...
@@ -269,6 +269,18 @@ namespace Titanium.Web.Proxy
/// </summary>
/// </summary>
public
Func
<
string
,
string
,
Task
<
bool
>>
AuthenticateUserFunc
{
get
;
set
;
}
public
Func
<
string
,
string
,
Task
<
bool
>>
AuthenticateUserFunc
{
get
;
set
;
}
/// <summary>
/// A pluggable callback to authenticate clients by scheme instead of requiring basic auth.
/// Parameters are current working session, schemeType, and token as provided by a calling client.
/// Should return success for successful authentication, continuation if the package requests, or failure.
/// </summary>
public
Func
<
SessionEventArgsBase
,
string
,
string
,
Task
<
ProxyAuthenticationContext
>>
AuthenticateSchemeFunc
{
get
;
set
;
}
/// <summary>
/// A collection of scheme types, e.g. basic, NTLM, Kerberos, Negotiate, to return if authentication is required.
/// </summary>
public
IEnumerable
<
string
>
SupportedAuthenticationSchemes
{
get
;
set
;
}
=
new
string
[
0
];
/// <summary>
/// <summary>
/// Dispose the Proxy instance.
/// Dispose the Proxy instance.
/// </summary>
/// </summary>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment