From 098c0540565bd679c828e2bc938385e20c81235e Mon Sep 17 00:00:00 2001 From: Ionut Manolache Date: Thu, 9 Jan 2025 12:31:33 -0500 Subject: [PATCH] backward compatibility with guardian domains --- Guardian/API/APIClient.swift | 7 ++++++- GuardianTests/GuardianSpec.swift | 22 ++++++++++++++++++---- README.md | 2 +- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Guardian/API/APIClient.swift b/Guardian/API/APIClient.swift index a28ef87..046771c 100644 --- a/Guardian/API/APIClient.swift +++ b/Guardian/API/APIClient.swift @@ -77,6 +77,11 @@ struct APIClient: API { private extension URL { func appendingPathComponentIfNeeded(_ pathComponent: String) -> URL { - lastPathComponent == pathComponent ? self : appendingPathComponent(pathComponent) + guard + lastPathComponent != pathComponent, + !absoluteString.hasSuffix("guardian.auth0.com"), + absoluteString.range(of: "guardian\\.[^\\.]*\\.auth0\\.com", options: .regularExpression) == nil + else { return self } + return appendingPathComponent(pathComponent) } } diff --git a/GuardianTests/GuardianSpec.swift b/GuardianTests/GuardianSpec.swift index 216e4d3..5531b4d 100644 --- a/GuardianTests/GuardianSpec.swift +++ b/GuardianTests/GuardianSpec.swift @@ -58,13 +58,27 @@ class GuardianSpec: QuickSpec { it("should return api with url only") { expect(Guardian.api(url: URL(string: "https://samples.guardian.auth0.com")!)).toNot(beNil()) } + } + + describe("adding path component") { + it("should not add path component to url with guardian.auth0.com suffix") { + expect(Guardian.api(url: URL(string: "https://samples.guardian.auth0.com")!).baseUrl.absoluteString).to(equal("https://samples.guardian.auth0.com")) + } + + it("should not add path component to url with guardian.region.auth0.com") { + expect(Guardian.api(url: URL(string: "https://samples.guardian.en.auth0.com")!).baseUrl.absoluteString).to(equal("https://samples.guardian.en.auth0.com")) + } + + it("should not add path component to custom url without guardian with already added path component") { + expect(Guardian.api(url: URL(string: "https://samples.auth0.com/appliance-mfa")!).baseUrl.absoluteString).to(equal("https://samples.auth0.com/appliance-mfa")) + } - it("should have base url with single 'appliance-mfa' component for passed url without this component") { - expect(Guardian.api(url: URL(string: "https://samples.guardian.auth0.com")!).baseUrl.absoluteString).to(equal("https://samples.guardian.auth0.com/appliance-mfa")) + it("should add path component to custom url without guardian without already added path component") { + expect(Guardian.api(url: URL(string: "https://samples.auth0.com")!).baseUrl.absoluteString).to(equal("https://samples.auth0.com/appliance-mfa")) } - it("should have base url with single 'appliance-mfa' component for passed url with this component") { - expect(Guardian.api(url: URL(string: "https://samples.guardian.auth0.com/appliance-mfa")!).baseUrl.absoluteString).to(equal("https://samples.guardian.auth0.com/appliance-mfa")) + it("should add path component to custom url with guardian without already added path component") { + expect(Guardian.api(url: URL(string: "https://samples.guardian.some.thing.auth0.com")!).baseUrl.absoluteString).to(equal("https://samples.guardian.some.thing.auth0.com/appliance-mfa")) } } diff --git a/README.md b/README.md index bed348c..96ab759 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ github "auth0/Guardian.swift" ~> 1.4.2 import Guardian ``` -Set the auth0 domains for your tenant: +Set the domain for your auth0 tenant: ```swift let tenantDomain = "..auth0.com" ```