• Stars
    star
    443
  • Rank 98,504 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 5 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Provide "Sign In With Apple" functionality to your Laravel app.

Sign In With Apple for Laravel

repository-open-graph-template

Supporting This Package

This is an MIT-licensed open source project with its ongoing development made possible by the support of the community. If you'd like to support this, and our other packages, please consider sponsoring us via the button above.

We thank the following sponsors for their generosity, please take a moment to check them out:

Table of Contents

Requirements

  • PHP 7.3+
  • Laravel 8.0+
  • Socialite 5.0+
  • Apple Developer Subscription

Installation

siwa-video-cover

  1. Install the composer package:

    composer require genealabs/laravel-sign-in-with-apple

    We also recommend using geneaLabs/laravel-socialiter to automatically manage user resolution and persistence:

    composer require genealabs/laravel-socialiter

Configuration

  1. Create an App ID for your website (https://developer.apple.com/account/resources/identifiers/list/bundleId) with the following details:

    • Platform: iOS, tvOS, watchOS (I'm unsure if either choice has an effect for web apps)
    • Description: (something like "example.com app id")
    • Bundle ID (Explicit): com.example.id (or something similar)
    • Check "Sign In With Apple"
  2. Create a Service ID for your website (https://developer.apple.com/account/resources/identifiers/list/serviceId) with the following details:

    • Description: (something like "example.com service id")
    • Identifier: com.example.service (or something similar)
    • Check "Sign In With Apple"
    • Configure "Sign In With Apple":
      • Primary App Id: (select the primary app id created in step 1)
      • Web Domain: example.com (the domain of your web site)
      • Return URLs: https://example.com/apple-signin (the route pointing to the callback method in your controller)
      • Click "Save".
      • Click the "Edit" button to edit the details of the "Sign In With Apple" configuration we just created.
      • If you haven't verified the domain yet, download the verification file, upload it to https://example.com/.well-known/apple-developer-domain-association.txt, and then click the "Verify" button.
  3. Create a Private Key for your website (https://developer.apple.com/account/resources/authkeys/list) with the following details:

    • Key Name:
    • Check "Sign In With Apple"
    • Configure "Sign In With Apple":
      • Primary App ID: (select the primary app id created in step 1)
      • Click "Save"
    • Click "Continue"
    • Click "Register"
    • Click "Download"
    • Rename the downloaded file to key.txt
  4. Create your app's client secret:

    • Install the JWT Gem:

      sudo gem install jwt
    • Create a file called client_secret.rb to process the private key:

      require 'jwt'
      
      key_file = 'key.txt'
      team_id = ''
      client_id = ''
      key_id = ''
      
      ecdsa_key = OpenSSL::PKey::EC.new IO.read key_file
      
      headers = {
      'kid' => key_id
      }
      
      claims = {
          'iss' => team_id,
          'iat' => Time.now.to_i,
          'exp' => Time.now.to_i + 86400*180,
          'aud' => 'https://appleid.apple.com',
          'sub' => client_id,
      }
      
      token = JWT.encode claims, ecdsa_key, 'ES256', headers
      
      puts token
    • Fill in the following fields:

      • team_id: This can be found on the top-right corner when logged into your Apple Developer account, right under your name.
      • client_id: This is the identifier from the Service Id created in step 2 above, for example com.example.service
      • key_id: This is the identifier of the private key created in step 3 above.
    • Save the file and run it from the terminal. It will spit out a JWT which is your client secret, which you will need to add to your .env file in the next step.

      ruby client_secret.rb
  5. Set the necessary environment variables in your .env file:

    SIGN_IN_WITH_APPLE_LOGIN="/apple/login/controller/login/action"
    SIGN_IN_WITH_APPLE_REDIRECT="/apple/login/controller/callback/action"
    SIGN_IN_WITH_APPLE_CLIENT_ID="your app's service id as registered with Apple"
    SIGN_IN_WITH_APPLE_CLIENT_SECRET="your app's client secret as calculated in step 4"

Implementation

Button

Add the following blade directive to your login page:

@signInWithApple($color, $hasBorder, $type, $borderRadius)
Parameter Definition
$color String, either "black" or "white.
$hasBorder Boolean, either true or false.
$type String, either "sign-in" or "continue".
$borderRadius Integer, greater or equal to 0.

Controller

This implementation uses Socialite to get the login credentials. The following is an example implementation of the controller:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use GeneaLabs\LaravelSocialiter\Facades\Socialiter;
use Laravel\Socialite\Facades\Socialite;

class AppleSigninController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function login()
    {
        return Socialite::driver("sign-in-with-apple")
            ->scopes(["name", "email"])
            ->redirect();
    }

    public function callback(Request $request)
    {
        // get abstract user object, not persisted
        $user = Socialite::driver("sign-in-with-apple")
            ->user();
        
        // or use Socialiter to automatically manage user resolution and persistence
        $user = Socialiter::driver("sign-in-with-apple")
            ->login();
    }
}

Note that when processing the returned $user object, it is critical to know that the sub element is the unique identifier for the user, NOT the email address. For more details, visit https://developer.apple.com/documentation/signinwithapplerestapi/authenticating_users_with_sign_in_with_apple.


Credits

  1. https://developer.okta.com/blog/2019/06/04/what-the-heck-is-sign-in-with-apple
  2. https://developer.apple.com/sign-in-with-apple/get-started

Commitment to Quality

During package development I try as best as possible to embrace good design and development practices, to help ensure that this package is as good as it can be. My checklist for package development includes:

  • Achieve as close to 100% code coverage as possible using unit tests.
  • Eliminate any issues identified by SensioLabs Insight and Scrutinizer.
  • Be fully PSR1, PSR2, and PSR4 compliant.
  • Include comprehensive documentation in README.md.
  • Provide an up-to-date CHANGELOG.md which adheres to the format outlined at http://keepachangelog.com.
  • Have no PHPMD or PHPCS warnings throughout all code.

Contributing

Please observe and respect all aspects of the included Code of Conduct.

Reporting Issues

When reporting issues, please fill out the included template as completely as possible. Incomplete issues may be ignored or closed if there is not enough information included to be actionable.

Submitting Pull Requests

Please review the Contribution Guidelines. Only PRs that meet all criterium will be accepted.

If you ❤️ open-source software, give the repos you use a ⭐️.

We have included the awesome symfony/thanks composer package as a dev dependency. Let your OS package maintainers know you appreciate them by starring the packages you use. Simply run composer thanks after installing this package. (And not to worry, since it's a dev-dependency it won't be installed in your live environment.)

More Repositories

1

laravel-model-caching

Eloquent model-caching made easy.
PHP
2,246
star
2

laravel-caffeine

Keeping Your Laravel Forms Awake.
PHP
921
star
3

laravel-governor

Manage authorization with granular role-based permissions in your Laravel Apps.
PHP
183
star
4

laravel-messenger

Notifying your users doesn't have to be a lot of work.
PHP
135
star
5

nova-map-marker-field

Provides an visual interface for editing latitude and longitude coordinates.
Vue
131
star
6

laravel-mixpanel

Intuitive drop-in analytics.
PHP
117
star
7

laravel-pivot-events

PHP
114
star
8

nova-gutenberg

Implementation of the Gutenberg editor as a Laravel Nova Field based on Laraberg.
Vue
107
star
9

laravel-socialiter

Automatically manage user persistence and resolution for any Laravel Socialite provider.
PHP
101
star
10

laravel-casts

Form builder for Laravel.
PHP
93
star
11

laravel-maps

Easy - peasy - maps for Laravel
PHP
81
star
12

laravel-overridable-model

Provide a uniform method of allowing models to be overridden in Laravel.
PHP
63
star
13

nova-file-upload-field

The easiest drag-and-drop file uploading field for Laravel Nova.
Vue
53
star
14

laravel-multi-step-progressbar

Quickly implement a multi-step progress-bar in Laravel.
PHP
45
star
15

laravel-impersonator

Package to allow user-impersonation in your Laravel and Nova apps.
PHP
45
star
16

laravel-optimized-postgres

Implement all textual fields as `text` in Postgres databases.
PHP
41
star
17

laravel-null-carbon

Carbon null-class.
PHP
39
star
18

nova-prepopulate-searchable

A tool to allow BelongsTo searchable fields to be pre-populated with data
Vue
37
star
19

laravel-changelog

Easily integrate and display your changelog in your app.
PHP
37
star
20

laravel-appleseed

Prevent the pesky Apple Touch Icon error log entries.
PHP
30
star
21

Phpgmaps

Larvel 5 implementation of BIOSTALL/CodeIgniter-Google-Maps-V3-API-Library.
PHP
27
star
22

nova-passport-manager

Manage Passport clients and tokens from within Nova.
Vue
26
star
23

nova-multi-tenant-manager

Manage tenants and their settings in Laravel Nova.
Vue
21
star
24

interesting-packages-and-articles

List of packages and articles we are interested in using that fulfill specific needs.
17
star
25

nova-telescope

Integrate Laravel Telescope into Laravel Nova.
PHP
17
star
26

tailwind-mac-colors

Color palette and swatches for macOS's color picker.
16
star
27

php-coding-standards

Custom PHPCS sniffs that support all our coding standards.
Shell
15
star
28

action-reviewdog-phpstan

🐾 Run PHPStan with ReviewDog.
Shell
12
star
29

nova-horizon

Integrate Laravel Horizon into Laravel Nova.
PHP
10
star
30

laravel-registrar

PHP
9
star
31

laravel-tawk

Easily and quickly integrate Tawk.to LiveChat into your laravel app in under 5 minutes!
PHP
8
star
32

tailwindcss-sketch-design-system

An atomic design system for TailwindCSS implemented in Sketch.
CSS
8
star
33

cashier-paypal

Laravel Cashier-compatible billing for PayPal
PHP
7
star
34

panic-nova-intelephense.novaextension

JavaScript
6
star
35

laravel-dev-environment

A collection of scripts and tutorials to optimize your Laravel development environment.
Shell
6
star
36

laravel-imagery

Serve contextually optimized images to speed up page rendering and improve the user experience!
PHP
6
star
37

laravel-authorization-addons

Additional helper methods and blade directives to help with more complex authorization queries.
PHP
6
star
38

action-reviewdog-phpmd

🐾 Run PHP Mess Detector with ReviewDog.
Shell
5
star
39

awesome-laravel-ignition-plugins

A curated list of plugins for the Laravel Igniter error package.
5
star
40

documentation

Documentation for all our open-source packages.
CSS
4
star
41

laravel-cashier-braintree

PHP
4
star
42

laravel-whoops-atom

Open Whoops Stack Trace Files in Atom Editor.
PHP
3
star
43

laravel-codespaces-app-template

Template for creating Laravel apps in codespaces.
PHP
3
star
44

action-reviewdog-phpcs

Shell
3
star
45

bootstrap-css-grid-override

Update bootstrap 4 with native CSS-grid with graceful degradation.
CSS
3
star
46

credo

My beliefs on being.
2
star
47

SculptKeyboardMapping

Keyboard mapping for Microsoft Sculpt Keyboard on Mac using KeyRemap4MacBook
2
star
48

panic-nova-phpcs.novaextension

JavaScript
2
star
49

laravel-codespaces-package-template

Template for creating Laravel packages in codespaces.
PHP
2
star
50

bones-macros

useful form macros for Laravel's Blade template engine.
PHP
2
star
51

laravel-nova-morph-many-to-one

Drop-in package that adds a many-to-one (inverse of one-to-many) polymorphic relationship to Eloquent and Nova.
PHP
2
star
52

laravel-collection-macros

PHP
2
star
53

unity-heraldry-generator

Heraldic Generator created in Unity using C# with the goal of providing a common web and in-game mechanism for creating Coats of Arms.
C#
1
star
54

laravel-multi-tenant-manager

PHP
1
star
55

panic-nova-phpmd.novaextension

JavaScript
1
star
56

panic-nova-atom-keybindings

1
star
57

nova-prepopulate-searchable-old

A tool to allow BelongsTo searchable fields to be pre-populated with data.
Vue
1
star
58

laravel-two-way-attribute-casting

Perform attribute casting when saving Eloquent models to the database.
PHP
1
star