|
| 1 | +/* |
| 2 | + SFOAuthErrorCode.swift |
| 3 | + SalesforceSDKCore |
| 4 | + |
| 5 | + Copyright (c) 2026-present, salesforce.com, inc. All rights reserved. |
| 6 | + |
| 7 | + Redistribution and use of this software in source and binary forms, with or without modification, |
| 8 | + are permitted provided that the following conditions are met: |
| 9 | + * Redistributions of source code must retain the above copyright notice, this list of conditions |
| 10 | + and the following disclaimer. |
| 11 | + * Redistributions in binary form must reproduce the above copyright notice, this list of |
| 12 | + conditions and the following disclaimer in the documentation and/or other materials provided |
| 13 | + with the distribution. |
| 14 | + * Neither the name of salesforce.com, inc. nor the names of its contributors may be used to |
| 15 | + endorse or promote products derived from this software without specific prior written |
| 16 | + permission of salesforce.com, inc. |
| 17 | + |
| 18 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR |
| 19 | + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 21 | + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY |
| 25 | + WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +import Foundation |
| 29 | + |
| 30 | +/// Typed representation of the OAuth token endpoint error values defined by the |
| 31 | +/// Salesforce server in OauthErrorCode.java (core/identity-common-api). |
| 32 | +/// |
| 33 | +/// Use ``from(_:)`` to parse the raw `error` string from a token endpoint response. |
| 34 | +@objc public enum SFOAuthErrorCode: Int, CaseIterable { |
| 35 | + case unknown = 0 |
| 36 | + case accessDenied |
| 37 | + case appBlocked |
| 38 | + case appNotFound |
| 39 | + case authorizationPending |
| 40 | + case badJtiClaim |
| 41 | + case appAttestationFailed |
| 42 | + case appAttestationFailedRetry |
| 43 | + case ecAppPolicyNotFound |
| 44 | + case exceededRegistrationLimit |
| 45 | + case failCloseAppBlocked |
| 46 | + case failedRegistration |
| 47 | + case immediateUnsuccessful |
| 48 | + case installationError |
| 49 | + case invalidAppAccess |
| 50 | + case invalidAssertionType |
| 51 | + case invalidBasicAuthHeader |
| 52 | + case invalidClient |
| 53 | + case invalidClientId |
| 54 | + case invalidDpopProof |
| 55 | + case invalidDistributionState |
| 56 | + case invalidExpid |
| 57 | + case invalidGrant |
| 58 | + case invalidOtp |
| 59 | + case invalidRequest |
| 60 | + case invalidScope |
| 61 | + case invalidSessionLevel |
| 62 | + case invalidToken |
| 63 | + case loginError |
| 64 | + case oauthFlowDisabled |
| 65 | + case oauthPolicyNotFound |
| 66 | + case otpError |
| 67 | + case redirectUriMissing |
| 68 | + case redirectUriMismatch |
| 69 | + case registrationError |
| 70 | + case serverError |
| 71 | + case serviceUnavailable |
| 72 | + case slowDown |
| 73 | + case systemDown |
| 74 | + case unknownError |
| 75 | + case unsupportedExpid |
| 76 | + case unsupportedGrantType |
| 77 | + case unsupportedResponseType |
| 78 | + case unsupportedTokenType |
| 79 | + case useDpopNonce |
| 80 | + |
| 81 | + /// Returns the ``SFOAuthErrorCode`` whose wire value matches `string`, |
| 82 | + /// or `.unknown` if `string` is nil, empty, or not recognized. |
| 83 | + public static func from(_ string: String?) -> SFOAuthErrorCode { |
| 84 | + guard let string = string, !string.isEmpty else { return .unknown } |
| 85 | + return SFOAuthErrorCode.allCases.first { $0.wireValue == string } ?? .unknown |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/// Objective-C–accessible bridge for ``SFOAuthErrorCode``. |
| 90 | +/// Use `SFOAuthErrorCodeHelper.from(_:)` from Objective-C to parse error wire strings. |
| 91 | +@objc public class SFOAuthErrorCodeHelper: NSObject { |
| 92 | + /// Returns the integer raw value of the ``SFOAuthErrorCode`` matching `string`, |
| 93 | + /// or the raw value of `.unknown` (0) if not recognized. |
| 94 | + @objc public static func from(_ string: String?) -> NSInteger { |
| 95 | + return SFOAuthErrorCode.from(string).rawValue |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +public extension SFOAuthErrorCode { |
| 100 | + /// The wire string value sent in the token endpoint error JSON response. |
| 101 | + /// Returns `nil` for `.unknown`. |
| 102 | + var wireValue: String? { |
| 103 | + switch self { |
| 104 | + case .unknown: return nil |
| 105 | + case .accessDenied: return "access_denied" |
| 106 | + case .appBlocked: return "app_blocked" |
| 107 | + case .appNotFound: return "app_not_found" |
| 108 | + case .authorizationPending: return "authorization_pending" |
| 109 | + case .badJtiClaim: return "bad_jti_claim" |
| 110 | + case .appAttestationFailed: return "client_blocked" |
| 111 | + case .appAttestationFailedRetry: return "client_blocked_retry" |
| 112 | + case .ecAppPolicyNotFound: return "ecapp_policy_not_found" |
| 113 | + case .exceededRegistrationLimit: return "exceeded_registration_limit" |
| 114 | + case .failCloseAppBlocked: return "fail_close_app_blocked" |
| 115 | + case .failedRegistration: return "failed_registration" |
| 116 | + case .immediateUnsuccessful: return "immediate_unsuccessful" |
| 117 | + case .installationError: return "installation_error" |
| 118 | + case .invalidAppAccess: return "invalid_app_access" |
| 119 | + case .invalidAssertionType: return "invalid_assertion_type" |
| 120 | + case .invalidBasicAuthHeader: return "invalid_basic_auth_header" |
| 121 | + case .invalidClient: return "invalid_client" |
| 122 | + case .invalidClientId: return "invalid_client_id" |
| 123 | + case .invalidDpopProof: return "invalid_dpop_proof" |
| 124 | + case .invalidDistributionState: return "invalid_distribution_state" |
| 125 | + case .invalidExpid: return "invalid_expid" |
| 126 | + case .invalidGrant: return "invalid_grant" |
| 127 | + case .invalidOtp: return "invalid_otp" |
| 128 | + case .invalidRequest: return "invalid_request" |
| 129 | + case .invalidScope: return "invalid_scope" |
| 130 | + case .invalidSessionLevel: return "invalid_session_level" |
| 131 | + case .invalidToken: return "invalid_token" |
| 132 | + case .loginError: return "login_error" |
| 133 | + case .oauthFlowDisabled: return "oauth_flow_disabled" |
| 134 | + case .oauthPolicyNotFound: return "oauth_policy_not_found" |
| 135 | + case .otpError: return "otp_error" |
| 136 | + case .redirectUriMissing: return "redirect_uri_missing" |
| 137 | + case .redirectUriMismatch: return "redirect_uri_mismatch" |
| 138 | + case .registrationError: return "registration_error" |
| 139 | + case .serverError: return "server_error" |
| 140 | + case .serviceUnavailable: return "service_unavailable" |
| 141 | + case .slowDown: return "slow_down" |
| 142 | + case .systemDown: return "system_down" |
| 143 | + case .unknownError: return "unknown_error" |
| 144 | + case .unsupportedExpid: return "unsupported_expid" |
| 145 | + case .unsupportedGrantType: return "unsupported_grant_type" |
| 146 | + case .unsupportedResponseType: return "unsupported_response_type" |
| 147 | + case .unsupportedTokenType: return "unsupported_token_type" |
| 148 | + case .useDpopNonce: return "use_dpop_nonce" |
| 149 | + @unknown default: return nil |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments