{"id":13878607,"url":"https://github.com/rootstrap/apple_auth","last_synced_at":"2025-04-08T17:17:30.975Z","repository":{"id":43095370,"uuid":"262143860","full_name":"rootstrap/apple_auth","owner":"rootstrap","description":"Complete Ruby gem for Sign in with Apple. Actively maintained by rootstrap.com","archived":false,"fork":false,"pushed_at":"2024-10-09T17:35:00.000Z","size":73,"stargazers_count":90,"open_issues_count":3,"forks_count":20,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-01T15:14:43.663Z","etag":null,"topics":["apple","apple-sign-in","devise","devise-token-auth","hacktoberfest","rails","ruby","rubygem","sign-in-with-apple"],"latest_commit_sha":null,"homepage":"https://rootstrap.com","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rootstrap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-07T19:46:51.000Z","updated_at":"2025-01-15T02:52:08.000Z","dependencies_parsed_at":"2024-12-02T01:18:53.580Z","dependency_job_id":null,"html_url":"https://github.com/rootstrap/apple_auth","commit_stats":{"total_commits":37,"total_committers":11,"mean_commits":"3.3636363636363638","dds":0.7027027027027026,"last_synced_commit":"d71d10d370f2ec2107e361a8ef725e037ad2e152"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Fapple_auth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Fapple_auth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Fapple_auth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rootstrap%2Fapple_auth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rootstrap","download_url":"https://codeload.github.com/rootstrap/apple_auth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247888559,"owners_count":21013001,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apple","apple-sign-in","devise","devise-token-auth","hacktoberfest","rails","ruby","rubygem","sign-in-with-apple"],"created_at":"2024-08-06T08:01:54.561Z","updated_at":"2025-04-08T17:17:30.956Z","avatar_url":"https://github.com/rootstrap.png","language":"Ruby","readme":"# AppleAuth\n\n[![Gem Version](https://badge.fury.io/rb/apple_auth.svg)](https://badge.fury.io/rb/apple_auth)\n![CI](https://github.com/rootstrap/apple_auth/actions/workflows/ci.yml/badge.svg?branch=master)\n[![Maintainability](https://api.codeclimate.com/v1/badges/78453501221a76e3806e/maintainability)](https://codeclimate.com/github/rootstrap/apple_sign_in/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/78453501221a76e3806e/test_coverage)](https://codeclimate.com/github/rootstrap/apple_sign_in/test_coverage)\n\n## Installation\n\nAdd this line to your Gemfile:\n\n```ruby\ngem 'apple_auth'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself:\n\n    $ gem install apple_auth\n\n---\n\nAfter installing the gem, you need to run this generator.\n\n    $ rails g apple_auth:config\n\nThis will generate a new initializer: `apple_auth.rb` with the following default configuration:\n\n```ruby\nAppleAuth.configure do |config|\n  # config.apple_client_id = \u003cYour client_id in your Apple Developer account\u003e\n  # config.apple_private_key = \u003cYour private key provided by Apple\u003e\n  # config.apple_key_id = \u003cYour kid provided by Apple\u003e\n  # config.apple_team_id = \u003cYour team id provided by Apple\u003e\n  # config.redirect_uri = \u003cYour app redirect url\u003e\nend\n```\n\nSet your different credentials in the file by uncommenting the lines and adding your keys.\n\n---\n\n## Usage\n\nHere's an example of how to configure the gem:\n\n```ruby\nAppleAuth.configure do |config|\n  config.apple_client_id = 'com.yourapp...'\n  config.apple_private_key = \"-----BEGIN PRIVATE KEY-----\\nMIGTAgEA....\\n-----END PRIVATE KEY-----\"\n  config.apple_key_id = 'RTZ...'\n  config.apple_team_id = 'WNU...'\n  config.redirect_uri = 'https://localhost:3000'\nend\n```\n\nWe strongly recommend to use environment variables for these values.\n\n### Apple sign-in workflow:\n\n![alt text](https://docs-assets.developer.apple.com/published/360d59b776/rendered2x-1592224731.png)\n\nFor more information, check the [Apple oficial documentation](https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api).\n\n### Validate JWT token and get user information:\n\n```ruby\n# with a valid JWT\nuser_id = '000343.1d22d2937c7a4e56806dfb802b06c430...'\nvalid_jwt_token = 'eyJraWQiOiI4NkQ4OEtmIiwiYWxnIjoiUlMyNTYifQ.eyJpc...'\nAppleAuth::UserIdentity.new(user_id, valid_jwt_token).validate!\n\u003e\u003e  { exp: 1595279622, email: \"user@example.com\", email_verified: true , ...}\n\n# with an invalid JWT\ninvalid_jwt_token = 'eyJraWQiOiI4NkQsd4OEtmIiwiYWxnIjoiUlMyNTYifQ.edsyJpc...'\nAppleAuth::UserIdentity.new(user_id, invalid_jwt_token).validate!\n\u003e\u003e Traceback (most recent call last):..\n\u003e\u003e ...\n\u003e\u003e  AppleAuth::Conditions::JWTValidationError\n```\n\n### Verify user identity and get access and refresh tokens:\n\n```ruby\ncode = 'cfb77c21ecd444390a2c214cd33decdfb.0.mr...'\nAppleAuth::Token.new(code).authenticate!\n\u003e\u003e { access_token: \"a7058d...\", expires_at: 1595894672, refresh_token: \"r8f1ce...\" }\n```\n\n### Handle server to server notifications\n\nfrom the request parameter :payload\n\n```ruby\n# with a valid JWT\nparams[:payload] = \"eyJraWQiOiJZ......\"\nAppleAuth::ServerIdentity.new(params[:payload]).validate!\n\u003e\u003e {iss: \"https://appleid.apple.com\", exp: 1632224024, iat: 1632137624, jti: \"yctpp1ZHaGCzaNB9PWB4DA\",...}\n\n# with an invalid JWT\nparams[:payload] = \"asdasdasdasd......\"\nAppleAuth::ServerIdentity.new(params[:payload]).validate!\n\u003e\u003e JWT::VerificationError: Signature verification raised\n```\n\nImplementation in a controller would look like this:\n\n```ruby\nclass Hooks::AuthController \u003c ApplicationController\n\n  skip_before_action :verify_authenticity_token\n\n  # https://developer.apple.com/documentation/sign_in_with_apple/processing_changes_for_sign_in_with_apple_accounts\n  # NOTE: The Apple documentation states the events attribute as an array but is in fact a stringified json object\n  def apple\n    # will raise an error when the signature is invalid\n    payload = AppleAuth::ServerIdentity.new(params[:payload]).validate!\n    event = JSON.parse(payload[:events]).symbolize_keys\n    uid = event[\"sub\"]\n    user = User.find_by!(provider: 'apple', uid: uid)\n\n    case event[:type]\n    when \"email-enabled\", \"email-disabled\"\n      # Here we should update the user with the relay state\n    when \"consent-revoked\", \"account-delete\"\n      user.destroy!\n    else\n      throw event\n    end\n    render plain: \"200 OK\", status: :ok\n  end\nend\n```\n\n## Using with Devise\n\nIf you are using devise_token_auth gem, run this generator.\n\n    $ rails g apple_sign_in:apple_auth_controller [scope]\n\nIn the scope you need to write your path from controllers to your existent devise controllers.\nAn example `$ rails g apple_auth:apple_auth_controller api/v1/`\nThis will generate a new controller: `controllers/api/v1/apple_auth_controller.rb`.\n\nYou should configure the route, you can wrap it in the devise_scope block like:\n\n```\ndevise_scope :user do\n  resource :user, only: %i[update show] do\n    controller :apple_auth do\n      post :apple_auth, on: :collection, to: 'apple_auth#create'\n    end\n  end\nend\n```\n\n## Demo\n\nYou can find a full implementation of this gem in [this demo](https://github.com/rootstrap/apple-sign-in-rails).\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/rootstrap/apple_auth/issues. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/rootstrap/apple_auth/blob/master/CODE_OF_CONDUCT.md).\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the AppleAuth project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rootstrap/apple_auth/blob/master/CODE_OF_CONDUCT.md).\n\n## Credits\n\napple_auth gem is maintained by [Rootstrap](http://www.rootstrap.com) with the help of our\n[contributors](https://github.com/rootstrap/apple_auth/contributors).\n\n[\u003cimg src=\"https://s3-us-west-1.amazonaws.com/rootstrap.com/img/rs.png\" width=\"100\"/\u003e](http://www.rootstrap.com)\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frootstrap%2Fapple_auth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frootstrap%2Fapple_auth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frootstrap%2Fapple_auth/lists"}