Skip to content

Commit

Permalink
Fix parsing of url with flags Fix OAuthSwift#454
Browse files Browse the repository at this point in the history
  • Loading branch information
e-marchand committed Apr 17, 2018
1 parent 5577014 commit 4a2ba34
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 30 deletions.
10 changes: 9 additions & 1 deletion OAuthSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
0738BC6B1EBA755C000FA2F2 /* OAuthSwiftCredentialTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0738BC6A1EBA755C000FA2F2 /* OAuthSwiftCredentialTests.swift */; };
48942B5B2085DB5F00376BFA /* URLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 48942B5A2085DB5F00376BFA /* URLTests.swift */; };
6053EF6F1E93821400EB28B3 /* NotificationCenter+OAuthSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6053EF6E1E93821400EB28B3 /* NotificationCenter+OAuthSwift.swift */; };
6053EF701E93832400EB28B3 /* NotificationCenter+OAuthSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6053EF6E1E93821400EB28B3 /* NotificationCenter+OAuthSwift.swift */; };
6053EF711E93832500EB28B3 /* NotificationCenter+OAuthSwift.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6053EF6E1E93821400EB28B3 /* NotificationCenter+OAuthSwift.swift */; };
Expand Down Expand Up @@ -200,6 +201,7 @@
/* Begin PBXFileReference section */
0738BC6A1EBA755C000FA2F2 /* OAuthSwiftCredentialTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuthSwiftCredentialTests.swift; sourceTree = "<group>"; };
0ED45F76F07F16FB6FF639B7 /* Pods_OAuthSwiftTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_OAuthSwiftTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
48942B5A2085DB5F00376BFA /* URLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLTests.swift; sourceTree = "<group>"; };
6053EF6E1E93821400EB28B3 /* NotificationCenter+OAuthSwift.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NotificationCenter+OAuthSwift.swift"; sourceTree = "<group>"; };
674EB2AA3430160EBBAC72A1 /* Pods-OAuthSwiftTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-OAuthSwiftTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-OAuthSwiftTests/Pods-OAuthSwiftTests.debug.xcconfig"; sourceTree = "<group>"; };
C40890C01C11B37000E3146A /* OAuthSwift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OAuthSwift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -479,6 +481,7 @@
C4FAE4351C061660000CE669 /* SignTests.swift */,
C40C96D81DA639B700197628 /* UtilsTests.swift */,
C44244DA1C06316100880DAB /* ServicesTests.swift */,
48942B5A2085DB5F00376BFA /* URLTests.swift */,
C4D50DFB1BFB693F0053B624 /* Info.plist */,
C4472DE51C07235800B95B4B /* ServicesTest.plist */,
C4D50E021BFB6A340053B624 /* TestServer.swift */,
Expand Down Expand Up @@ -750,7 +753,7 @@
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0710;
LastUpgradeCheck = 0900;
LastUpgradeCheck = 0930;
ORGANIZATIONNAME = "Dongri Jin";
TargetAttributes = {
C40890BF1C11B37000E3146A = {
Expand Down Expand Up @@ -1067,6 +1070,7 @@
C4E46C511D462ED300BFCEF4 /* OAuthSwiftErrorTest.swift in Sources */,
C4D50E031BFB6A340053B624 /* TestServer.swift in Sources */,
C44244DB1C06316100880DAB /* ServicesTests.swift in Sources */,
48942B5B2085DB5F00376BFA /* URLTests.swift in Sources */,
C4D50E0D1BFB79D00053B624 /* OAuth2SwiftTests.swift in Sources */,
C40C96D91DA639B700197628 /* UtilsTests.swift in Sources */,
C4FAE4361C061660000CE669 /* SignTests.swift in Sources */,
Expand Down Expand Up @@ -1491,12 +1495,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down Expand Up @@ -1545,12 +1551,14 @@
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
Expand All @@ -46,7 +45,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,9 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down Expand Up @@ -57,7 +56,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
Expand All @@ -37,7 +36,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0900"
LastUpgradeVersion = "0930"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,7 +26,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
Expand All @@ -37,7 +36,6 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
8 changes: 8 additions & 0 deletions OAuthSwift.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
1 change: 1 addition & 0 deletions OAuthSwiftTests/SignTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class SignTests: XCTestCase {
for _ in 0..<tolerance {
let nonce = OAuthSwiftCredential.generateNonce()
dico[nonce] = ""
XCTAssertEqual(nonce.count, 8)
}

XCTAssertEqual(Double(tolerance), Double(dico.count), accuracy: 10)
Expand Down
54 changes: 54 additions & 0 deletions OAuthSwiftTests/URLTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// URLTests.swift
// OAuthSwiftTests
//
// Created by Eric Marchand on 17/04/2018.
// Copyright © 2018 Dongri Jin. All rights reserved.
//

import Foundation

import XCTest
@testable import OAuthSwift

class URLTest: XCTestCase {

func testNoQuery() {
let url = URL(string: "http://localhost/")
var responseParameters = [String: String]()
if let query = url?.query {
responseParameters += query.parametersFromQueryString
}
XCTAssertTrue(responseParameters.isEmpty)
}
func testWithQuery() {
let url = URL(string: "http://localhost/?code=azeaze")
var responseParameters = [String: String]()
if let query = url?.query {
responseParameters += query.parametersFromQueryString
}
XCTAssertFalse(responseParameters.isEmpty)
XCTAssertNotNil(responseParameters["code"])
}

func testWithQueryWithFlag() {
let url = URL(string: "http://localhost/?close&code=azeaze")
var responseParameters = [String: String]()
if let query = url?.query {
responseParameters += query.parametersFromQueryString
}
XCTAssertFalse(responseParameters.isEmpty)
XCTAssertNotNil(responseParameters["code"])
}

func testWithQueryWithMultipleFlag() {
let url = URL(string: "http://localhost/?close&test&code=azeaze&flag")
var responseParameters = [String: String]()
if let query = url?.query {
responseParameters += query.parametersFromQueryString
}
XCTAssertFalse(responseParameters.isEmpty)
XCTAssertNotNil(responseParameters["code"])
}

}
4 changes: 2 additions & 2 deletions Sources/OAuthSwiftCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ open class OAuthSwiftCredential: NSObject, NSCoding, Codable {
}

open class func generateNonce() -> String {
let uuidString = UUID().uuidString
return uuidString.substring(to: 8)
let uuidString: String = UUID().uuidString
return uuidString[0..<8]
}

open func authorizationHeader(method: OAuthSwiftHTTPRequest.Method, url: URL, parameters: OAuthSwift.Parameters, body: Data? = nil, timestamp: String, nonce: String) -> String {
Expand Down
37 changes: 24 additions & 13 deletions Sources/String+OAuthSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,22 @@ extension String {
scanner.scanUpTo(elementSeparator, into: &value)
scanner.scanString(elementSeparator, into: nil)

if let key = key as String?, let value = value as String? {
parameters.updateValue(value, forKey: key)
if let key = key as String? {
if let value = value as String? {
if key.contains(elementSeparator) {
var keys = key.components(separatedBy: elementSeparator)
if let key = keys.popLast() {
parameters.updateValue(value, forKey: String(key))
}
for flag in keys {
parameters.updateValue("", forKey: flag)
}
} else {
parameters.updateValue(value, forKey: key)
}
} else {
parameters.updateValue("", forKey: key)
}
}
}

Expand All @@ -81,23 +95,20 @@ extension String {
return self.removingPercentEncoding ?? self
}

var droppedLast: String {
let to = self.index(before: self.endIndex)
return String(self[..<to])
}

mutating func dropLast() {
self.remove(at: self.index(before: self.endIndex))
}

func substring(to offset: String.IndexDistance) -> String {
let to = self.index(self.startIndex, offsetBy: offset)
return String(self[..<to])
subscript (bounds: CountableClosedRange<Int>) -> String {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return String(self[start...end])
}

func substring(from offset: String.IndexDistance) -> String {
let from = self.index(self.startIndex, offsetBy: offset)
return String(self[from...])
subscript (bounds: CountableRange<Int>) -> String {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
return String(self[start..<end])
}
}

Expand Down

0 comments on commit 4a2ba34

Please sign in to comment.