Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
Add additional configuration validations (okta#10)
Browse files Browse the repository at this point in the history
* Add additional validations to make it easier for the developer to understand why Okta isn't working if they provided a bad config.

* Refactor

* Add FluentAssertions dependency to improve test clarity
* Convert OktaOptionsValidator to an abstract class with an abstract ValidateOptions method that should be implemented by the derived classes.
* Change String.Contains by IndexOf
  • Loading branch information
laura-rodriguez authored Apr 30, 2018
1 parent 525f291 commit 59e8f46
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 126 deletions.
6 changes: 6 additions & 0 deletions Okta.AspNet.Abstractions.Test/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="FluentAssertions.TestFramework" value="xunit"/>
</appSettings>
</configuration>
14 changes: 14 additions & 0 deletions Okta.AspNet.Abstractions.Test/MockOktaOptionsValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Okta.AspNet.Abstractions.Test
{
public class MockOktaOptionsValidator : OktaOptionsValidator
{
protected override void ValidateOptions(OktaOptions options)
{
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
Expand Down
73 changes: 24 additions & 49 deletions Okta.AspNet.Abstractions.Test/OktaMvcOptionsValidatorShould.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Okta.AspNet.Abstractions;
using System;
using System;
using Xunit;
using FluentAssertions;

namespace Okta.AspNet.Abstractions.Test
{
Expand All @@ -13,84 +13,59 @@ public void FailWhenClientSecretIsNullOrEmpty(String clientSecret)
{
var options = new OktaMvcOptions()
{
OrgUrl = "OrgUrl",
OrgUrl = OktaOptionsValidatorHelper.VALID_ORG_URL,
ClientId = "ClientId",
ClientSecret = clientSecret
};

ShouldFailValidation(options, nameof(OktaMvcOptions.ClientSecret));
Action action = () => new OktaMvcOptionsValidator().Validate(options);
action.Should().Throw<ArgumentNullException>().Where(e => e.ParamName == nameof(OktaMvcOptions.ClientSecret));
}

[Theory]
[InlineData(null)]
[InlineData("")]
public void FailWhenRedirectUriIsNullOrEmpty(String redirectUri)
{
var options = new OktaMvcOptions()
{
OrgUrl = "OrgUrl",
ClientId = "ClientId",
ClientSecret = "ClientSecret",
RedirectUri = redirectUri
};

ShouldFailValidation(options, nameof(OktaMvcOptions.RedirectUri));
}

[Theory]
[InlineData(null)]
[InlineData("")]
public void FailWhenClientIdIsNullOrEmpty(String clientId)
[Fact]
public void FailWhenClientSecretIsNotDefined()
{
var options = new OktaMvcOptions()
{
OrgUrl = "OrgUrl",
ClientId = clientId,
OrgUrl = OktaOptionsValidatorHelper.VALID_ORG_URL,
ClientId = "ClientId",
ClientSecret = "{ClientSecret}"
};

ShouldFailValidation(options, nameof(OktaMvcOptions.ClientId));
Action action = () => new OktaMvcOptionsValidator().Validate(options);
action.Should().Throw<ArgumentException>().Where(e => e.ParamName == nameof(OktaMvcOptions.ClientSecret));
}

[Theory]
[InlineData(null)]
[InlineData("")]
public void FailIfOrgUrlIsNullOrEmpty(String orgUrl)
public void FailWhenRedirectUriIsNullOrEmpty(String redirectUri)
{
var options = new OktaMvcOptions()
{
OrgUrl = orgUrl,
ClientId = "ClientId"
};
{
OrgUrl = OktaOptionsValidatorHelper.VALID_ORG_URL,
ClientId = "ClientId",
ClientSecret = "ClientSecret",
RedirectUri = redirectUri
};

ShouldFailValidation(options, nameof(OktaMvcOptions.OrgUrl));
Action action = () => new OktaMvcOptionsValidator().Validate(options);
action.Should().Throw<ArgumentNullException>().Where(e => e.ParamName == nameof(OktaMvcOptions.RedirectUri));
}

[Fact]
public void NotThrowWhenParamsAreProvided()
{
var options = new OktaMvcOptions()
{
OrgUrl = "OrgUrl",
OrgUrl = OktaOptionsValidatorHelper.VALID_ORG_URL,
ClientId = "ClientId",
ClientSecret = "ClientSecret",
RedirectUri = "RedirectUri"
};

new OktaMvcOptionsValidator().Validate(options);
Assert.True(true, "No exception was thrown.");
}

private void ShouldFailValidation(OktaMvcOptions options, string paramName)
{
try
{
new OktaMvcOptionsValidator().Validate(options);
Assert.True(false, "No exception was thrown.");
}
catch (ArgumentNullException e)
{
Assert.Contains(e.ParamName, paramName);
}
Action action = () => new OktaMvcOptionsValidator().Validate(options);
action.Should().NotThrow();
}
}
}
7 changes: 7 additions & 0 deletions Okta.AspNet.Abstractions.Test/OktaOptionsValidatorHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Okta.AspNet.Abstractions.Test
{
public static class OktaOptionsValidatorHelper
{
public static readonly string VALID_ORG_URL = "https://myOktaDomain.oktapreview.com";
}
}
97 changes: 97 additions & 0 deletions Okta.AspNet.Abstractions.Test/OktaOptionsValidatorShould.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using Xunit;
using FluentAssertions;
namespace Okta.AspNet.Abstractions.Test
{
public class OktaOptionsValidatorShould
{
[Theory]
[InlineData(null)]
[InlineData("")]
public void FailWhenClientIdIsNullOrEmpty(String clientId)
{
var options = new OktaOptions()
{
OrgUrl = OktaOptionsValidatorHelper.VALID_ORG_URL,
ClientId = clientId,
};

Action action = () => new MockOktaOptionsValidator().Validate(options);
action.Should().Throw<ArgumentNullException>().Where(e => e.ParamName == nameof(OktaOptions.ClientId));
}

[Theory]
[InlineData(null)]
[InlineData("")]
public void FailIfOrgUrlIsNullOrEmpty(String orgUrl)
{
var options = new OktaOptions()
{
OrgUrl = orgUrl,
ClientId = "ClientId"
};

Action action = () => new MockOktaOptionsValidator().Validate(options);
action.Should().Throw<ArgumentNullException>().Where(e => e.ParamName == nameof(OktaOptions.OrgUrl));
}

[Theory]
[InlineData("http://myOktaDomain.oktapreview.com")]
[InlineData("httsp://myOktaDomain.oktapreview.com")]
[InlineData("invalidOrgUrl")]
public void FailIfOrgUrlIsNotStartingWithHttps(String orgUrl)
{
var options = new OktaMvcOptions()
{
OrgUrl = orgUrl,
ClientId = "ClientId"
};

Action action = () => new MockOktaOptionsValidator().Validate(options);
action.Should().Throw<ArgumentException>().Where(e => e.ParamName == nameof(OktaOptions.OrgUrl));
}

[Theory]
[InlineData("https://{Youroktadomain}.com")]
[InlineData("https://{yourOktaDomain}.com")]
[InlineData("https://{YourOktaDomain}.com")]
public void FailIfOrgUrlIsNotDefined(String orgUrl)
{
var options = new OktaMvcOptions()
{
OrgUrl = orgUrl,
ClientId = "ClientId"
};

Action action = () => new MockOktaOptionsValidator().Validate(options);
action.Should().Throw<ArgumentException>().Where(e => e.ParamName == nameof(OktaOptions.OrgUrl));
}

[Fact]
public void FailIfOrgUrlIsIncludingAdmin()
{
var options = new OktaMvcOptions()
{
OrgUrl = "https://myOktaOrg-admin.oktapreview.com",
ClientId = "ClientId"
};

Action action = () => new MockOktaOptionsValidator().Validate(options);
action.Should().Throw<ArgumentException>().Where(e => e.ParamName == nameof(OktaOptions.OrgUrl));
}

[Fact]
public void FailIfOrgUrlHasTypo()
{
var options = new OktaMvcOptions()
{
OrgUrl = "https://myOktaDomain.oktapreview.com.com",
ClientId = "ClientId"
};

Action action = () => new MockOktaOptionsValidator().Validate(options);
action.Should().Throw<ArgumentException>().Where(e => e.ParamName == nameof(OktaOptions.OrgUrl));
}
}
}

67 changes: 0 additions & 67 deletions Okta.AspNet.Abstractions.Test/OktaWebApiOptionsValidatorShould.cs

This file was deleted.

18 changes: 12 additions & 6 deletions Okta.AspNet.Abstractions/OktaMvcOptionsValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@ namespace Okta.AspNet.Abstractions
{
public class OktaMvcOptionsValidator : OktaOptionsValidator
{
public void Validate(OktaMvcOptions options)
protected override void ValidateOptions(OktaOptions options)
{
base.ValidateBaseOktaOptions(options);
var mvcOptions = (OktaMvcOptions)options;

if (string.IsNullOrEmpty(options.ClientSecret))
if (string.IsNullOrEmpty(mvcOptions.ClientSecret))
{
throw new ArgumentNullException(nameof(options.ClientSecret),
throw new ArgumentNullException(nameof(mvcOptions.ClientSecret),
"Your Okta Application client secret is missing. You can find it in the Okta Developer Console in the details for the Application you created.");
}

if (string.IsNullOrEmpty(options.RedirectUri))
if (mvcOptions.ClientSecret.IndexOf("{ClientSecret}", StringComparison.OrdinalIgnoreCase) >= 0)
{
throw new ArgumentNullException(nameof(options.RedirectUri),
throw new ArgumentException( "You need to copy your client secret from the Okta Developer Console in the details for the Application you created."
, nameof(mvcOptions.ClientSecret));
}

if (string.IsNullOrEmpty(mvcOptions.RedirectUri))
{
throw new ArgumentNullException(nameof(mvcOptions.RedirectUri),
"Your Okta Application redirect URI is missing. You can find it in the Okta Developer Console in the details for the Application you created.");
}
}
Expand Down
Loading

0 comments on commit 59e8f46

Please sign in to comment.