Skip to content

Commit

Permalink
feat: Add support for insecure connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Kistler committed Jun 3, 2019
1 parent 869c50a commit 975bb68
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Source/AssistantV1/Assistant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public class Assistant {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Watson Assistant v1 service to extract
information about the error that occurred.
Expand Down
8 changes: 8 additions & 0 deletions Source/AssistantV2/Assistant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public class Assistant {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Watson Assistant v2 service to extract
information about the error that occurred.
Expand Down
8 changes: 8 additions & 0 deletions Source/CompareComplyV1/CompareComply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ public class CompareComply {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Compare and Comply service to extract
information about the error that occurred.
Expand Down
8 changes: 8 additions & 0 deletions Source/DiscoveryV1/Discovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ public class Discovery {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Discovery service to extract
information about the error that occurred.
Expand Down
8 changes: 8 additions & 0 deletions Source/LanguageTranslatorV3/LanguageTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ public class LanguageTranslator {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Language Translator service to extract
information about the error that occurred.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public class NaturalLanguageClassifier {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Natural Language Classifier service to extract
information about the error that occurred.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ public class NaturalLanguageUnderstanding {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Natural Language Understanding service to extract
information about the error that occurred.
Expand Down
8 changes: 8 additions & 0 deletions Source/PersonalityInsightsV3/PersonalityInsights.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ public class PersonalityInsights {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Personality Insights service to extract
information about the error that occurred.
Expand Down
8 changes: 8 additions & 0 deletions Source/SpeechToTextV1/SpeechToText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ public class SpeechToText {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Speech to Text service to extract
information about the error that occurred.
Expand Down
37 changes: 37 additions & 0 deletions Source/SupportingFiles/InsecureConnection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright IBM Corporation 2018
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

import Foundation

class InsecureConnection {
/// Allow network requests to a server without verification of the server certificate.
/// **IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
static func session() -> URLSession {
return URLSession(configuration: .default, delegate: AllowInsecureConnectionDelegate(), delegateQueue: nil)
}
}

/**
URLSession delegate that is used to bypass SSL certificate verification.
**IMPORTANT**: This can potentially cause dangerous security breaches, so use only if you are certain that you have taken necessary precautions.
*/
class AllowInsecureConnectionDelegate: NSObject, URLSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(URLSession.AuthChallengeDisposition.useCredential, credential)
}
}
8 changes: 8 additions & 0 deletions Source/TextToSpeechV1/TextToSpeech.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ public class TextToSpeech {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Text to Speech service to extract
information about the error that occurred.
Expand Down
8 changes: 8 additions & 0 deletions Source/ToneAnalyzerV3/ToneAnalyzer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ public class ToneAnalyzer {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Tone Analyzer service to extract
information about the error that occurred.
Expand Down
8 changes: 8 additions & 0 deletions Source/VisualRecognitionV3/VisualRecognition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public class VisualRecognition {
}
}

/**
Allow network requests to a server without verification of the server certificate.
**IMPORTANT**: This should ONLY be used if truly intended, as it is unsafe otherwise.
*/
public func disableSSLVerification() {
session = InsecureConnection.session()
}

/**
Use the HTTP response and data received by the Visual Recognition service to extract
information about the error that occurred.
Expand Down
11 changes: 11 additions & 0 deletions Tests/AssistantV1Tests/AssistantV1UnitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,17 @@ class AssistantV1UnitTests: XCTestCase {
waitForExpectations(timeout: timeout)
}

// MARK: - Allow Insecure Connections

#if !os(Linux)
func testAllowInsecureConnections() {
let assistant = Assistant(version: versionDate, username: "username", password: "password")
XCTAssertNil(assistant.session.delegate)
assistant.disableSSLVerification()
XCTAssertNotNil(assistant.session.delegate)
}
#endif

// MARK: - Inject Credentials

#if os(Linux)
Expand Down
26 changes: 26 additions & 0 deletions WatsonDeveloperCloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@
92EE4EFB219E25350008CE33 /* contract_A.pdf in Resources */ = {isa = PBXBuildFile; fileRef = 92EE4EF8219E25350008CE33 /* contract_A.pdf */; };
92EE4EFD21A470630008CE33 /* SpeechToTextUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92EE4EFC21A470630008CE33 /* SpeechToTextUnitTests.swift */; };
92FB960221FB852C00FEAD21 /* confirm.abnf in Resources */ = {isa = PBXBuildFile; fileRef = 92FB95FE21FB7FF100FEAD21 /* confirm.abnf */; };
CA03C887229C1918009503B9 /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA151FA2200EE23F00DBA44F /* carz.zip in Resources */ = {isa = PBXBuildFile; fileRef = CA151FA0200EE0CF00DBA44F /* carz.zip */; };
CA28815D20CB07D600FC992C /* WatsonCredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A6C7B3C1D8C960300CD97FA /* WatsonCredentials.swift */; };
CA28817620CB095100FC992C /* glossary.tmx in Resources */ = {isa = PBXBuildFile; fileRef = CA28817420CB095100FC992C /* glossary.tmx */; };
Expand All @@ -600,6 +601,17 @@
CA29B872222E0AE700626030 /* SyntaxResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA29B86D222E0AE700626030 /* SyntaxResult.swift */; };
CA35CD4720E3234900FACB2B /* ToneContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA35CD4620E3234800FACB2B /* ToneContent.swift */; };
CA35CD4920E3237400FACB2B /* ProfileContent.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA35CD4820E3237400FACB2B /* ProfileContent.swift */; };
CA54269B229ECBBE0077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA54269C229ECBC00077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA54269D229ECBC20077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA54269E229ECBC30077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA54269F229ECBC40077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA5426A0229ECBC80077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA5426A1229ECBC90077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA5426A2229ECBCB0077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA5426A3229ECBCC0077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA5426A4229ECBD00077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA5426A5229ECBD10077618A /* InsecureConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA03C886229C1918009503B9 /* InsecureConnection.swift */; };
CA59FDA1224AA0E000E152AE /* RowHeaderIDs.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA59FD9A224AA0DE00E152AE /* RowHeaderIDs.swift */; };
CA59FDA2224AA0E000E152AE /* TypeLabelComparison.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA59FD9B224AA0DF00E152AE /* TypeLabelComparison.swift */; };
CA59FDA3224AA0E000E152AE /* Value.swift in Sources */ = {isa = PBXBuildFile; fileRef = CA59FD9C224AA0DF00E152AE /* Value.swift */; };
Expand Down Expand Up @@ -1312,6 +1324,7 @@
B1F21DF51CE2461800393146 /* ToneScore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToneScore.swift; sourceTree = "<group>"; };
B1F21DF71CE2461800393146 /* ToneAnalyzerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToneAnalyzerTests.swift; sourceTree = "<group>"; };
B1F21DF81CE2461800393146 /* ToneAnalyzer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToneAnalyzer.swift; sourceTree = "<group>"; };
CA03C886229C1918009503B9 /* InsecureConnection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InsecureConnection.swift; path = Source/SupportingFiles/InsecureConnection.swift; sourceTree = "<group>"; };
CA151FA0200EE0CF00DBA44F /* carz.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = carz.zip; sourceTree = "<group>"; };
CA28815F20CB090700FC992C /* DeleteModelResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeleteModelResult.swift; sourceTree = "<group>"; };
CA28816020CB090700FC992C /* IdentifiableLanguage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IdentifiableLanguage.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2124,6 +2137,7 @@
children = (
92882AFF220E10B4001BC702 /* ibm-credentials.env */,
920645F4212E2880004ACCC9 /* Dependencies */,
CA03C886229C1918009503B9 /* InsecureConnection.swift */,
6800FC9C2056D7340078788A /* AssistantV1.h */,
924EE77D21505C2C0048C0E5 /* AssistantV2.h */,
928661A82191109C0064C6FD /* CompareComplyV1.h */,
Expand Down Expand Up @@ -3979,6 +3993,7 @@
683947EC202CF4F7007E3CF3 /* SemanticRolesAction.swift in Sources */,
683947FC202CF4F7007E3CF3 /* FeatureSentimentResults.swift in Sources */,
CA8E928022343610001BFABF /* KeywordsResultSentiment.swift in Sources */,
CA5426A0229ECBC80077618A /* InsecureConnection.swift in Sources */,
683947D7202CF4F7007E3CF3 /* SemanticRolesObject.swift in Sources */,
CA8E9273223435F7001BFABF /* AnalysisResultsMetadata.swift in Sources */,
683947F4202CF4F7007E3CF3 /* TargetedEmotionResults.swift in Sources */,
Expand Down Expand Up @@ -4033,6 +4048,7 @@
9206460B212E2880004ACCC9 /* opus_header.c in Sources */,
689A170E203CCDA9002CFC66 /* Word.swift in Sources */,
124C2E221D19E2FA00D9F602 /* TextToSpeech.swift in Sources */,
CA5426A3229ECBCC0077618A /* InsecureConnection.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -4118,6 +4134,7 @@
68AF8F742069690300D552E3 /* Notice.swift in Sources */,
92261EA321EFFFE800A1A620 /* Gateway.swift in Sources */,
68AF8F8A2069690300D552E3 /* UpdateCollectionRequest.swift in Sources */,
CA54269D229ECBC20077618A /* InsecureConnection.swift in Sources */,
68AF8F732069690300D552E3 /* PDFSettings.swift in Sources */,
68AF8F6D2069690300D552E3 /* QueryNoticesResponse.swift in Sources */,
68AF8F5D2069690300D552E3 /* DocumentAccepted.swift in Sources */,
Expand Down Expand Up @@ -4252,6 +4269,7 @@
92BF69AE213F129A00FE6325 /* Shared.swift in Sources */,
6800FCB22056D8D50078788A /* EntityCollection.swift in Sources */,
CADA96992110ABB500B5BD84 /* DialogNodeOutputTextValuesElement.swift in Sources */,
CA03C887229C1918009503B9 /* InsecureConnection.swift in Sources */,
6800FCA22056D8D50078788A /* Counterexample.swift in Sources */,
6800FCBE2056D8D50078788A /* MessageInput.swift in Sources */,
6800FCB12056D8D50078788A /* Entity.swift in Sources */,
Expand Down Expand Up @@ -4288,6 +4306,7 @@
689A16B3203C9946002CFC66 /* Trait.swift in Sources */,
689A16AE203C9946002CFC66 /* ContentItem.swift in Sources */,
689A16B4203C9946002CFC66 /* ConsumptionPreferences.swift in Sources */,
CA5426A1229ECBC90077618A /* InsecureConnection.swift in Sources */,
CA35CD4920E3237400FACB2B /* ProfileContent.swift in Sources */,
689A16AF203C9946002CFC66 /* ConsumptionPreferencesCategory.swift in Sources */,
689A16B2203C9946002CFC66 /* Content.swift in Sources */,
Expand Down Expand Up @@ -4326,6 +4345,7 @@
689A1694203C88C7002CFC66 /* ClassifiedImage.swift in Sources */,
689A1696203C88C7002CFC66 /* FaceAge.swift in Sources */,
689A169B203C88C7002CFC66 /* Classifier.swift in Sources */,
CA5426A5229ECBD10077618A /* InsecureConnection.swift in Sources */,
689A1692203C88C7002CFC66 /* ClassResult.swift in Sources */,
689A1691203C88C7002CFC66 /* ImageWithFaces.swift in Sources */,
689A1698203C88C7002CFC66 /* WarningInfo.swift in Sources */,
Expand Down Expand Up @@ -4366,6 +4386,7 @@
7AAAF4131CEE9D5300B74848 /* ToneScore.swift in Sources */,
68D09C22200D61870087ADE5 /* UtteranceAnalyses.swift in Sources */,
68D09C24200D61870087ADE5 /* Utterance.swift in Sources */,
CA5426A4229ECBD00077618A /* InsecureConnection.swift in Sources */,
7AAAF4121CEE9D5300B74848 /* ToneCategory.swift in Sources */,
68D09C20200D61870087ADE5 /* UtteranceAnalysis.swift in Sources */,
68D09C1F200D61870087ADE5 /* ToneChatInput.swift in Sources */,
Expand Down Expand Up @@ -4408,6 +4429,7 @@
68F7FF07207D458900E84DBF /* RecognitionJobs.swift in Sources */,
68F7FEFF207D458900E84DBF /* LanguageModel.swift in Sources */,
7A9EC80A1D79B75100507725 /* SpeechToTextEncoder.swift in Sources */,
CA5426A2229ECBCB0077618A /* InsecureConnection.swift in Sources */,
68F7FF05207D458900E84DBF /* RecognitionJob.swift in Sources */,
68F7FF0C207D458900E84DBF /* WordAlternativeResult.swift in Sources */,
68F7FF0D207D458900E84DBF /* WordError.swift in Sources */,
Expand Down Expand Up @@ -4462,6 +4484,7 @@
92BF69B3213F129A00FE6325 /* Shared.swift in Sources */,
68A6A9AF202A38490069585F /* ClassifierList.swift in Sources */,
68A6A9AD202A38490069585F /* ClassifyInput.swift in Sources */,
CA54269F229ECBC40077618A /* InsecureConnection.swift in Sources */,
68438EA0208A987400FD7DB9 /* ClassifyCollectionInput.swift in Sources */,
68438E9F208A987400FD7DB9 /* ClassificationCollection.swift in Sources */,
68A6A9AE202A38490069585F /* Classifier.swift in Sources */,
Expand Down Expand Up @@ -4489,6 +4512,7 @@
buildActionMask = 2147483647;
files = (
922078542152FCE400C8C7E4 /* MessageContextGlobal.swift in Sources */,
CA54269B229ECBBE0077618A /* InsecureConnection.swift in Sources */,
922078552152FCE400C8C7E4 /* MessageInputOptions.swift in Sources */,
922078532152FCE400C8C7E4 /* DialogLogMessage.swift in Sources */,
922078412152FCE000C8C7E4 /* Assistant.swift in Sources */,
Expand Down Expand Up @@ -4555,6 +4579,7 @@
920F0AA0219CC1E900070C5B /* UpdatedLabelsIn.swift in Sources */,
920F0AA1219CC1E900070C5B /* SectionTitle.swift in Sources */,
920F0AB5219CC1E900070C5B /* ContractAmts.swift in Sources */,
CA54269C229ECBC00077618A /* InsecureConnection.swift in Sources */,
920F0A8A219CC1E900070C5B /* ClassifyReturn.swift in Sources */,
920F0AAD219CC1E900070C5B /* TypeLabel.swift in Sources */,
920F0AB4219CC1E900070C5B /* Attribute.swift in Sources */,
Expand Down Expand Up @@ -4623,6 +4648,7 @@
CA28818220CB099D00FC992C /* IdentifiableLanguages.swift in Sources */,
CA28817F20CB099D00FC992C /* IdentifiedLanguage.swift in Sources */,
CA28818120CB099D00FC992C /* TranslateRequest.swift in Sources */,
CA54269E229ECBC30077618A /* InsecureConnection.swift in Sources */,
CA28817C20CB099D00FC992C /* TranslationResult.swift in Sources */,
CA28817B20CB099D00FC992C /* Translation.swift in Sources */,
);
Expand Down

0 comments on commit 975bb68

Please sign in to comment.