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

Authorization Error: unsupported URL #362

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
71 changes: 71 additions & 0 deletions Sources/OktaOidc/Common/OktaOidcConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,85 @@ public class OktaOidcConfig: NSObject {
self.additionalParams == config.additionalParams
}

class URLHandler: URLProtocol {

private static let requestIdentifier = Bundle.main.bundleIdentifier ?? "unknown bundle identifier"

class RuntimeError: Error {
init(_ error: String) {
}
}

override class func canInit(with request: URLRequest) -> Bool {
guard request.url?.scheme == requestIdentifier && request.url?.scheme != "unknown bundle identifier" else {
return false
}

guard let handled = URLProtocol.property(forKey: URLHandler.requestIdentifier, in: request) as? Bool else {
return true
}

return !handled
}

override func startLoading() {
guard let request = (self.request as NSURLRequest).mutableCopy() as? NSMutableURLRequest else {
return
}

URLProtocol.setProperty(true, forKey: URLHandler.requestIdentifier, in: request)

DispatchQueue.global(qos: .background).async {

guard let url = request.url, let headers = request.allHTTPHeaderFields else {
self.client?.urlProtocol(self, didFailWithError: RuntimeError("URLHandler - Invalid URL."))
return
}

guard let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: "HTTP/1.1", headerFields: headers) else {
self.client?.urlProtocol(self, didFailWithError: RuntimeError("URLHandler - Invalid Response."))
return
}


let json: [String: Any] = ["key": "value"]

do {
let data = try JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
self.client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
self.client?.urlProtocol(self, didLoad: data as Data)
self.client?.urlProtocolDidFinishLoading(self)
} catch {
self.client?.urlProtocol(self, didFailWithError: error)
}
}
}

override func stopLoading() {

}

override class func canonicalRequest(for request: URLRequest) -> URLRequest {
return request
}

override class func requestIsCacheEquivalent(_ a: URLRequest, to b: URLRequest) -> Bool {
return super.requestIsCacheEquivalent(a, to: b)
}
}

class func setupURLSession() {
/*
Setup auth session to block redirection because authorization request
implies redirection and passing authCode as a query parameter.
*/
var protocolClasses = [AnyClass]()
protocolClasses.append(URLHandler.self)

let config = URLSessionConfiguration.default
config.httpShouldSetCookies = false
config.httpAdditionalHeaders = [[OktaUserAgent.userAgentHeaderKey()]: [OktaUserAgent.userAgentHeaderValue()]]
config.protocolClasses = protocolClasses

let session = URLSession(
configuration: config,
Expand Down