-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using dedicated session for feeds, centralised auth header creation.
- Loading branch information
Showing
10 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) Erik Doernenburg and contributors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use these files except in compliance with the License. | ||
*/ | ||
|
||
import Foundation | ||
|
||
extension URLRequest { | ||
|
||
public static func basicAuthValue(user: String, password: String) -> String { | ||
let credentialString = "\(user):\(password)" | ||
guard let credentialData = credentialString.data(using: .utf8) else { | ||
// TODO: Consider adding error handling here | ||
return "" | ||
} | ||
let credentialAsBase64 = credentialData.base64EncodedString(options: []) | ||
return "Basic \(credentialAsBase64)" | ||
} | ||
|
||
public static func bearerAuthValue(token: String) -> String { | ||
return "Bearer \(token)" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) Erik Doernenburg and contributors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use these files except in compliance with the License. | ||
*/ | ||
|
||
import AppKit | ||
|
||
extension URLSession { | ||
|
||
public static var feedSession = makeFeedSession() | ||
|
||
private static func makeFeedSession() -> URLSession { | ||
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: FeedSessionDelegate(), delegateQueue: nil) | ||
|
||
return session | ||
} | ||
} | ||
|
||
class FeedSessionDelegate: NSObject, URLSessionTaskDelegate { | ||
|
||
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping @Sendable (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { | ||
|
||
let authMethod = challenge.protectionSpace.authenticationMethod | ||
guard authMethod == NSURLAuthenticationMethodHTTPBasic else { | ||
completionHandler(.performDefaultHandling, nil) | ||
return | ||
} | ||
|
||
// TODO: figure out what to do if we end up here | ||
// We should only end up here if we didn't provide credentials but the server requires | ||
// authentication or if the credentials provided are not accepted. Note: Realistically | ||
// this is the only place to discover the authentication realm the server uses, should | ||
// we need to expose that to the user. | ||
|
||
debugPrint("received authentication challenge for \(challenge.protectionSpace)") | ||
completionHandler(.performDefaultHandling, nil) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright (c) Erik Doernenburg and contributors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
* not use these files except in compliance with the License. | ||
*/ | ||
|
||
import XCTest | ||
@testable import CCMenu | ||
|
||
class URLRequestExtensionTests: XCTestCase { | ||
|
||
func testCreatesUnicodeBasicAuthHeader() throws { | ||
let value = URLRequest.basicAuthValue(user: "test", password: "\u{1F600}") | ||
XCTAssertEqual("Basic dGVzdDrwn5iA", value) | ||
} | ||
|
||
} | ||
|