forked from Kitura/Swift-JWT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJWTError.swift
66 lines (51 loc) · 3.18 KB
/
JWTError.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* 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 Foundation
// MARK: JWTError
/// A struct representing the different errors that can be thrown by SwiftJWT
public struct JWTError: LocalizedError, Equatable {
/// A human readable description of the error.
public let errorDescription: String?
private let internalError: InternalError
private enum InternalError {
case invalidJWTString, failedVerification, osVersionToLow, invalidPrivateKey, invalidData, invalidKeyID, missingPEMHeaders
}
/// Error when an invalid JWT String is provided
public static let invalidJWTString = JWTError(errorDescription: "Input was not a valid JWT String", internalError: .invalidJWTString)
/// Error when the JWT signiture fails verification.
public static let failedVerification = JWTError(errorDescription: "JWT verifier failed to verify the JWT String signiture", internalError: .failedVerification)
/// Error when using RSA encryption with an OS version that is too low.
public static let osVersionToLow = JWTError(errorDescription: "macOS 10.12.0 (Sierra) or higher or iOS 10.0 or higher is required by CryptorRSA", internalError: .osVersionToLow)
/// Error when an invalid private key is provided for RSA encryption.
public static let invalidPrivateKey = JWTError(errorDescription: "Provided private key could not be used to sign JWT", internalError: .invalidPrivateKey)
/// Error when the provided Data cannot be decoded to a String
public static let invalidUTF8Data = JWTError(errorDescription: "Could not decode Data from UTF8 to String", internalError: .invalidData)
/// Error when the KeyID field `kid` in the JWT header fails to generate a JWTSigner or JWTVerifier
public static let invalidKeyID = JWTError(errorDescription: "The JWT KeyID `kid` header failed to generate a JWTSigner/JWTVerifier", internalError: .invalidKeyID)
/// Error when a PEM string is provided without the expected PEM headers/footers. (e.g. -----BEGIN PRIVATE KEY-----)
public static let missingPEMHeaders = JWTError(errorDescription: "The provided key did not have the expected PEM headers/footers", internalError: .missingPEMHeaders)
/// Function to check if JWTErrors are equal. Required for equatable protocol.
public static func == (lhs: JWTError, rhs: JWTError) -> Bool {
return lhs.internalError == rhs.internalError
}
/// Function to enable pattern matching against generic Errors.
public static func ~= (lhs: JWTError, rhs: Error) -> Bool {
guard let rhs = rhs as? JWTError else {
return false
}
return lhs == rhs
}
}