Laravel AT Protocol Client (alpha & unstable)
1<?php
2
3namespace SocialDept\AtpClient\Providers;
4
5use SocialDept\AtpClient\Contracts\CredentialProvider;
6use SocialDept\AtpClient\Data\AccessToken;
7use SocialDept\AtpClient\Data\Credentials;
8
9class FileCredentialProvider implements CredentialProvider
10{
11 protected string $storagePath;
12
13 public function __construct(?string $storagePath = null)
14 {
15 $this->storagePath = $storagePath ?? storage_path('app/atp-credentials');
16
17 if (! is_dir($this->storagePath)) {
18 mkdir($this->storagePath, 0755, true);
19 }
20 }
21
22 public function getCredentials(string $did): ?Credentials
23 {
24 $path = $this->path($did);
25
26 if (! file_exists($path)) {
27 return null;
28 }
29
30 return unserialize(file_get_contents($path));
31 }
32
33 public function storeCredentials(string $did, AccessToken $token): void
34 {
35 file_put_contents($this->path($did), serialize($this->toCredentials($token)));
36 }
37
38 public function updateCredentials(string $did, AccessToken $token): void
39 {
40 $this->storeCredentials($did, $token);
41 }
42
43 public function removeCredentials(string $did): void
44 {
45 $path = $this->path($did);
46
47 if (file_exists($path)) {
48 unlink($path);
49 }
50 }
51
52 protected function path(string $did): string
53 {
54 return $this->storagePath.'/'.hash('sha256', $did).'.cred';
55 }
56
57 protected function toCredentials(AccessToken $token): Credentials
58 {
59 return new Credentials(
60 did: $token->did,
61 accessToken: $token->accessJwt,
62 refreshToken: $token->refreshJwt,
63 expiresAt: $token->expiresAt,
64 handle: $token->handle,
65 issuer: $token->issuer,
66 scope: $token->scope,
67 authType: $token->authType,
68 );
69 }
70}