Skip to content

Commit

Permalink
update package
Browse files Browse the repository at this point in the history
  • Loading branch information
aldrahastur committed Sep 4, 2024
1 parent 94e7029 commit 7f32a8c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# This is my package logto-auth

[![Latest Version on Packagist](https://img.shields.io/packagist/v/aldrahastur/logto-auth.svg?style=flat-square)](https://packagist.org/packages/aldrahastur/logto-auth)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/aldrahastur/logto-auth/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/aldrahastur/logto-auth/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/aldrahastur/logto-auth/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/aldrahastur/logto-auth/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/aldrahastur/logto-auth.svg?style=flat-square)](https://packagist.org/packages/aldrahastur/logto-auth)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/bikefreaks/logto-auth.svg?style=flat-square)](https://packagist.org/packages/bikefreaks/logto-auth)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/bikefreaks/logto-auth/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/bikefreaks/logto-auth/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/bikefreaks/logto-auth/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/bikefreaks/logto-auth/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/bikefreaks/logto-auth.svg?style=flat-square)](https://packagist.org/packages/bikefreaks/logto-auth)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Expand All @@ -20,7 +20,7 @@ We highly appreciate you sending us a postcard from your hometown, mentioning wh
You can install the package via composer:

```bash
composer require aldrahastur/logto-auth
composer require bikefreaks/logto-auth
```

You can publish and run the migrations with:
Expand Down
5 changes: 4 additions & 1 deletion config/logto-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

// config for bikefreaks/LogtoAuth
return [

'endpoint' => env('LOG_TO_ENDPOINT'),
'app_id' => env('LOG_TO_APP_ID'),
'app_secret' => env('LOG_TO_APP_SECRET'),
'lee_way' => env('LOG_TO_LEE_WAY', 15)
];
24 changes: 24 additions & 0 deletions src/Extensions/LaravelSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace B\Extensions;

use Illuminate\Support\Facades\Session;
use Logto\Sdk\Storage\StorageKey;

class LaravelSession implements \Logto\Sdk\Storage\Storage
{
public function get(StorageKey $key): ?string
{
return Session::get($key->value);
}

public function set(StorageKey $key, ?string $value): void
{
Session::put($key->value, $value);
}

public function delete(StorageKey $key): void
{
Session::forget($key->value);
}
}
67 changes: 66 additions & 1 deletion src/LogtoAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,69 @@

namespace bikefreaks\LogtoAuth;

class LogtoAuth {}
use B\Extensions\LaravelSession;
use Firebase\JWT\JWT;
use GuzzleHttp\Client;
use Logto\Sdk\LogtoClient;
use Logto\Sdk\LogtoConfig;
use Logto\Sdk\Models\OidcProviderMetadata;
use Logto\Sdk\Oidc\OidcCore;
use Phpfastcache\CacheManager;

class LogtoAuth extends LogtoClient
{

public function __construct()
{
$endpoint = config('logto-io.endpoint');
$appId = config('logto-io.app_id');
$appSecret = config('logto-io.app_secret');
$leeWay = config('logto-io.lee_way');
JWT::$leeway = $leeWay;
if (empty($endpoint) || empty($appId) || empty($appSecret)) {
throw new \Exception('Logto configuration is not set');
}
$this->config = new LogtoConfig(
config('logto-io.endpoint'),
config('logto-io.app_id'),
config('logto-io.app_secret')
);
$this->storage = new LaravelSession();
$this->oidcCore = self::create($this->config->endpoint);
}

protected static function create(string $logtoEndpoint): OidcCore
{
$cacheManager = CacheManager::getInstance('files');
$cacheObject = $cacheManager->getItem('logto-oidc-configuration-' . urlencode($logtoEndpoint));
$cachedConfiguration = $cacheObject->get();
if (!$cachedConfiguration) {
$client = new Client();
$cachedConfiguration = $client->get(
$logtoEndpoint . '/oidc/.well-known/openid-configuration',
['headers' => ['user-agent' => '@logto/php', 'accept' => '*/*']]
)->getBody()->getContents();
$cacheObject->set($cachedConfiguration)->expiresAfter(3600);// 1 hour
$cacheManager->save($cacheObject);
}
return new OidcCore(new OidcProviderMetadata(...json_decode($cachedConfiguration, true)));
}
public function getSubject(): string
{
return $this->fetchUserInfo()->sub;
}

public function config(): LogtoConfig
{
return $this->config;
}

public function handleSignInCallback(): void
{
if (!($_SERVER['PATH_INFO'] ?? null)) {
$_SERVER['PATH_INFO'] = parse_url($this->getSignInSession()->redirectUri, PHP_URL_PATH);
}

parent::handleSignInCallback();
}
}

0 comments on commit 7f32a8c

Please sign in to comment.