+52
src/Providers/CacheCredentialProvider.php
+52
src/Providers/CacheCredentialProvider.php
···
1
+
<?php
2
+
3
+
namespace SocialDept\AtpClient\Providers;
4
+
5
+
use Illuminate\Support\Facades\Cache;
6
+
use SocialDept\AtpClient\Contracts\CredentialProvider;
7
+
use SocialDept\AtpClient\Data\AccessToken;
8
+
use SocialDept\AtpClient\Data\Credentials;
9
+
10
+
class CacheCredentialProvider implements CredentialProvider
11
+
{
12
+
protected string $prefix = 'atp:credentials:';
13
+
14
+
public function getCredentials(string $did): ?Credentials
15
+
{
16
+
return Cache::get($this->key($did));
17
+
}
18
+
19
+
public function storeCredentials(string $did, AccessToken $token): void
20
+
{
21
+
Cache::put($this->key($did), $this->toCredentials($token));
22
+
}
23
+
24
+
public function updateCredentials(string $did, AccessToken $token): void
25
+
{
26
+
$this->storeCredentials($did, $token);
27
+
}
28
+
29
+
public function removeCredentials(string $did): void
30
+
{
31
+
Cache::forget($this->key($did));
32
+
}
33
+
34
+
protected function key(string $did): string
35
+
{
36
+
return $this->prefix.$did;
37
+
}
38
+
39
+
protected function toCredentials(AccessToken $token): Credentials
40
+
{
41
+
return new Credentials(
42
+
did: $token->did,
43
+
accessToken: $token->accessJwt,
44
+
refreshToken: $token->refreshJwt,
45
+
expiresAt: $token->expiresAt,
46
+
handle: $token->handle,
47
+
issuer: $token->issuer,
48
+
scope: $token->scope,
49
+
authType: $token->authType,
50
+
);
51
+
}
52
+
}
+70
src/Providers/FileCredentialProvider.php
+70
src/Providers/FileCredentialProvider.php
···
1
+
<?php
2
+
3
+
namespace SocialDept\AtpClient\Providers;
4
+
5
+
use SocialDept\AtpClient\Contracts\CredentialProvider;
6
+
use SocialDept\AtpClient\Data\AccessToken;
7
+
use SocialDept\AtpClient\Data\Credentials;
8
+
9
+
class 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
+
}
+52
src/Providers/SessionCredentialProvider.php
+52
src/Providers/SessionCredentialProvider.php
···
1
+
<?php
2
+
3
+
namespace SocialDept\AtpClient\Providers;
4
+
5
+
use Illuminate\Support\Facades\Session;
6
+
use SocialDept\AtpClient\Contracts\CredentialProvider;
7
+
use SocialDept\AtpClient\Data\AccessToken;
8
+
use SocialDept\AtpClient\Data\Credentials;
9
+
10
+
class SessionCredentialProvider implements CredentialProvider
11
+
{
12
+
protected string $prefix = 'atp.credentials.';
13
+
14
+
public function getCredentials(string $did): ?Credentials
15
+
{
16
+
return Session::get($this->key($did));
17
+
}
18
+
19
+
public function storeCredentials(string $did, AccessToken $token): void
20
+
{
21
+
Session::put($this->key($did), $this->toCredentials($token));
22
+
}
23
+
24
+
public function updateCredentials(string $did, AccessToken $token): void
25
+
{
26
+
$this->storeCredentials($did, $token);
27
+
}
28
+
29
+
public function removeCredentials(string $did): void
30
+
{
31
+
Session::forget($this->key($did));
32
+
}
33
+
34
+
protected function key(string $did): string
35
+
{
36
+
return $this->prefix.$did;
37
+
}
38
+
39
+
protected function toCredentials(AccessToken $token): Credentials
40
+
{
41
+
return new Credentials(
42
+
did: $token->did,
43
+
accessToken: $token->accessJwt,
44
+
refreshToken: $token->refreshJwt,
45
+
expiresAt: $token->expiresAt,
46
+
handle: $token->handle,
47
+
issuer: $token->issuer,
48
+
scope: $token->scope,
49
+
authType: $token->authType,
50
+
);
51
+
}
52
+
}