Skip to content

Commit

Permalink
feat: Add support for an authorize URL override (#854)
Browse files Browse the repository at this point in the history
Co-authored-by: Desu Sai Venkat <[email protected]>
  • Loading branch information
erichoracek and desusai7 committed Jun 19, 2024
1 parent 1c3e758 commit f5005cb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
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

0 comments on commit f5005cb

Please sign in to comment.