11# AppleAuth
22
3- [ ![ CI] ( https://api.travis-ci.org /rootstrap/apple_auth.svg?branch=master )] ( https://travis-ci.org /github/rootstrap/apple_auth )
3+ [ ![ CI] ( https://api.travis-ci.com /rootstrap/apple_auth.svg?branch=master )] ( https://travis-ci.com /github/rootstrap/apple_auth )
44[ ![ Maintainability] ( https://api.codeclimate.com/v1/badges/78453501221a76e3806e/maintainability )] ( https://codeclimate.com/github/rootstrap/apple_sign_in/maintainability )
55[ ![ Test Coverage] ( https://api.codeclimate.com/v1/badges/78453501221a76e3806e/test_coverage )] ( https://codeclimate.com/github/rootstrap/apple_sign_in/test_coverage )
66
5656
5757We strongly recommend to use environment variables for these values.
5858
59- Apple sign-in workflow:
59+ ### Apple sign-in workflow:
6060
6161![ alt text] ( https://docs-assets.developer.apple.com/published/360d59b776/rendered2x-1592224731.png )
6262
6363For more information, check the [ Apple oficial documentation] ( https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api ) .
6464
65- Validate JWT token and get user information:
65+ ### Validate JWT token and get user information:
6666
6767``` ruby
6868# with a valid JWT
@@ -79,14 +79,59 @@ AppleAuth::UserIdentity.new(user_id, invalid_jwt_token).validate!
7979>> AppleAuth ::Conditions ::JWTValidationError
8080```
8181
82- Verify user identity and get access and refresh tokens:
82+ ### Verify user identity and get access and refresh tokens:
8383
8484``` ruby
8585code = ' cfb77c21ecd444390a2c214cd33decdfb.0.mr...'
8686AppleAuth ::Token .new (code).authenticate!
8787>> { access_token: " a7058d..." , expires_at: 1595894672 , refresh_token: " r8f1ce..." }
8888```
8989
90+ ### Handle server to server notifications
91+
92+ from the request parameter : payload
93+
94+ ``` ruby
95+ # with a valid JWT
96+ params[:payload ] = " eyJraWQiOiJZ......"
97+ AppleAuth ::ServerIdentity .new (params[:payload ]).validate!
98+ >> {iss: " https://appleid.apple.com" , exp: 1632224024 , iat: 1632137624 , jti: " yctpp1ZHaGCzaNB9PWB4DA" ,...}
99+
100+ # with an invalid JWT
101+ params[:payload ] = " asdasdasdasd......"
102+ AppleAuth ::ServerIdentity .new (params[:payload ]).validate!
103+ >> JWT ::VerificationError: Signature verification raised
104+ ```
105+
106+ Implementation in a controller would look like this:
107+
108+ ``` ruby
109+ class Hooks ::AuthController < ApplicationController
110+
111+ skip_before_action :verify_authenticity_token
112+
113+ # https://developer.apple.com/documentation/sign_in_with_apple/processing_changes_for_sign_in_with_apple_accounts
114+ # NOTE: The Apple documentation states the events attribute as an array but is in fact a stringified json object
115+ def apple
116+ # will raise an error when the signature is invalid
117+ payload = AppleAuth ::ServerIdentity .new (params[:payload ]).validate!
118+ event = JSON .parse(payload[:events ]).symbolize_keys
119+ uid = event[" sub" ]
120+ user = User .find_by!(provider: ' apple' , uid: uid)
121+
122+ case event[:type ]
123+ when " email-enabled" , " email-disabled"
124+ # Here we should update the user with the relay state
125+ when " consent-revoked" , " account-delete"
126+ user.destroy!
127+ else
128+ throw event
129+ end
130+ render plain: " 200 OK" , status: :ok
131+ end
132+ end
133+ ```
134+
90135## Using with Devise
91136
92137If you are using devise_token_auth gem, run this generator.
0 commit comments