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

OA-4036 Add URLSession authChallenge handler #39

Merged
merged 1 commit into from
Nov 4, 2019
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
2 changes: 1 addition & 1 deletion JSONRequest.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "JSONRequest"
s.version = "0.10.0"
s.version = "0.10.1"
s.summary = "JSONRequest is a tiny Swift library for Synchronous and Asynchronous HTTP JSON requests."
s.homepage = "http://github.com/hathway/JSONRequest"
s.license = { :type => "MIT", :file => "LICENSE" }
Expand Down
4 changes: 4 additions & 0 deletions JSONRequest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
957B333D1CA43EAD00A4A2C6 /* JSONRequestDELETETests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 957B333C1CA43EAD00A4A2C6 /* JSONRequestDELETETests.swift */; };
95975B081C1E2CFF004750A8 /* JSONRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 95975B071C1E2CFF004750A8 /* JSONRequest.swift */; };
95975B0B1C1E31E0004750A8 /* JSONRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 95975B031C1E2CA0004750A8 /* JSONRequest.h */; settings = {ATTRIBUTES = (Public, ); }; };
D1FA0585237029430011424E /* JSONRequestAuthChallengeHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1FA0584237029430011424E /* JSONRequestAuthChallengeHandler.swift */; };
D4F9B0F01F268BE3004C6B15 /* testFiles in Resources */ = {isa = PBXBuildFile; fileRef = D4F9B0EF1F268BE3004C6B15 /* testFiles */; };
DA9BEACF1F2BF63700EEDC94 /* URLEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA9BEACE1F2BF63700EEDC94 /* URLEncoding.swift */; };
F5CA216D29A78D7BD6E012C3 /* Pods_JSONRequestTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3F6AFE01224BBF6E8DD4CBAD /* Pods_JSONRequestTests.framework */; };
Expand Down Expand Up @@ -58,6 +59,7 @@
95975B071C1E2CFF004750A8 /* JSONRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = JSONRequest.swift; path = JSONRequest/JSONRequest.swift; sourceTree = "<group>"; };
95975B0E1C1F39D2004750A8 /* Package.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Package.swift; sourceTree = "<group>"; };
A262DBB8FA4EFE8496CA36C8 /* Pods-JSONRequest.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-JSONRequest.debug.xcconfig"; path = "Pods/Target Support Files/Pods-JSONRequest/Pods-JSONRequest.debug.xcconfig"; sourceTree = "<group>"; };
D1FA0584237029430011424E /* JSONRequestAuthChallengeHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONRequestAuthChallengeHandler.swift; sourceTree = "<group>"; };
D4F9B0EF1F268BE3004C6B15 /* testFiles */ = {isa = PBXFileReference; lastKnownFileType = folder; path = testFiles; sourceTree = "<group>"; };
DA9BEACE1F2BF63700EEDC94 /* URLEncoding.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = URLEncoding.swift; path = JSONRequest/URLEncoding.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -156,6 +158,7 @@
95975B031C1E2CA0004750A8 /* JSONRequest.h */,
95975AFF1C1E2C68004750A8 /* Info.plist */,
95975B071C1E2CFF004750A8 /* JSONRequest.swift */,
D1FA0584237029430011424E /* JSONRequestAuthChallengeHandler.swift */,
9575F2081C448965000B5A59 /* JSONRequestVerbs.swift */,
DA9BEACE1F2BF63700EEDC94 /* URLEncoding.swift */,
);
Expand Down Expand Up @@ -353,6 +356,7 @@
files = (
9575F2091C448965000B5A59 /* JSONRequestVerbs.swift in Sources */,
95975B081C1E2CFF004750A8 /* JSONRequest.swift in Sources */,
D1FA0585237029430011424E /* JSONRequestAuthChallengeHandler.swift in Sources */,
DA9BEACF1F2BF63700EEDC94 /* URLEncoding.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
14 changes: 12 additions & 2 deletions JSONRequest/JSONRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ open class JSONRequest {
/* Used for dependency injection of outside URLSessions (keep nil to use default) */
private var urlSession: URLSession?

private static let urlSessionDelegate = JSONRequestSessionDelegate()
public static var authChallengeHandler: JSONRequestAuthChallengeHandler? {
get { return urlSessionDelegate.authChallengeHandler }
set { urlSessionDelegate.authChallengeHandler = newValue }
}

/* Set to false during testing to avoid test failures due to lack of internet access */
internal static var requireNetworkAccess = true

Expand Down Expand Up @@ -145,7 +151,9 @@ open class JSONRequest {
let urlCache: URLCache? = isURLCacheEnabled ? URLCache(memoryCapacity: capacity, diskCapacity: capacity, diskPath: nil) : nil
sessionConfig.urlCache = urlCache
JSONRequest.sessionConfigurationDelegate?(sessionConfig)
urlSession = URLSession(configuration: JSONRequest.sessionConfig)
urlSession = URLSession(configuration: JSONRequest.sessionConfig,
delegate: JSONRequest.urlSessionDelegate,
delegateQueue: .main)
}

// MARK: Non-public business logic (testable but not public outside the module)
Expand Down Expand Up @@ -228,7 +236,9 @@ open class JSONRequest {
config.timeoutIntervalForRequest = timeout
config.timeoutIntervalForResource = timeout
}
let session = URLSession(configuration: config)
let session = URLSession(configuration: config,
delegate: JSONRequest.urlSessionDelegate,
delegateQueue: .main)
if forcedTimeout == nil {
// if there isn't a custom timeout, set the member variable with this new session we've created for future use.
urlSession = session
Expand Down
21 changes: 21 additions & 0 deletions JSONRequestAuthChallengeHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// JSONRequestAuthChallengeHandler.swift
// JSONRequest
//
// Created by Yevhenii Hutorov on 04.11.2019.
// Copyright © 2019 Hathway. All rights reserved.
//

import Foundation

public protocol JSONRequestAuthChallengeHandler {
func handle(_ session: URLSession, challenge: URLAuthenticationChallenge) -> URLSession.AuthChallengeDisposition
}

class JSONRequestSessionDelegate: NSObject, URLSessionDelegate {
var authChallengeHandler: JSONRequestAuthChallengeHandler?
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let disposition = authChallengeHandler?.handle(session, challenge: challenge) ?? .performDefaultHandling
completionHandler(disposition, nil)
}
}