Skip to content

Commit

Permalink
Migration 0823 (#18)
Browse files Browse the repository at this point in the history
* Kitura/Kitura#675 Test module name change

* Kitura/Kitura#675 Updated swift version

* Kitura/Kitura#675 Swift language changes

* Kitura/Kitura#675 Options now a Dictionary<String, Any> on all platforms

* IBM-Swift/Kitra#65 Changes to Tests due to Credentials API change

* Kitura/Kitura#675 Migrate from KituraSys.Queue to Dispatch

* Kitura/Kitura#675 Changes due to @escaping issues

* Kitura/Kitura#697 Updated Swift version

* Kitura/Kitura#697 Swift language changes

* Kitura/Kitura#697 Linux Foundation changes

* Kitura/Kitura#697 Updated dependencies
  • Loading branch information
shmuelk committed Aug 28, 2016
1 parent 80b209a commit f2ead2d
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DEVELOPMENT-SNAPSHOT-2016-07-25-a
DEVELOPMENT-SNAPSHOT-2016-08-23-a
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import PackageDescription
let package = Package(
name: "Kitura-Credentials",
dependencies: [
.Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 0, minor: 26),
.Package(url: "https://github.com/IBM-Swift/Kitura-Session.git", majorVersion: 0, minor: 26)
.Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 0, minor: 27),
.Package(url: "https://github.com/IBM-Swift/Kitura-Session.git", majorVersion: 0, minor: 27)
]
)
24 changes: 7 additions & 17 deletions Sources/Credentials/Credentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,21 @@ import Foundation

import SwiftyJSON

#if os(Linux)
public typealias OptionValue = Any
#else
public typealias OptionValue = AnyObject
#endif

public class Credentials : RouterMiddleware {

var nonRedirectingPlugins = [CredentialsPluginProtocol]()
var redirectingPlugins = [String : CredentialsPluginProtocol]()
public var options : [String:OptionValue]
public var options : [String:Any]

public convenience init () {
self.init(options: [String:OptionValue]())
self.init(options: [String:Any]())
}

public init (options: [String:OptionValue]) {
public init (options: [String:Any]) {
self.options = options
}

public func handle(request: RouterRequest, response: RouterResponse, next: () -> Void) {
public func handle(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) {
if let session = request.session {
if let _ = request.userProfile {
next()
Expand Down Expand Up @@ -99,7 +93,7 @@ public class Credentials : RouterMiddleware {
else {
// All the plugins passed
if let session = request.session, !self.redirectingPlugins.isEmpty {
session["returnTo"] = JSON(request.originalURL as OptionValue)
session["returnTo"] = JSON(request.originalURL)
self.redirectUnauthorized(response: response)
}
else {
Expand Down Expand Up @@ -136,11 +130,7 @@ public class Credentials : RouterMiddleware {
}
else {
nonRedirectingPlugins.append(plugin)
#if os(OSX)
nonRedirectingPlugins[nonRedirectingPlugins.count - 1].usersCache = NSCache()
#else
nonRedirectingPlugins[nonRedirectingPlugins.count - 1].usersCache = Cache()
#endif
nonRedirectingPlugins[nonRedirectingPlugins.count - 1].usersCache = NSCache()
}
}

Expand Down Expand Up @@ -209,7 +199,7 @@ public class Credentials : RouterMiddleware {
profile["displayName"] = userProfile.displayName
profile["provider"] = credentialsType
profile["id"] = userProfile.id
session["userProfile"] = JSON(profile as OptionValue)
session["userProfile"] = JSON(profile)

var redirect : String?
if session["returnTo"].type != .null {
Expand Down
10 changes: 5 additions & 5 deletions Sources/Credentials/CredentialsPluginProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import Foundation

public protocol CredentialsPluginProtocol {
var name: String { get }
#if os(Linux)
var usersCache: Cache? { get set }
#else
var usersCache: NSCache<NSString, BaseCacheElement>? { get set }
#endif
var redirecting: Bool { get }

func authenticate (request: RouterRequest, response: RouterResponse, options: [String:OptionValue], onSuccess: (UserProfile) -> Void, onFailure: (HTTPStatusCode?, [String:String]?) -> Void, onPass: (HTTPStatusCode?, [String:String]?) -> Void, inProgress: () -> Void)
func authenticate (request: RouterRequest, response: RouterResponse,
options: [String:Any], onSuccess: @escaping (UserProfile) -> Void,
onFailure: @escaping (HTTPStatusCode?, [String:String]?) -> Void,
onPass: @escaping (HTTPStatusCode?, [String:String]?) -> Void,
inProgress: @escaping () -> Void)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ public class BadSessionPlugin : CredentialsPluginProtocol {
return true
}

#if os(OSX)
public var usersCache : NSCache<NSString, BaseCacheElement>?
#else
public var usersCache : Cache?
#endif

public init (clientId: String, clientSecret : String, callbackUrl : String) {
self.clientId = clientId
self.clientSecret = clientSecret
self.callbackUrl = callbackUrl
}

public func authenticate (request: RouterRequest, response: RouterResponse, options: [String:OptionValue], onSuccess: (UserProfile) -> Void, onFailure: (HTTPStatusCode?, [String:String]?) -> Void, onPass: (HTTPStatusCode?, [String:String]?) -> Void, inProgress: () -> Void) {
public func authenticate (request: RouterRequest, response: RouterResponse,
options: [String:Any], onSuccess: @escaping (UserProfile) -> Void,
onFailure: @escaping (HTTPStatusCode?, [String:String]?) -> Void,
onPass: @escaping (HTTPStatusCode?, [String:String]?) -> Void,
inProgress: @escaping () -> Void) {
if let code = request.queryParameters["code"], code == "123" {
onFailure(nil, nil)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import XCTest

import Kitura
import KituraNet
import KituraSys

import Foundation
import Dispatch


protocol CredentialsTest {
Expand All @@ -34,15 +34,15 @@ extension CredentialsTest {
// sleep(10)
}

func performServerTest(router: ServerDelegate, asyncTasks: (expectation: XCTestExpectation) -> Void...) {
func performServerTest(router: ServerDelegate, asyncTasks: @escaping (XCTestExpectation) -> Void...) {
let server = setupServer(port: 8090, delegate: router)
sleep(10)
let requestQueue = Queue(type: QueueType.serial)
let requestQueue = DispatchQueue(label: "Request queue")

for (index, asyncTask) in asyncTasks.enumerated() {
let expectation = self.expectation(index)
requestQueue.enqueueAsynchronously {
asyncTask(expectation: expectation)
requestQueue.sync {
asyncTask(expectation)
}
}

Expand All @@ -53,15 +53,17 @@ extension CredentialsTest {
}
}

func performRequest(method: String, host: String = "localhost", path: String, callback: ClientRequest.Callback, headers: [String: String]? = nil, requestModifier: ((ClientRequest) -> Void)? = nil) {
func performRequest(method: String, host: String = "localhost", path: String, callback: @escaping ClientRequest.Callback, headers: [String: String]? = nil, requestModifier: ((ClientRequest) -> Void)? = nil) {
var allHeaders = [String: String]()
if let headers = headers {
for (headerName, headerValue) in headers {
allHeaders[headerName] = headerValue
}
}
allHeaders["Content-Type"] = "text/plain"
let req = HTTP.request([.method(method), .hostname(host), .port(8090), .path(path), .headers(allHeaders)], callback: callback)
let options: [ClientRequest.Options] =
[.method(method), .hostname(host), .port(8090), .path(path), .headers(allHeaders)]
let req = HTTP.request(options, callback: callback)
if let requestModifier = requestModifier {
requestModifier(req)
}
Expand All @@ -76,7 +78,7 @@ extension CredentialsTest {

extension XCTestCase: CredentialsTest {
func expectation(_ index: Int) -> XCTestExpectation {
let expectationDescription = "\(self.dynamicType)-\(index)"
let expectationDescription = "\(type(of: self))-\(index)"
return self.expectation(description: expectationDescription)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ public class DummySessionPlugin : CredentialsPluginProtocol {
return true
}

#if os(OSX)
public var usersCache : NSCache<NSString, BaseCacheElement>?
#else
public var usersCache : Cache?
#endif

public init (clientId: String, clientSecret : String, callbackUrl : String) {
self.clientId = clientId
self.clientSecret = clientSecret
self.callbackUrl = callbackUrl
}

public func authenticate (request: RouterRequest, response: RouterResponse, options: [String:OptionValue], onSuccess: (UserProfile) -> Void, onFailure: (HTTPStatusCode?, [String:String]?) -> Void, onPass: (HTTPStatusCode?, [String:String]?) -> Void, inProgress: () -> Void) {
public func authenticate (request: RouterRequest, response: RouterResponse,
options: [String:Any], onSuccess: @escaping (UserProfile) -> Void,
onFailure: @escaping (HTTPStatusCode?, [String:String]?) -> Void,
onPass: @escaping (HTTPStatusCode?, [String:String]?) -> Void,
inProgress: @escaping () -> Void) {

if let code = request.queryParameters["code"], code == "123" {
let userProfile = UserProfile(id: "123", displayName: "Dummy User", provider: self.name, name: UserProfile.UserProfileName(familyName: "User", givenName: "Dummy", middleName: "Dummy"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ public class DummyTokenPlugin : CredentialsPluginProtocol {

public init () {}

#if os(OSX)
public var usersCache : NSCache<NSString, BaseCacheElement>?
#else
public var usersCache : Cache?
#endif

public func authenticate (request: RouterRequest, response: RouterResponse, options: [String:OptionValue], onSuccess: (UserProfile) -> Void, onFailure: (HTTPStatusCode?, [String:String]?) -> Void, onPass: (HTTPStatusCode?, [String:String]?) -> Void, inProgress: () -> Void) {
public func authenticate (request: RouterRequest, response: RouterResponse,
options: [String:Any], onSuccess: @escaping (UserProfile) -> Void,
onFailure: @escaping (HTTPStatusCode?, [String:String]?) -> Void,
onPass: @escaping (HTTPStatusCode?, [String:String]?) -> Void,
inProgress: @escaping () -> Void) {
if let type = request.headers["X-token-type"], type == name {
if let token = request.headers["access_token"], token == "dummyToken123" {
let userProfile = UserProfile(id: "123", displayName: "Dummy User", provider: self.name)
let newCacheElement = BaseCacheElement(profile: userProfile)
self.usersCache!.setObject(newCacheElement, forKey: token.bridge())
#if os(OSX)
self.usersCache!.setObject(newCacheElement, forKey: token as NSString)
#else
self.usersCache!.setObject(newCacheElement, forKey: NSString(string: token))
#endif
onSuccess(userProfile)
}
else {
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import XCTest

@testable import CredentialsTestSuite
@testable import CredentialsTests


XCTMain([
Expand Down

0 comments on commit f2ead2d

Please sign in to comment.