• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / auth / 23386290410

21 Mar 2026 06:48PM UTC coverage: 64.989% (+1.2%) from 63.76%
23386290410

push

github

web-flow
Merge pull request #43 from daycry/development

v5.0.0

302 of 465 new or added lines in 26 files covered. (64.95%)

19 existing lines in 3 files now uncovered.

3306 of 5087 relevant lines covered (64.99%)

47.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.88
/src/Models/OAuthTokenRepository.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Auth.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Auth\Models;
15

16
use Daycry\Auth\Entities\UserIdentity;
17
use Daycry\Auth\Enums\IdentityType;
18

19
/**
20
 * Repository for OAuth identity operations.
21
 *
22
 * Encapsulates all OAuth token/identity CRUD from UserIdentityModel
23
 * into a focused, single-responsibility class.
24
 */
25
class OAuthTokenRepository
26
{
27
    public function __construct(
26✔
28
        private readonly UserIdentityModel $identityModel,
29
    ) {
30
    }
26✔
31

32
    /**
33
     * Find an OAuth identity for a given user and provider.
34
     */
35
    public function findByUserAndProvider(int $userId, string $provider): ?UserIdentity
11✔
36
    {
37
        /** @var UserIdentity|null */
38
        return $this->identityModel
11✔
39
            ->where('user_id', $userId)
11✔
40
            ->where('type', IdentityType::oauthProvider($provider))
11✔
41
            ->first();
11✔
42
    }
43

44
    /**
45
     * Find an OAuth identity by provider and social ID (the 'secret' column).
46
     */
47
    public function findByProviderAndSocialId(string $provider, string $socialId): ?UserIdentity
11✔
48
    {
49
        /** @var UserIdentity|null */
50
        return $this->identityModel
11✔
51
            ->where('type', IdentityType::oauthProvider($provider))
11✔
52
            ->where('secret', $socialId)
11✔
53
            ->first();
11✔
54
    }
55

56
    /**
57
     * Create a new OAuth identity row.
58
     *
59
     * @param array<string, mixed> $data Must include: name, secret, secret2, extra, expires (nullable)
60
     */
61
    public function createOAuthIdentity(int $userId, string $provider, array $data): void
10✔
62
    {
63
        $this->identityModel->insert(array_merge($data, [
10✔
64
            'user_id' => $userId,
10✔
65
            'type'    => IdentityType::oauthProvider($provider),
10✔
66
        ]));
10✔
67
    }
68

69
    /**
70
     * Update an existing OAuth identity (token refresh, re-login, etc.).
71
     */
72
    public function updateOAuthIdentity(UserIdentity $identity): void
4✔
73
    {
74
        $this->identityModel->save($identity);
4✔
75
    }
76

77
    /**
78
     * Get the stored profile data for a user's OAuth identity.
79
     *
80
     * @return array<string, mixed>
81
     */
82
    public function getProfileData(int $userId, string $provider): array
4✔
83
    {
84
        $identity = $this->findByUserAndProvider($userId, $provider);
4✔
85

86
        if ($identity === null || empty($identity->extra)) {
4✔
NEW
87
            return [];
×
88
        }
89

90
        $extraData = $this->parseExtra($identity->extra);
4✔
91

92
        return $extraData['profile'] ?? [];
4✔
93
    }
94

95
    /**
96
     * Parse the extra field from an OAuth identity.
97
     *
98
     * Handles backward compatibility: legacy format stored the refresh token
99
     * as a plain string, new format uses JSON.
100
     *
101
     * @return array<string, mixed>
102
     */
103
    public function parseExtra(?string $extra): array
10✔
104
    {
105
        if ($extra === null || $extra === '') {
10✔
106
            return [];
1✔
107
        }
108

109
        $decoded = json_decode($extra, true);
9✔
110

111
        if (is_array($decoded) && json_last_error() === JSON_ERROR_NONE) {
9✔
112
            return $decoded;
5✔
113
        }
114

115
        // Legacy: plain string is the refresh token
116
        return ['refresh_token' => $extra];
4✔
117
    }
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc