10
10
use Logto \Sdk \Models \OidcProviderMetadata ;
11
11
use Phpfastcache \CacheManager ;
12
12
use Firebase \JWT \JWT ;
13
+ use Phpfastcache \Helper \Psr16Adapter ;
13
14
14
15
/**
15
16
* The core OIDC functions for the Logto client. Provider-agonistic functions
@@ -23,18 +24,29 @@ class OidcCore
23
24
24
25
/**
25
26
* Create a OidcCore instance for the given Logto endpoint using the discovery URL.
26
- * Note it may take a few time to fetch the provider metadata since it will send a
27
- * network request.
27
+ *
28
+ * Note it may take a while to fetch the metadata from the endpoint for the first time.
29
+ * After that, the metadata will be cached for 1 hour.
28
30
*/
29
31
static function create (string $ logtoEndpoint ): OidcCore
30
32
{
33
+ $ defaultDriver = 'Files ' ;
34
+ $ Psr16Adapter = new Psr16Adapter ($ defaultDriver );
31
35
$ client = new Client ();
36
+ $ cacheKey = 'logto_oidc_metadata. ' . urlencode ($ logtoEndpoint );
37
+
38
+ if ($ metadata = $ Psr16Adapter ->get ($ cacheKey )) {
39
+ return new OidcCore (new OidcProviderMetadata (...$ metadata ));
40
+ }
41
+
32
42
$ body = $ client ->get (
33
43
$ logtoEndpoint . '/oidc/.well-known/openid-configuration ' ,
34
44
['headers ' => ['user-agent ' => '@logto/php ' , 'accept ' => '*/* ' ]]
35
45
)->getBody ()->getContents ();
46
+ $ metadata = json_decode ($ body , true );
47
+ $ Psr16Adapter ->set ($ cacheKey , $ metadata , 3600 );
36
48
37
- return new OidcCore (new OidcProviderMetadata (...json_decode ( $ body , true ) ));
49
+ return new OidcCore (new OidcProviderMetadata (...$ metadata ));
38
50
}
39
51
40
52
/** Generate a random string (32 bytes) for the state parameter. */
0 commit comments