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
Show 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
private
async
Task
<
bool
>
checkAuthorization
(
SessionEventArgsBase
session
)
{
// If we are not authorizing clients return true
if
(
AuthenticateUserFunc
==
null
)
if
(
AuthenticateUserFunc
==
null
&&
AuthenticateSchemeFunc
==
null
)
{
return
true
;
}
...
...
@@ -38,8 +38,49 @@ namespace Titanium.Web.Proxy
}
var
headerValueParts
=
header
.
Value
.
Split
(
ProxyConstants
.
SpaceSplit
);
if
(
headerValueParts
.
Length
!=
2
||
!
headerValueParts
[
0
].
EqualsIgnoreCase
(
KnownHeaders
.
ProxyAuthorizationBasic
))
if
(
headerValueParts
.
Length
!=
2
)
{
// Return not authorized
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
}
if
(
AuthenticateUserFunc
!=
null
)
{
return
await
authenticateUserBasic
(
session
,
headerValueParts
);
}
if
(
AuthenticateSchemeFunc
!=
null
)
{
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
false
;
}
catch
(
Exception
e
)
{
ExceptionFunc
(
new
ProxyAuthorizationException
(
"Error whilst authorizing request"
,
session
,
e
,
httpHeaders
));
// Return not authorized
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
}
}
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"
);
...
...
@@ -65,23 +106,13 @@ namespace Titanium.Web.Proxy
return
authenticated
;
}
catch
(
Exception
e
)
{
ExceptionFunc
(
new
ProxyAuthorizationException
(
"Error whilst authorizing request"
,
session
,
e
,
httpHeaders
));
// Return not authorized
session
.
WebSession
.
Response
=
createAuthentication407Response
(
"Proxy Authentication Invalid"
);
return
false
;
}
}
/// <summary>
/// Create an authentication required response.
/// </summary>
/// <param name="description">Response description.</param>
/// <returns></returns>
private
Response
createAuthentication407Response
(
string
description
)
private
Response
createAuthentication407Response
(
string
description
,
string
continuation
=
null
)
{
var
response
=
new
Response
{
...
...
@@ -90,11 +121,39 @@ namespace Titanium.Web.Proxy
StatusDescription
=
description
};
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
.
FixProxyHeaders
();
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
/// </summary>
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>
/// Dispose the Proxy instance.
/// </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