-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from vapor/signer-access-through-config
Signer access through config
- Loading branch information
Showing
10 changed files
with
159 additions
and
91 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import JWT | ||
import Vapor | ||
|
||
// MARK: Signer map access | ||
|
||
private let jwtSignersKey = "jwt-provider:signers" | ||
|
||
extension Config { | ||
public internal(set) var signers: SignerMap? { | ||
get { return storage[jwtSignersKey] as? SignerMap } | ||
set { storage[jwtSignersKey] = newValue } | ||
} | ||
|
||
/// Returns the JWT signers | ||
/// or throws an error if not properly configured | ||
public func assertSigners() throws -> SignerMap { | ||
guard let signers = self.signers else { | ||
throw JWTProviderError.noJWTSigner | ||
} | ||
|
||
return signers | ||
} | ||
|
||
/// Returns the JWT signer with the supplied identifier key | ||
public func assertSigner(kid: String) throws -> Signer { | ||
let signers = try assertSigners() | ||
guard let signer = signers[kid] else { | ||
throw JWTProviderError.noJWTSigner | ||
} | ||
return signer | ||
} | ||
} | ||
|
||
// MARK: JSON Web Key Set (JWKS) URL access | ||
|
||
private let jwtJWKSURLKey = "jwt-provider:jwks-url" | ||
|
||
extension Config { | ||
|
||
/// Returns the JWKS URL | ||
public internal(set) var jwksURL: String? { | ||
get { return storage[jwtJWKSURLKey] as? String } | ||
set { storage[jwtJWKSURLKey] = newValue } | ||
} | ||
|
||
/// Returns the JWKS URL | ||
/// or throws an error if not properly configured | ||
public func assertJWKSURL() throws -> String { | ||
guard let jwksURL = jwksURL else { | ||
throw JWTProviderError.noJWTSigner | ||
} | ||
|
||
return jwksURL | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import JWT | ||
import Vapor | ||
|
||
extension Droplet { | ||
|
||
/// Returns the JWT signer | ||
@available(*, deprecated, message: "Use signers instead.") | ||
public internal(set) var signer: Signer? { | ||
get { return signers?.legacySigner } | ||
set { | ||
if let signer = newValue { | ||
if signers != nil { | ||
signers?.legacySigner = signer | ||
} else { | ||
signers = SignerMap(legacySigner: signer) | ||
} | ||
} else { | ||
signers?.legacySigner = nil | ||
} | ||
} | ||
} | ||
|
||
/// Returns the main JWT signer | ||
/// or throws an error if not properly configured | ||
public func assertSigner() throws -> Signer { | ||
// NB. duplicated code from `signer` is necessary | ||
// to prevent deprecation warning | ||
guard let signer = signers?.legacySigner else { | ||
throw JWTProviderError.noJWTSigner | ||
} | ||
|
||
return signer | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Sources/JWTProvider/Deprecated/Provider+legacySigner.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import JWT | ||
import Vapor | ||
|
||
extension Provider { | ||
@available(*, deprecated, message: "Use signers instead.") | ||
public var signer: Signer { | ||
if let legacySigner = signers?.legacySigner { | ||
return legacySigner | ||
} else if let signer = signers?.first?.value { | ||
return signer | ||
} else { | ||
fatalError("Trying to access a legacy signer when none has been specified.") | ||
} | ||
} | ||
|
||
@available(*, deprecated, message: "Use init(signers: SignerMap) instead.") | ||
public convenience init(signer: Signer) { | ||
self.init(signers: SignerMap(legacySigner: signer)) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Sources/JWTProvider/Deprecated/SignerMap+legacySigner.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import JWT | ||
|
||
private let jwtLegacySignerKey = "jwt-providers:legacy-signer" | ||
|
||
internal extension Dictionary where Key == String, Value == Signer { | ||
internal init(legacySigner: Signer) { | ||
self = [jwtLegacySignerKey: legacySigner] | ||
} | ||
|
||
var legacySigner: Signer? { | ||
get { | ||
return self[jwtLegacySignerKey] | ||
} | ||
set { | ||
self[jwtLegacySignerKey] = newValue | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,48 @@ | ||
import Vapor | ||
// `Signer`s used to be stored on the `Droplet`'s storage. | ||
// They have since been moved to `Config` to enable access | ||
// to signers in `Configinitializable` objects | ||
// (eg. `Providers`). | ||
// This file makes sure the `Signer`s can still be accessed | ||
// through from the Droplet. | ||
|
||
import JWT | ||
import Vapor | ||
|
||
let jwtLegacySignerKey = "jwt-providers:legacy-signer" | ||
// MARK: Signer map access (via Config) | ||
|
||
extension Droplet { | ||
@available(*, deprecated, message: "Use signers instead.") | ||
public internal(set) var signer: Signer? { | ||
get { return self.signers?[jwtLegacySignerKey] } | ||
set { | ||
if let signer = newValue { | ||
|
||
if self.signers != nil { | ||
self.signers?[jwtLegacySignerKey] = signer | ||
} else { | ||
self.signers = [jwtLegacySignerKey: signer] | ||
} | ||
} else { | ||
self.signers?[jwtLegacySignerKey] = nil | ||
} | ||
} | ||
} | ||
|
||
/// Returns the JWT signer | ||
/// or throws an error if not properly configured | ||
@available(*, deprecated, message: "Use assertSigner(kid:) or assertSigners() instead.") | ||
public func assertSigner() throws -> Signer { | ||
guard let signer = self.signer else { | ||
throw JWTProviderError.noJWTSigner | ||
} | ||
|
||
return signer | ||
} | ||
} | ||
|
||
private let jwtSignersKey = "jwt-provider:signers" | ||
|
||
extension Droplet { | ||
/// Returns the JWT signers | ||
public internal(set) var signers: SignerMap? { | ||
get { return storage[jwtSignersKey] as? SignerMap } | ||
set { storage[jwtSignersKey] = newValue } | ||
get { return config.signers } | ||
set { config.signers = newValue } | ||
} | ||
|
||
/// Returns the JWT signers | ||
/// or throws an error if not properly configured | ||
public func assertSigners() throws -> SignerMap { | ||
guard let signers = self.signers else { | ||
throw JWTProviderError.noJWTSigner | ||
} | ||
|
||
return signers | ||
return try config.assertSigners() | ||
} | ||
|
||
/// Returns the JWT signer with the supplied identifier key | ||
public func assertSigner(kid: String) throws -> Signer { | ||
let signers = try assertSigners() | ||
guard let signer = signers[kid] else { | ||
throw JWTProviderError.noJWTSigner | ||
} | ||
return signer | ||
return try config.assertSigner(kid: kid) | ||
} | ||
} | ||
|
||
private let jwtJWKSURLKey = "jwt-provider:jwks-url" | ||
// MARK: JSON Web Key Set (JWKS) URL access (via Config) | ||
|
||
extension Droplet { | ||
|
||
/// Returns the JWKS URL | ||
public internal(set) var jwksURL: String? { | ||
get { return storage[jwtJWKSURLKey] as? String } | ||
set { storage[jwtJWKSURLKey] = newValue } | ||
get { return config.jwksURL } | ||
set { config.jwksURL = newValue } | ||
} | ||
|
||
/// Returns the JWKS URL | ||
/// or throws an error if not properly configured | ||
public func assertJWKSURL() throws -> String { | ||
guard let jwksURL = self.jwksURL else { | ||
throw JWTProviderError.noJWTSigner | ||
} | ||
|
||
return jwksURL | ||
return try config.assertJWKSURL() | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import Foundation | ||
import JWT | ||
|
||
public protocol SignerFactory { | ||
|
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
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