Created
June 1, 2018 01:29
-
-
Save Andrew-Lees11/9e0f56de9baa26f167181928023be90f to your computer and use it in GitHub Desktop.
MultipleTypeSafeCredentials
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
/** | |
* 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 Kitura | |
import Foundation | |
public protocol MultipleTypeSafeCredentials: TypeSafeMiddleware { | |
static var auths: [TypeSafeCredentialsPluginProtocol.Type] { get } | |
init(successAuth: TypeSafeCredentialsPluginProtocol) | |
} | |
extension MultipleTypeSafeCredentials { | |
public static func handle(request: RouterRequest, response: RouterResponse, completion: @escaping (Self?, RequestError?) -> Void) { | |
for auth in auths { | |
auth.authenticate(request: request, response: response, onSuccess: { (successAuth) in | |
return completion(Self(successAuth: successAuth),nil) | |
}, onFailure: { (_, _) in | |
}, onPass: { (_, _) in | |
}) { | |
} | |
} | |
completion(nil, .unauthorized) | |
} | |
} | |
public final class MultiAuth: TypeSafeMiddleware { | |
public let id: String | |
public let provider: String | |
public static let auths: [TypeSafeCredentialsPluginProtocol.Type] = [DefaultTypeSafeFacebookToken.self, UserHTTPBasic.self] | |
public init(successAuth: TypeSafeCredentialsPluginProtocol){ | |
self.id = successAuth.id | |
self.provider = successAuth.provider | |
} | |
} | |
public final class MultiAuthNoId: TypeSafeMiddleware { | |
let successName: String | |
let id: String | |
static let auths: [TypeSafeCredentialsPluginProtocol.Type] = [DefaultTypeSafeFacebookToken.self, UserHTTPBasic.self] | |
init(successAuth: TypeSafeCredentialsPluginProtocol){ | |
switch(successAuth.self) { | |
case let facebook as DefaultTypeSafeFacebookToken: | |
self.id = facebook.id | |
self.successName = String(describing: facebook) | |
case let basic as UserHTTPBasic: | |
self.id = basic.id | |
self.successName = String(describing: basic) | |
default: | |
self.id = "failed" | |
self.successName = "failed" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment