-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from PerimeterX/release/3.2.0
Release version 3.2.0 to master
- Loading branch information
Showing
36 changed files
with
2,065 additions
and
644 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
PerimeterXModule/CustomBehavior/ICredentialsExtractionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Web; | ||
|
||
namespace PerimeterX.CustomBehavior | ||
{ | ||
public interface ICredentialsExtractionHandler | ||
{ | ||
ExtractedCredentials Handle(HttpRequest httpRequest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Web; | ||
|
||
namespace PerimeterX.CustomBehavior | ||
{ | ||
public interface ILoginSuccessfulHandler | ||
{ | ||
bool Handle(HttpResponse httpResponse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
PerimeterXModule/Internals/CredentialsIntelligence/CredentialIntelligenceManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using Jil; | ||
using PerimeterX.CustomBehavior; | ||
|
||
namespace PerimeterX | ||
{ | ||
public class CredentialIntelligenceManager | ||
{ | ||
private ICredentialsIntelligenceProtocol protocol; | ||
private List<ExtractorObject> loginCredentialsExtractors; | ||
|
||
public CredentialIntelligenceManager(string ciVersion, List<ExtractorObject> loginCredentialsExraction) | ||
{ | ||
this.protocol = CredentialsIntelligenceProtocolFactory.Create(ciVersion); | ||
this.loginCredentialsExtractors = loginCredentialsExraction; | ||
} | ||
|
||
public LoginCredentialsFields ExtractCredentialsFromRequest(PxContext context, HttpRequest request, ICredentialsExtractionHandler credentialsExtractionHandler) | ||
{ | ||
try | ||
{ | ||
ExtractorObject extractionDetails = FindMatchCredentialsDetails(request); | ||
|
||
if (extractionDetails != null) | ||
{ | ||
ExtractedCredentials extractedCredentials = ExtractLoginCredentials(context, request, credentialsExtractionHandler, extractionDetails); | ||
|
||
|
||
if (extractedCredentials != null) | ||
{ | ||
return protocol.ProcessCredentials(extractedCredentials); | ||
} | ||
} | ||
|
||
} catch (Exception ex) | ||
{ | ||
PxLoggingUtils.LogError(string.Format("Failed to extract credentials.", ex.Message)); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private ExtractedCredentials ExtractLoginCredentials(PxContext context, HttpRequest request, ICredentialsExtractionHandler credentialsExtractionHandler, ExtractorObject extractionDetails) | ||
{ | ||
if (credentialsExtractionHandler != null) | ||
{ | ||
return credentialsExtractionHandler.Handle(request); | ||
} | ||
else | ||
{ | ||
return HandleExtractCredentials(extractionDetails, context, request); | ||
} | ||
} | ||
|
||
private ExtractorObject FindMatchCredentialsDetails(HttpRequest request) | ||
{ | ||
foreach (ExtractorObject loginObject in this.loginCredentialsExtractors) | ||
{ | ||
if (IsRequestMatchLoginRequestConfiguration(loginObject, request)) | ||
{ | ||
return loginObject; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static bool IsRequestMatchLoginRequestConfiguration(ExtractorObject extractorObject, HttpRequest request) | ||
{ | ||
if (request.HttpMethod.ToLower() == extractorObject.Method) | ||
{ | ||
if (extractorObject.PathType == "exact" && request.Path == extractorObject.Path) | ||
{ | ||
return true; | ||
} | ||
|
||
if (extractorObject.PathType == "regex" && Regex.IsMatch(request.Path, extractorObject.Path)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private ExtractedCredentials HandleExtractCredentials(ExtractorObject extractionDetails, PxContext pxContext, HttpRequest request) | ||
{ | ||
string userFieldName = extractionDetails.UserFieldName; | ||
string passwordFieldName = extractionDetails.PassFieldName; | ||
|
||
if (userFieldName == null || passwordFieldName == null) | ||
{ | ||
return null; | ||
} | ||
|
||
Dictionary<string, string> headers = pxContext.GetLowercaseHeadersAsDictionary(); | ||
|
||
if (extractionDetails.SentThrough == "header") | ||
{ | ||
return ExtractFromHeader(userFieldName, passwordFieldName, headers); | ||
} else if (extractionDetails.SentThrough == "query-param") | ||
{ | ||
return new ExtractedCredentials( | ||
request.QueryString[userFieldName].Replace(" ", "+"), | ||
request.QueryString[passwordFieldName].Replace(" ", "+") | ||
); | ||
} else if (extractionDetails.SentThrough == "body") | ||
{ | ||
return ExtractFromBody(userFieldName, passwordFieldName, headers, request); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static ExtractedCredentials ExtractFromHeader(string userFieldName, string passwordFieldName, Dictionary<string, string> headers) | ||
{ | ||
bool isUsernameHeaderExist = headers.TryGetValue(userFieldName.ToLower(), out string userName); | ||
bool isPasswordHeaderExist = headers.TryGetValue(passwordFieldName.ToLower(), out string password); | ||
|
||
if (!isUsernameHeaderExist && !isPasswordHeaderExist) { return null; } | ||
|
||
return new ExtractedCredentials(userName, password); | ||
} | ||
|
||
private ExtractedCredentials ExtractFromBody(string userFieldName, string passwordFieldName, Dictionary<string, string> headers, HttpRequest request) | ||
{ | ||
bool isContentTypeHeaderExist = headers.TryGetValue("content-type", out string contentType); | ||
|
||
string body = BodyReader.ReadRequestBody(request); | ||
|
||
if (!isContentTypeHeaderExist) | ||
{ | ||
return null; | ||
} else if (contentType.Contains("application/json")) | ||
{ | ||
return ExtractCredentialsFromJson(body, userFieldName, passwordFieldName); | ||
} else if (contentType.Contains("x-www-form-urlencoded")) | ||
{ | ||
return ReadValueFromUrlEncoded(body, userFieldName, passwordFieldName); | ||
} else if (contentType.Contains("form-data")) | ||
{ | ||
return ExtractValueFromMultipart(body, contentType, userFieldName, passwordFieldName); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private ExtractedCredentials ExtractCredentialsFromJson(string body, string userFieldName, string passwordFieldName) { | ||
|
||
dynamic jsonBody = JSON.DeserializeDynamic(body, PxConstants.JSON_OPTIONS); | ||
|
||
string userValue = PxCommonUtils.ExtractValueFromNestedJson(userFieldName, jsonBody); | ||
string passValue = PxCommonUtils.ExtractValueFromNestedJson(passwordFieldName, jsonBody); | ||
|
||
return new ExtractedCredentials(userValue, passValue); | ||
} | ||
|
||
private ExtractedCredentials ReadValueFromUrlEncoded(string body, string userFieldName, string passwordFieldName) | ||
{ | ||
var parametersQueryString = HttpUtility.ParseQueryString(body); | ||
var parametersDictionary = new Dictionary<string, string>(); | ||
foreach (var key in parametersQueryString.AllKeys) | ||
{ | ||
parametersDictionary.Add(key, parametersQueryString[key]); | ||
} | ||
|
||
return ExtractCredentialsFromDictionary(parametersDictionary, userFieldName, passwordFieldName); | ||
} | ||
|
||
private ExtractedCredentials ExtractValueFromMultipart(string body, string contentType, string userFieldName, string passwordFieldName) | ||
{ | ||
Dictionary<string, string> formData = BodyReader.GetFormDataContentAsDictionary(body, contentType); | ||
|
||
return ExtractCredentialsFromDictionary(formData, userFieldName, passwordFieldName); | ||
} | ||
|
||
private ExtractedCredentials ExtractCredentialsFromDictionary(Dictionary<string, string> parametersDictionary, string userFieldName, string passwordFieldName) | ||
{ | ||
bool isUsernameExist = parametersDictionary.TryGetValue(userFieldName, out string userField); | ||
bool isPasswordExist = parametersDictionary.TryGetValue(passwordFieldName, out string passwordField); | ||
|
||
if (!isUsernameExist && !isPasswordExist) | ||
{ | ||
return null; | ||
} | ||
|
||
return new ExtractedCredentials(userField, passwordField); | ||
} | ||
} | ||
|
||
|
||
} |
21 changes: 21 additions & 0 deletions
21
PerimeterXModule/Internals/CredentialsIntelligence/CredentialsIntelligenceProtocolFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using PerimeterX; | ||
|
||
namespace PerimeterX | ||
{ | ||
public class CredentialsIntelligenceProtocolFactory | ||
{ | ||
public static ICredentialsIntelligenceProtocol Create(string protocolVersion) | ||
{ | ||
switch(protocolVersion) | ||
{ | ||
case CIVersion.V2: | ||
return new V2CredentialsIntelligenceProtocol(); | ||
case CIVersion.MULTISTEP_SSO: | ||
return new MultistepSSoCredentialsIntelligenceProtocol(); | ||
default: | ||
throw new Exception("Unknown CI protocol version: " + protocolVersion); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
PerimeterXModule/Internals/CredentialsIntelligence/ExtractedCredentials.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace PerimeterX | ||
{ | ||
public class ExtractedCredentials | ||
{ | ||
[DataMember(Name = "username")] | ||
public string Username { get; set; } | ||
|
||
[DataMember(Name = "password")] | ||
public string Password { get; set; } | ||
|
||
public ExtractedCredentials(string username, string password) | ||
{ | ||
this.Username = username; | ||
this.Password = password; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
PerimeterXModule/Internals/CredentialsIntelligence/ExtractorObject.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace PerimeterX | ||
{ | ||
[DataContract] | ||
public class ExtractorObject | ||
{ | ||
[DataMember(Name = "path")] | ||
public string Path; | ||
|
||
[DataMember(Name = "path_type")] | ||
public string PathType; | ||
|
||
[DataMember(Name = "method")] | ||
public string Method; | ||
|
||
[DataMember(Name = "sent_through")] | ||
public string SentThrough; | ||
|
||
[DataMember(Name = "pass_field")] | ||
public string PassFieldName; | ||
|
||
[DataMember(Name = "user_field")] | ||
public string UserFieldName; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
PerimeterXModule/Internals/CredentialsIntelligence/ICredentialsIntelligenceProtocol.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
| ||
namespace PerimeterX | ||
{ | ||
public interface ICredentialsIntelligenceProtocol | ||
{ | ||
LoginCredentialsFields ProcessCredentials(ExtractedCredentials extractedCredentials); | ||
} | ||
} |
Oops, something went wrong.