-
Notifications
You must be signed in to change notification settings - Fork 425
fix: Throw ConfigurationError when invalid Auth0Client configuration #2026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e9eb654
add Auth0Client Configuration validation
tusharpandey13 ea6adc5
udpate config validation logic
tusharpandey13 df53d40
Merge https://github.com/auth0/nextjs-auth0 into feature/configuratio…
tusharpandey13 3c864e0
udpate config validation logic
tusharpandey13 6c7347e
udpate config validation logic
tusharpandey13 47fda0b
Merge branch 'main' into feature/configuration-validation
tusharpandey13 8e41333
update branch to use esm properly, related to #2028
tusharpandey13 0f57478
Merge branch 'feature/configuration-validation' of https://github.com…
tusharpandey13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,182 @@ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { ConfigurationError, ConfigurationErrorCode } from "../errors/index.js"; | ||
import { Auth0Client } from "./client.js"; | ||
|
||
describe("Auth0Client", () => { | ||
// Store original env vars | ||
const originalEnv = { ...process.env }; | ||
|
||
// Define correct environment variable names | ||
const ENV_VARS = { | ||
DOMAIN: "AUTH0_DOMAIN", | ||
CLIENT_ID: "AUTH0_CLIENT_ID", | ||
CLIENT_SECRET: "AUTH0_CLIENT_SECRET", | ||
CLIENT_ASSERTION_SIGNING_KEY: "AUTH0_CLIENT_ASSERTION_SIGNING_KEY", | ||
APP_BASE_URL: "APP_BASE_URL", | ||
SECRET: "AUTH0_SECRET", | ||
SCOPE: "AUTH0_SCOPE" | ||
}; | ||
|
||
// Clear env vars before each test | ||
beforeEach(() => { | ||
vi.resetModules(); | ||
// Clear all environment variables that might affect the tests | ||
delete process.env[ENV_VARS.DOMAIN]; | ||
delete process.env[ENV_VARS.CLIENT_ID]; | ||
delete process.env[ENV_VARS.CLIENT_SECRET]; | ||
delete process.env[ENV_VARS.CLIENT_ASSERTION_SIGNING_KEY]; | ||
delete process.env[ENV_VARS.APP_BASE_URL]; | ||
delete process.env[ENV_VARS.SECRET]; | ||
delete process.env[ENV_VARS.SCOPE]; | ||
}); | ||
|
||
// Restore env vars after each test | ||
afterEach(() => { | ||
process.env = { ...originalEnv }; | ||
}); | ||
|
||
describe("constructor validation", () => { | ||
it("should throw ConfigurationError when all required options are missing", () => { | ||
expect(() => new Auth0Client()).toThrow(ConfigurationError); | ||
|
||
try { | ||
new Auth0Client(); | ||
} catch (error) { | ||
const configError = error as ConfigurationError; | ||
expect(configError).toBeInstanceOf(ConfigurationError); | ||
expect(configError.code).toBe( | ||
ConfigurationErrorCode.MISSING_REQUIRED_OPTIONS | ||
); | ||
expect(configError.missingOptions).toContain("domain"); | ||
expect(configError.missingOptions).toContain("clientId"); | ||
expect(configError.missingOptions).toContain("clientAuthentication"); | ||
expect(configError.missingOptions).toContain("appBaseUrl"); | ||
expect(configError.missingOptions).toContain("secret"); | ||
|
||
// Check that error message contains specific text | ||
expect(configError.message).toContain( | ||
"Not all required options where provided" | ||
); | ||
expect(configError.message).toContain(ENV_VARS.DOMAIN); | ||
expect(configError.message).toContain(ENV_VARS.CLIENT_ID); | ||
expect(configError.message).toContain(ENV_VARS.CLIENT_SECRET); | ||
expect(configError.message).toContain( | ||
ENV_VARS.CLIENT_ASSERTION_SIGNING_KEY | ||
); | ||
expect(configError.message).toContain(ENV_VARS.APP_BASE_URL); | ||
expect(configError.message).toContain(ENV_VARS.SECRET); | ||
} | ||
}); | ||
|
||
it("should throw ConfigurationError when some required options are missing", () => { | ||
// Provide some but not all required options | ||
const options = { | ||
domain: "example.auth0.com", | ||
clientId: "client_123" | ||
}; | ||
|
||
try { | ||
new Auth0Client(options); | ||
} catch (error) { | ||
const configError = error as ConfigurationError; | ||
expect(configError).toBeInstanceOf(ConfigurationError); | ||
expect(configError.code).toBe( | ||
ConfigurationErrorCode.MISSING_REQUIRED_OPTIONS | ||
); | ||
// These should be missing | ||
expect(configError.missingOptions).toContain("clientAuthentication"); | ||
expect(configError.missingOptions).toContain("appBaseUrl"); | ||
expect(configError.missingOptions).toContain("secret"); | ||
// These should not be in the missing list | ||
expect(configError.missingOptions).not.toContain("domain"); | ||
expect(configError.missingOptions).not.toContain("clientId"); | ||
|
||
// Error message should only contain instructions for missing options | ||
expect(configError.message).toContain(ENV_VARS.CLIENT_SECRET); | ||
expect(configError.message).toContain( | ||
ENV_VARS.CLIENT_ASSERTION_SIGNING_KEY | ||
); | ||
expect(configError.message).toContain(ENV_VARS.APP_BASE_URL); | ||
expect(configError.message).toContain(ENV_VARS.SECRET); | ||
expect(configError.message).not.toContain(`Set ${ENV_VARS.DOMAIN}`); | ||
expect(configError.message).not.toContain(`Set ${ENV_VARS.CLIENT_ID}`); | ||
} | ||
}); | ||
|
||
it("should accept clientSecret as authentication method", () => { | ||
// Set required environment variables with clientSecret | ||
process.env[ENV_VARS.DOMAIN] = "env.auth0.com"; | ||
process.env[ENV_VARS.CLIENT_ID] = "env_client_id"; | ||
process.env[ENV_VARS.CLIENT_SECRET] = "env_client_secret"; | ||
process.env[ENV_VARS.APP_BASE_URL] = "https://myapp.com"; | ||
process.env[ENV_VARS.SECRET] = "env_secret"; | ||
|
||
// Should not throw | ||
const client = new Auth0Client(); | ||
|
||
// The client should be instantiated successfully | ||
expect(client).toBeInstanceOf(Auth0Client); | ||
}); | ||
|
||
it("should accept clientAssertionSigningKey as authentication method", () => { | ||
// Set required environment variables with clientAssertionSigningKey instead of clientSecret | ||
process.env[ENV_VARS.DOMAIN] = "env.auth0.com"; | ||
process.env[ENV_VARS.CLIENT_ID] = "env_client_id"; | ||
process.env[ENV_VARS.CLIENT_ASSERTION_SIGNING_KEY] = "some-signing-key"; | ||
process.env[ENV_VARS.APP_BASE_URL] = "https://myapp.com"; | ||
process.env[ENV_VARS.SECRET] = "env_secret"; | ||
|
||
// Should not throw | ||
const client = new Auth0Client(); | ||
|
||
// The client should be instantiated successfully | ||
expect(client).toBeInstanceOf(Auth0Client); | ||
}); | ||
|
||
it("should prioritize options over environment variables", () => { | ||
// Set environment variables | ||
process.env[ENV_VARS.DOMAIN] = "env.auth0.com"; | ||
process.env[ENV_VARS.CLIENT_ID] = "env_client_id"; | ||
process.env[ENV_VARS.CLIENT_SECRET] = "env_client_secret"; | ||
process.env[ENV_VARS.APP_BASE_URL] = "https://myapp.com"; | ||
process.env[ENV_VARS.SECRET] = "env_secret"; | ||
|
||
// Provide conflicting options | ||
const options = { | ||
domain: "options.auth0.com", | ||
clientId: "options_client_id", | ||
clientSecret: "options_client_secret", | ||
appBaseUrl: "https://options-app.com", | ||
secret: "options_secret" | ||
}; | ||
|
||
// Mock the validateAndExtractRequiredOptions to verify which values are used | ||
const mockValidateAndExtractRequiredOptions = vi | ||
.fn() | ||
.mockReturnValue(options); | ||
const originalValidateAndExtractRequiredOptions = | ||
Auth0Client.prototype["validateAndExtractRequiredOptions"]; | ||
Auth0Client.prototype["validateAndExtractRequiredOptions"] = | ||
mockValidateAndExtractRequiredOptions; | ||
|
||
try { | ||
new Auth0Client(options); | ||
|
||
// Check that validateAndExtractRequiredOptions was called with our options | ||
expect(mockValidateAndExtractRequiredOptions).toHaveBeenCalledWith( | ||
options | ||
); | ||
// The first argument of the first call should be our options object | ||
const passedOptions = | ||
mockValidateAndExtractRequiredOptions.mock.calls[0][0]; | ||
expect(passedOptions.domain).toBe("options.auth0.com"); | ||
expect(passedOptions.clientId).toBe("options_client_id"); | ||
} finally { | ||
// Restore the original method | ||
Auth0Client.prototype["validateAndExtractRequiredOptions"] = | ||
originalValidateAndExtractRequiredOptions; | ||
} | ||
}); | ||
}); | ||
}); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.