Skip to content

Commit

Permalink
Merge pull request #13 from huntmori/12-get-profile-implement
Browse files Browse the repository at this point in the history
12 get profile implement
  • Loading branch information
huntmori authored Jan 18, 2024
2 parents 0bf31b9 + c60c8ba commit 1eae41d
Show file tree
Hide file tree
Showing 18 changed files with 440 additions and 53 deletions.
18 changes: 7 additions & 11 deletions app/repositories.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use App\Application\Common\service\TokenService;
use App\Application\Common\service\TokenServiceImplement;
use App\Application\Middleware\JwtHandler;
use App\Application\Middleware\JwtMiddleware;
use App\Domain\Profile\Repository\ProfileRepository;
Expand All @@ -20,21 +22,15 @@
// $containerBuilder->addDefinitions([
// UserRepository::class => autowire(InMemoryUserRepository::class),
// ]);

$containerBuilder->addDefinitions([
UserService::class => autowire(UserServiceImplement::class),
]);
$containerBuilder->addDefinitions([
UserRepository::class => autowire(UserRepositoryImplement::class),
]);
$containerBuilder->addDefinitions([

ProfileService::class => autowire(ProfileServiceImplement::class),
]);
$containerBuilder->addDefinitions([
ProfileRepository::class => autowire(ProfileRepositoryImplement::class)
]);
$containerBuilder->addDefinitions([
ProfileRepository::class => autowire(ProfileRepositoryImplement::class),

JwtMiddleware::class => autowire(JwtMiddleware::class),
JwtHandler::class => autowire(JwtHandler::class)
JwtHandler::class => autowire(JwtHandler::class),
TokenService::class => autowire(TokenServiceImplement::class)
]);
};
5 changes: 5 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
"",
ProfileController::class . ':createUserProfile'
)->add(JwtMiddleware::class);

$group->get("/{uid}", ProfileController::class.":getProfile")
->add(JwtMiddleware::class);
$group->get("", ProfileController::class.":getProfiles")
->add(JwtMiddleware::class);
});
/*
$app->get("/test", function(Request $request, Response $response) use ($app) {
Expand Down
46 changes: 46 additions & 0 deletions src/Application/Common/model/JwtClaim.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Application\Common\model;

use JsonSerializable;

class JwtClaim implements JsonSerializable
{
public ?string $userId;
public ?string $profileUid;
public ?int $exp;

public function init(string $userId, string $profileUid, int $exp) : JwtClaim
{
$this->userId = $userId;
$this->profileUid = $profileUid;
$this->exp = $exp;

return $this;
}

public function initFromArray(array $claim) : JwtClaim
{
$this->init(
$claim['userId'],
$claim['profileUid'],
$claim['exp']
);

return $this;
}

public function toArray() : array
{
return [
'userId'=>$this->userId,
'profileUid'=>$this->profileUid,
'exp'=> $this->exp
];
}

public function jsonSerialize(): array
{
return $this->toArray();
}
}
12 changes: 12 additions & 0 deletions src/Application/Common/service/TokenService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Application\Common\service;

use App\Application\Common\model\JwtClaim;

interface TokenService
{
public function getUserIdFromToken(string $token);
public function getProfileUidFromToken(string $token);
public function getClaimFromToken(string $token) : ?JwtClaim;
}
50 changes: 50 additions & 0 deletions src/Application/Common/service/TokenServiceImplement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Application\Common\service;

use App\Application\Common\MemberPasswordEncrypt;
use App\Application\Common\model\JwtClaim;
use App\Application\Middleware\JwtHandler;
use Psr\Log\LoggerInterface;

class TokenServiceImplement implements TokenService
{
private ?MemberPasswordEncrypt $passwordEncrypt;
private ?JwtHandler $jwtHandler;
private ?LoggerInterface $logger;

public function __construct(
MemberPasswordEncrypt $passwordEncrypt,
JwtHandler $jwtHandler,
LoggerInterface $logger
) {
$this->passwordEncrypt = $passwordEncrypt;
$this->logger = $logger;
$this->jwtHandler = $jwtHandler;
}

public function getUserIdFromToken(string $token) : string
{
$decodedToken = $this->passwordEncrypt->decrypt($token);
$decryptedJwt = $this->jwtHandler->decryptToken($decodedToken);
$claims = $this->jwtHandler->decodeJwt($decryptedJwt);

return $claims->userId;
}

public function getProfileUidFromToken(string $token) : string
{
$decodedToken = $this->passwordEncrypt->decrypt($token);
$decryptedJwt = $this->jwtHandler->decryptToken($decodedToken);
$claims = $this->jwtHandler->decodeJwt($decryptedJwt);

return $claims->profileUid;
}

public function getClaimFromToken(string $token) : ?JwtClaim
{
$decodedToken = $this->passwordEncrypt->decrypt($token);
$decryptedJwt = $this->jwtHandler->decryptToken($decodedToken);
return $this->jwtHandler->decodeJwt($decryptedJwt);
}
}
29 changes: 19 additions & 10 deletions src/Application/Middleware/JwtHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Application\Middleware;

use App\Application\Common\model\JwtClaim;
use App\Application\Settings\SettingsInterface;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
Expand All @@ -18,34 +19,42 @@ public function __construct(SettingsInterface $settings)
$this->encryptKey = $settings->get('config')['MEMBER_PASSWORD_ENCRYPT_KEY'];
}

public function createToken($userId) :string
public function createToken(array $params) :string
{
$claims = [
'userId' => $userId,
'exp'=>strtotime($this->TOKEN_EXPIRE)
];
$userId = $params['userId'];
$profileUid = $params['profileUid'];

$claims = (new JwtClaim())->init(
$userId,
$profileUid,
strtotime($this->TOKEN_EXPIRE)
);

$token = $this->encodeJwt($claims);
//echo PHP_EOL.'token : '.$token.PHP_EOL;

return $this->encryptToken($token);
}

public function getUserIdFromClaims(array $claims) {
return $claims['userId'];
}

public function encodeJwt($claims) : string
public function encodeJwt(JwtClaim $claims) : string
{
return JWT::encode($claims, $this->encryptKey, $this->jwtEncodeAlgorithm);
return JWT::encode(
$claims->toArray(),
$this->encryptKey,
$this->jwtEncodeAlgorithm
);
}

public function decodeJwt($token) : array
public function decodeJwt(string $token) : JwtClaim
{
$key = new Key($this->encryptKey, $this->jwtEncodeAlgorithm);
$headers = new \stdClass();
$decoded = JWT::decode($token, $key, $headers);

return (array)$decoded;
return (new JwtClaim())->initFromArray((array)$decoded);
}

public function encryptToken(string $token) : string
Expand Down
5 changes: 2 additions & 3 deletions src/Application/Middleware/JwtMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ public function validateTokenReturnUserIdx(string $token) : bool
$claims = $this->jwtHandler->decodeJwt($tokenDecoded);
var_dump($claims);
// 유효기간 확인
$expiredAt = $claims['exp'];
$expiredAt = $claims->exp;
$now = strtotime("now");
if ($expiredAt < $now) {
return false;
}
$userId = $claims['userId'];
// 세션 확인
//session_start();
//$tokens = $_SESSION[$userId];
Expand All @@ -74,7 +73,7 @@ public function validateTokenReturnUserIdx(string $token) : bool
//}

// 유저 확인
return $userId;
return $claims->userId;
}

public function validateToken(string $token) : bool
Expand Down
19 changes: 19 additions & 0 deletions src/Domain/Common/repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,23 @@ public function selectOne(string $sql, array $paramMap, string $returnType) {
$this->disposePdo($pdo);
return $result;
}

public function selectList(string $sql, array $paramMap, string $returnType) : array {
$pdo = $this->getPdo();
$stmt = $pdo->prepare($sql);

$keys = array_keys($paramMap);
for ($i=0; $i<count($keys); $i++) {
$key = $keys[$i];
$stmt->bindValue($key, $paramMap[$key]);
}

$stmt->execute();
$array = [];
while($row = $stmt->fetchObject($returnType)) {
$array[] = $row;
}
$this->disposePdo($pdo);
return $array;
}
}
3 changes: 3 additions & 0 deletions src/Domain/Profile/Repository/ProfileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ public function getUserProfileByProfileIdx(int $profileIdx);
public function getUserProfileByUserUid(string $uid);

public function getUserProfileByProfileUid(string $uid);

public function getUserProfileByUserIdxAndActivate(string $userUid, bool $activated) : array;
public function checkNicknameCount(string $nickname) : int;
}
70 changes: 68 additions & 2 deletions src/Domain/Profile/Repository/ProfileRepositoryImplement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Domain\Profile\entities\Profile;
use App\Domain\Profile\Repository\ProfileRepository;
use Psr\Container\ContainerInterface;
use stdClass;

class ProfileRepositoryImplement extends BaseRepository implements ProfileRepository
{
Expand Down Expand Up @@ -81,8 +82,73 @@ public function getUserProfileByUserUid(string $uid)
return null;
}

public function getUserProfileByProfileUid(string $uid)
public function getUserProfileByProfileUid(string $uid) : ?Profile
{
return null;
return $this->selectOne(
"SELECT
pro.idx,
pro.uid,
pro.user_uid as userUid,
pro.profile_nickname as profileNickName,
pro.is_primary as isPrimary,
pro.deleted,
pro.activated,
pro.banned,
pro.created_at as createdAt,
pro.updated_at as updatedAt
FROM
profile pro
WHERE 1=1
AND pro.uid = :profileUid",
[
'profileUid'=>$uid
],
Profile::class
);
}

public function getUserProfileByUserIdxAndActivate(string $userUid, bool $activated): array
{
return $this->selectList(
"SELECT
pro.idx,
pro.uid,
pro.user_uid as userUid,
pro.profile_nickname as profileNickName,
pro.is_primary as isPrimary,
pro.deleted,
pro.activated,
pro.banned,
pro.created_at as createdAt,
pro.updated_at as updatedAt
FROM
profile pro
WHERE 1=1
AND pro.user_uid = :userUid
AND pro.activated = :activated ",
[
'userUid' => $userUid,
'activated' => $activated
],
Profile::class
);
}

public function checkNicknameCount(string $nickname) : int
{
$result = $this->selectList(
"
SELECT 1
FROM profile pro
WHERE profile_nickname = :nickname
AND deleted = false
",
[
'nickname' => $nickname,
],
stdClass::class
);

return count($result);
}
}
Loading

0 comments on commit 1eae41d

Please sign in to comment.