Skip to content

Commit

Permalink
feat: add klarna provider (#1297)
Browse files Browse the repository at this point in the history
Co-authored-by: Taher Dhilawala <[email protected]>
Co-authored-by: Lucas Michot <[email protected]>
  • Loading branch information
3 people authored Nov 23, 2024
0 parents commit ca15942
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
13 changes: 13 additions & 0 deletions KlarnaExtendSocialite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SocialiteProviders\Klarna;

use SocialiteProviders\Manager\SocialiteWasCalled;

class KlarnaExtendSocialite
{
public function handle(SocialiteWasCalled $socialiteWasCalled): void
{
$socialiteWasCalled->extendSocialite('klarna', Provider::class);
}
}
63 changes: 63 additions & 0 deletions Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace SocialiteProviders\Klarna;

use GuzzleHttp\RequestOptions;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;

class Provider extends AbstractProvider
{
public const IDENTIFIER = 'KLARNA';

/**
* The separating character for the requested scopes.
*
* @var string
*/
protected $scopeSeparator = ' ';

protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase('https://login.klarna.com/eu/lp/idp/oauth2/auth', $state);
}

protected function getTokenUrl(): string
{
return 'https://login.klarna.com/eu/lp/idp/oauth2/token';
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://login.klarna.com/eu/lp/idp/userinfo', [
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
]);

return json_decode((string) $response->getBody(), true);
}

/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['sub'],
'nickname' => $user['given_name'],
'name' => $user['given_name'].' '.$user['family_name'],
'email' => $user['email'],
'email_verified' => $user['email_verified'],
'avatar' => null,
'national_identification_number' => $user['national_identification_number'] ?? null,
'national_identification_number_country' => $user['national_identification_number_country'] ?? null,
'phone' => $user['phone'] ?? null,
'phone_verified' => $user['phone_verified'] ?? null,
]);
}
}
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Intercom

```bash
composer require socialiteproviders/klarna
```

## Installation & Basic Usage

Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below.

### Add configuration to `config/services.php`

```php
'klarna' => [
'client_id' => env('KLARNA_CLIENT_ID'),
'client_secret' => env('KLARNA_CLIENT_SECRET'),
'redirect' => env('KLARNA_REDIRECT_URI')
],
```

### Add provider event listener

#### Laravel 11+

In Laravel 11, the default `EventServiceProvider` provider was removed. Instead, add the listener using the `listen` method on the `Event` facade, in your `AppServiceProvider` `boot` method.

* Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.

```php
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
$event->extendSocialite('klarna', \SocialiteProviders\Klarna\Provider::class);
});
```
<details>
<summary>
Laravel 10 or below
</summary>
Configure the package's listener to listen for `SocialiteWasCalled` events.

Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions.

```php
protected $listen = [
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... other providers
\SocialiteProviders\Klarna\KlarnaExtendSocialite::class.'@handle',
],
];
```
</details>

### Usage

You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed):

```php
return Socialite::driver('klarna')->redirect();
```

### Returned User fields

- ``id``
- ``name``
- ``nickname`` (same as ``first_name`` in ``name``)
- ``email``
- ``email_verified`` (boolean indicating if the email is verified)
- ``phone``
- ``phone_verified`` (boolean indicating if the phone is verified)
- ``national_identification_number``
- ``national_identification_number_country``
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "socialiteproviders/klarna",
"description": "Klarna Oauth2 Provider for Laravel Socialite",
"license": "MIT",
"keywords": [
"klarna",
"laravel",
"oauth",
"provider",
"socialite"
],
"authors": [
{
"name": "Taher Dhilawala",
"email": "[email protected]"
}
],
"support": {
"issues": "https://github.com/socialiteproviders/providers/issues",
"source": "https://github.com/socialiteproviders/providers",
"docs": "https://socialiteproviders.com/klarna"
},
"require": {
"php": "^8.0",
"ext-json": "*",
"socialiteproviders/manager": "^4.4"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\Klarna\\": ""
}
}
}

0 comments on commit ca15942

Please sign in to comment.