Class: StandardId::Providers::Apple
- Inherits:
-
Base
- Object
- Base
- StandardId::Providers::Apple
- Defined in:
- lib/standard_id/apple/providers/apple.rb
Constant Summary collapse
- ISSUER =
"https://appleid.apple.com".freeze
- AUTH_ENDPOINT =
"#{ISSUER}/auth/authorize".freeze
- TOKEN_ENDPOINT =
"#{ISSUER}/auth/token".freeze
- JWKS_URI =
"#{ISSUER}/auth/keys".freeze
- DEFAULT_SCOPE =
"name email".freeze
- DEFAULT_RESPONSE_MODE =
"form_post".freeze
- AUTHORIZATION_PARAM_DEFAULTS =
{ scope: DEFAULT_SCOPE, response_mode: DEFAULT_RESPONSE_MODE }.freeze
Class Method Summary collapse
- .authorization_url(state:, redirect_uri:, **options) ⇒ Object
- .config_schema ⇒ Object
- .default_scope ⇒ Object
- .exchange_code_for_user_info(code:, redirect_uri:, client_id: StandardId.config.apple_client_id, nonce: nil) ⇒ Object
- .get_user_info(code: nil, id_token: nil, access_token: nil, redirect_uri: nil, nonce: nil, **options) ⇒ Object
- .provider_name ⇒ Object
- .resolve_params(params, context: {}) ⇒ Object
- .skip_csrf? ⇒ Boolean
- .supported_authorization_params ⇒ Object
- .supports_mobile_callback? ⇒ Boolean
- .verify_id_token(id_token:, client_id: StandardId.config.apple_client_id, nonce: nil) ⇒ Object
Class Method Details
.authorization_url(state:, redirect_uri:, **options) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/standard_id/apple/providers/apple.rb', line 29 def (state:, redirect_uri:, **) ensure_basic_credentials! query = { client_id: StandardId.config.apple_client_id, redirect_uri:, response_type: "code", state: } .each do |param| query[param] = [param] || AUTHORIZATION_PARAM_DEFAULTS[param] end "#{AUTH_ENDPOINT}?#{URI.encode_www_form(query.compact)}" end |
.config_schema ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/standard_id/apple/providers/apple.rb', line 63 def config_schema { apple_client_id: { type: :string, default: nil }, apple_mobile_client_id: { type: :string, default: nil }, apple_private_key: { type: :string, default: nil }, apple_key_id: { type: :string, default: nil }, apple_team_id: { type: :string, default: nil } } end |
.default_scope ⇒ Object
73 74 75 |
# File 'lib/standard_id/apple/providers/apple.rb', line 73 def default_scope DEFAULT_SCOPE end |
.exchange_code_for_user_info(code:, redirect_uri:, client_id: StandardId.config.apple_client_id, nonce: nil) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/standard_id/apple/providers/apple.rb', line 92 def exchange_code_for_user_info(code:, redirect_uri:, client_id: StandardId.config.apple_client_id, nonce: nil) ensure_full_credentials!(client_id: client_id) raise StandardId::InvalidRequestError, "Missing authorization code" if code.blank? token_response = HttpClient.post_form(TOKEN_ENDPOINT, { client_id: client_id, client_secret: generate_client_secret(client_id: client_id), code: code, grant_type: "authorization_code", redirect_uri: redirect_uri }) unless token_response.is_a?(Net::HTTPSuccess) error_body = begin JSON.parse(token_response.body) rescue StandardError {} end raise StandardId::InvalidRequestError, "Failed to exchange Apple authorization code: #{error_body["error"]}" end parsed_token = JSON.parse(token_response.body) id_token = parsed_token["id_token"] raise StandardId::InvalidRequestError, "Apple response missing id_token" if id_token.blank? tokens = extract_token_payload(parsed_token) user_info = verify_id_token(id_token: id_token, client_id: client_id, nonce: nonce) build_response(user_info, tokens: tokens) rescue StandardError => e raise e if e.is_a?(StandardId::OAuthError) raise StandardId::OAuthError, e., cause: e end |
.get_user_info(code: nil, id_token: nil, access_token: nil, redirect_uri: nil, nonce: nil, **options) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/standard_id/apple/providers/apple.rb', line 46 def get_user_info(code: nil, id_token: nil, access_token: nil, redirect_uri: nil, nonce: nil, **) client_id = [:client_id] || StandardId.config.apple_client_id if id_token.present? build_response( verify_id_token(id_token: id_token, client_id: client_id, nonce: nonce), tokens: { id_token: id_token } ) elsif code.present? exchange_code_for_user_info(code: code, redirect_uri: redirect_uri, client_id: client_id, nonce: nonce) elsif access_token.present? raise StandardId::InvalidRequestError, "Access token login flow is not supported for Apple" else raise StandardId::InvalidRequestError, "Either code or id_token must be provided" end end |
.provider_name ⇒ Object
21 22 23 |
# File 'lib/standard_id/apple/providers/apple.rb', line 21 def provider_name "apple" end |
.resolve_params(params, context: {}) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/standard_id/apple/providers/apple.rb', line 85 def resolve_params(params, context: {}) flow = context[:flow] || :web client_id = flow == :mobile ? StandardId.config.apple_mobile_client_id : StandardId.config.apple_client_id params.merge(client_id: client_id) end |
.skip_csrf? ⇒ Boolean
77 78 79 |
# File 'lib/standard_id/apple/providers/apple.rb', line 77 def skip_csrf? true end |
.supported_authorization_params ⇒ Object
25 26 27 |
# File 'lib/standard_id/apple/providers/apple.rb', line 25 def [:nonce, :scope, :response_mode] end |
.supports_mobile_callback? ⇒ Boolean
81 82 83 |
# File 'lib/standard_id/apple/providers/apple.rb', line 81 def supports_mobile_callback? true end |
.verify_id_token(id_token:, client_id: StandardId.config.apple_client_id, nonce: nil) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/standard_id/apple/providers/apple.rb', line 127 def verify_id_token(id_token:, client_id: StandardId.config.apple_client_id, nonce: nil) raise StandardId::InvalidRequestError, "Missing id_token" if id_token.blank? raise StandardId::InvalidRequestError, "Apple client_id is not configured" if client_id.blank? decoded_token = JWT.decode(id_token, nil, false) header = decoded_token[1] jwk = fetch_jwk(kid: header["kid"]) verified_payload, = JWT.decode( id_token, jwk.public_key, true, algorithm: "RS256", iss: ISSUER, verify_iss: true, aud: client_id, verify_aud: true ) # Validate nonce if provided (web flow with server-generated nonce) if nonce.present? token_nonce = verified_payload["nonce"] if token_nonce != nonce raise StandardId::InvalidRequestError, "ID token nonce mismatch. Expected: #{nonce}, got: #{token_nonce}" end end { "sub" => verified_payload["sub"], "email" => verified_payload["email"], "email_verified" => verified_payload["email_verified"], "is_private_email" => verified_payload["is_private_email"] }.compact rescue JWT::InvalidAudError => e raise StandardId::InvalidRequestError, "Invalid Apple ID token audience: #{e.}" rescue JWT::DecodeError => e raise StandardId::InvalidRequestError, "Invalid Apple ID token: #{e.}" rescue StandardError => e raise e if e.is_a?(StandardId::OAuthError) raise StandardId::OAuthError, e., cause: e end |