Skip to content
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

Add support for an authorize URL override #854

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Auth0/Auth0WebAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class Auth0WebAuth: WebAuth {
private(set) var maxAge: Int?
private(set) var organization: String?
private(set) var invitationURL: URL?
private(set) var overrideAuthorizeURL: URL?
private(set) var provider: WebAuthProvider?
private(set) var onCloseCallback: (() -> Void)?

Expand Down Expand Up @@ -102,6 +103,11 @@ final class Auth0WebAuth: WebAuth {
return self
}

func authorizeURL(_ authorizeURL: URL) -> Self {
self.overrideAuthorizeURL = authorizeURL
return self
}

func nonce(_ nonce: String) -> Self {
self.nonce = nonce
return self
Expand Down Expand Up @@ -245,7 +251,7 @@ final class Auth0WebAuth: WebAuth {
state: String?,
organization: String?,
invitation: String?) -> URL {
let authorize = URL(string: "authorize", relativeTo: self.url)!
let authorize = self.overrideAuthorizeURL ?? URL(string: "authorize", relativeTo: self.url)!
var components = URLComponents(url: authorize, resolvingAgainstBaseURL: true)!
var items: [URLQueryItem] = []
var entries = defaults
Expand Down
6 changes: 6 additions & 0 deletions Auth0/WebAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public protocol WebAuth: Trackable, Loggable {
/// - Returns: The same `WebAuth` instance to allow method chaining.
func redirectURL(_ redirectURL: URL) -> Self

/// Specify a custom authorize URL to be used.
///
/// - Parameter authorizeURL: Custom authorize URL.
/// - Returns: The same `WebAuth` instance to allow method chaining.
func authorizeURL(_ authorizeURL: URL) -> Self

/// Specify an audience name for the API that your application will call using the access token returned after
/// authentication.
/// This value must match the **API Identifier** displayed in the APIs section of the [Auth0 Dashboard](https://manage.auth0.com/#/apis).
Expand Down
14 changes: 14 additions & 0 deletions Auth0Tests/WebAuthSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@ class WebAuthSpec: QuickSpec {
]
}

itBehavesLike(ValidAuthorizeURLExample) {
return [
"url": newWebAuth()
.authorizeURL(URL(string: "https://example.com/authorize")!)
.buildAuthorizeURL(withRedirectURL: RedirectURL, defaults: defaults, state: State, organization: nil, invitation: nil),
"domain": "example.com",
"query": defaultQuery(),
]
}

context("encoding") {

it("should encode + as %2B"){
Expand All @@ -294,6 +304,10 @@ class WebAuthSpec: QuickSpec {

}

it("should build with a custom authorize url") {
let url = URL(string: "https://example.com/authorize")!
expect(newWebAuth().authorizeURL(url).overrideAuthorizeURL) == url
}
}

describe("redirect uri") {
Expand Down
Loading