Skip to content

Commit

Permalink
Support Swift 4
Browse files Browse the repository at this point in the history
  • Loading branch information
djones6 committed Aug 7, 2019
1 parent e233417 commit 5ff44c8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.0.2
32 changes: 32 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.


import PackageDescription

let package = Package(
name: "Kitura-CredentialsJWT",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "CredentialsJWT",
targets: ["CredentialsJWT"]
)
],
dependencies: [
.package(url: "https://github.com/IBM-Swift/Kitura-Credentials.git", from: "2.4.0"),
.package(url: "https://github.com/IBM-Swift/Swift-JWT.git", from: "3.1.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "CredentialsJWT",
dependencies: ["Credentials", "SwiftJWT"]
),
// .testTarget(
// name: "CredentialsJWTTests",
// dependencies: ["CredentialsJWT"]
// )
]
)
6 changes: 6 additions & 0 deletions Sources/CredentialsJWT/AnyHashableMetatype.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ struct AnyHashableMetatype: Hashable {
self.base = base
}

#if swift(>=4.2)
func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(base))
}
#else
var hashValue: Int {
return ObjectIdentifier(base).hashValue
}
#endif
}
10 changes: 5 additions & 5 deletions Sources/CredentialsJWT/TypeSafeJWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ extension JWT: TypeSafeCredentials {
}

private static var usersCache: NSCache<NSString, JWTCacheElement<T>> {
let key = String(reflecting: Self.self)
let key = String(reflecting: JWT.self)
if let usersCache = TypeSafeJWTCache<T>.cacheForType[key] {
return usersCache
} else {
Expand Down Expand Up @@ -134,13 +134,13 @@ extension JWT: TypeSafeCredentials {
onSuccess(jwt)
}

static func getFromCache(token: String) -> Self? {
static func getFromCache(token: String) -> JWT? {
#if os(Linux)
let key = NSString(string: token)
#else
let key = token as NSString
#endif
guard let cacheElement = Self.usersCache.object(forKey: key) else {
guard let cacheElement = JWT.usersCache.object(forKey: key) else {
Log.debug("Cached token not found: \(token)")
return nil
}
Expand All @@ -155,13 +155,13 @@ extension JWT: TypeSafeCredentials {
return cacheElement.userProfile
}

static func saveInCache(profile: Self, token: String) {
static func saveInCache(profile: JWT, token: String) {
#if os(Linux)
let key = NSString(string: token)
#else
let key = token as NSString
#endif
Self.usersCache.setObject(JWTCacheElement(profile: profile), forKey: key)
JWT.usersCache.setObject(JWTCacheElement(profile: profile), forKey: key)
Log.debug("Token added to cache: \(token)")
}
}

0 comments on commit 5ff44c8

Please sign in to comment.