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

Commit

Permalink
Add/Modify WebOptions validator. (okta#33)
Browse files Browse the repository at this point in the history
Add/update configuration guards
  • Loading branch information
laura-rodriguez authored Sep 6, 2018
1 parent 1491b55 commit 01f7188
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
17 changes: 11 additions & 6 deletions Okta.AspNet.Abstractions.Test/OktaWebOptionsValidatorShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,30 @@ public void FailIfOktaDomainIsNotDefined(string oktaDomain)
action.Should().Throw<ArgumentException>().Where(e => e.ParamName == nameof(OktaWebOptions.OktaDomain));
}

[Fact]
public void FailIfOktaDomainIsIncludingAdmin()
[Theory]
[InlineData("https://myOktaOrg-admin.oktapreview.com")]
[InlineData("https://myOktaOrg-admin.okta.com")]
[InlineData("https://myOktaOrg-admin.okta-emea.com")]
public void FailIfOktaDomainIsIncludingAdmin(string oktaDomain)
{
var options = new OktaWebOptions()
{
OktaDomain = "https://myOktaOrg-admin.oktapreview.com",
OktaDomain = oktaDomain,
ClientId = "ClientId",
};

Action action = () => new OktaWebOptionsValidator<OktaWebOptions>().Validate(options);
action.Should().Throw<ArgumentException>().Where(e => e.ParamName == nameof(OktaWebOptions.OktaDomain));
}

[Fact]
public void FailIfOktaDomainHasTypo()
[Theory]
[InlineData("https://myOktaDomain.oktapreview.com.com")]
[InlineData("https://myOktaDomain.oktapreview.com://foo")]
public void FailIfOktaDomainHasTypo(string oktaDomain)
{
var options = new OktaWebOptions()
{
OktaDomain = "https://myOktaDomain.oktapreview.com.com",
OktaDomain = oktaDomain,
ClientId = "ClientId",
};

Expand Down
24 changes: 16 additions & 8 deletions Okta.AspNet.Abstractions/OktaWebOptionsValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// </copyright>

using System;
using System.Text.RegularExpressions;

namespace Okta.AspNet.Abstractions
{
Expand All @@ -26,46 +27,53 @@ public void Validate(OktaWebOptions options)
{
throw new ArgumentNullException(
nameof(options.OktaDomain),
"Your Okta domain is missing. You can find it in the Okta Developer Console. It'll look like: https://dev-12345.oktapreview.com");
"Your Okta URL is missing. Okta URLs should look like: https://{yourOktaDomain}. You can copy your domain from the Okta Developer Console.");
}

if (!options.OktaDomain.StartsWith("https://"))
{
throw new ArgumentException(
"Your Okta domain must start with https. You can copy your Okta domain from the Okta developer dashboard.",
"Your Okta URL must start with https. You can copy your domain from the Okta Developer Console.",
nameof(options.OktaDomain));
}

if (options.OktaDomain.IndexOf("{yourOktaDomain}", StringComparison.OrdinalIgnoreCase) >= 0)
{
throw new ArgumentException(
"You need to copy your Okta domain from the Okta developer dashboard.", nameof(options.OktaDomain));
"Replace {yourOktaDomain} with your Okta domain. You can copy your domain from the Okta Developer Console.", nameof(options.OktaDomain));
}

if (options.OktaDomain.IndexOf("-admin.oktapreview.com", StringComparison.OrdinalIgnoreCase) >= 0)
if (options.OktaDomain.IndexOf("-admin.oktapreview.com", StringComparison.OrdinalIgnoreCase) >= 0 ||
options.OktaDomain.IndexOf("-admin.okta.com", StringComparison.OrdinalIgnoreCase) >= 0 ||
options.OktaDomain.IndexOf("-admin.okta-emea.com", StringComparison.OrdinalIgnoreCase) >= 0)
{
throw new ArgumentException(
"Your Okta domain should not contain -admin. You can copy your Okta domain from the Okta developer dashboard.", nameof(options.OktaDomain));
"Your Okta domain should not contain -admin. Your domain is: {valueWithoutAdmin}. You can copy your domain from the Okta Developer Console.", nameof(options.OktaDomain));
}

if (options.OktaDomain.IndexOf(".com.com", StringComparison.OrdinalIgnoreCase) >= 0)
{
throw new ArgumentException(
"It looks like there's a typo in your Okta domain. You can copy your Okta domain from the Okta developer dashboard.", nameof(options.OktaDomain));
"It looks like there's a typo in your Okta domain. You can copy your domain from the Okta Developer Console.", nameof(options.OktaDomain));
}

if (Regex.Matches(options.OktaDomain, "://").Count != 1)
{
throw new ArgumentNullException(nameof(options.OktaDomain), "It looks like there's a typo in your Okta domain. You can copy your domain from the Okta Developer Console.");
}

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

if (options.ClientId.IndexOf("{ClientId}", StringComparison.OrdinalIgnoreCase) >= 0)
{
throw new ArgumentNullException(
nameof(options.ClientId),
"You need to copy your Client ID from the Okta Developer Console in the details for the Application you created.");
"Replace {clientId} with the client ID of your Application. You can copy it from the Okta Developer Console in the details for the Application you created.");
}

ValidateInternal((T)options);
Expand Down
4 changes: 2 additions & 2 deletions Okta.AspNet/OktaMvcOptionsValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ protected override void ValidateInternal(OktaMvcOptions options)
{
throw new ArgumentNullException(
nameof(options.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.");
"Your client secret is missing. You can copy it from the Okta Developer Console in the details for the Application you created.");
}

if (options.ClientSecret.IndexOf("{ClientSecret}", StringComparison.OrdinalIgnoreCase) >= 0)
{
throw new ArgumentException(
"You need to copy your client secret from the Okta Developer Console in the details for the Application you created.",
"Replace {clientSecret} with the client secret of your Application. You can copy it from the Okta Developer Console in the details for the Application you created.",
nameof(options.ClientSecret));
}

Expand Down

0 comments on commit 01f7188

Please sign in to comment.