Skip to content

Commit

Permalink
token and profile sql
Browse files Browse the repository at this point in the history
- 토큰 claim에 primary profile uid 추가
- 토큰 로직 수정
- 프로필 select sql 추가
- primary 프로필 없을 시 첫번째 프로필을 primary로 취급
  • Loading branch information
huntmori committed Jan 14, 2024
1 parent 0bf31b9 commit e850ab0
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/Application/Common/service/TokenService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Application\Common\service;

interface TokenService
{

}
32 changes: 32 additions & 0 deletions src/Application/Common/service/TokenServiceImplement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Application\Common\service;

use App\Application\Common\MemberPasswordEncrypt;
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) {
$decodedToken = $this->passwordEncrypt->decrypt($token);
$decryptedJwt = $this->jwtHandler->decryptToken($decodedToken);
$claims = $this->jwtHandler->decodeJwt($decryptedJwt);

return $this->jwtHandler->getUserIdFromClaims($claims);
}
}
7 changes: 5 additions & 2 deletions src/Application/Middleware/JwtHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ 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
{
$userId = $params['userId'];
$profileUid = $params['profileUid'];
$claims = [
'userId' => $userId,
'profileUid' => $profileUid,
'exp'=>strtotime($this->TOKEN_EXPIRE)
];

var_dump($claims);
$token = $this->encodeJwt($claims);
//echo PHP_EOL.'token : '.$token.PHP_EOL;
return $this->encryptToken($token);
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;
}
}
2 changes: 2 additions & 0 deletions src/Domain/Profile/Repository/ProfileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public function getUserProfileByProfileIdx(int $profileIdx);
public function getUserProfileByUserUid(string $uid);

public function getUserProfileByProfileUid(string $uid);

public function getUserProfileByUserIdxAndActivate(string $userUid, bool $activated) : array;
}
27 changes: 27 additions & 0 deletions src/Domain/Profile/Repository/ProfileRepositoryImplement.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,31 @@ public function getUserProfileByProfileUid(string $uid)
{
return null;
}

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
);
}
}
7 changes: 7 additions & 0 deletions src/Domain/Profile/controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ public function createUserProfile(Request $request, Response $response, array $a
$profile = $this->profileService->createUserProfileByRequestDto($requestDto);
return $this->respondWithData($response, $profile->toArray(), 200);
}

public function getProfiles(Request $request, Response $response, array $args) : Response
{
$token = $this->profileService->getUserProfilesByRequest($request);

}

}
2 changes: 1 addition & 1 deletion src/Domain/Profile/service/ProfileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

interface ProfileService
{
public function getUserProfiles(int $userIdx) : array;
public function getUserProfiles(string $uid) : array;
public function createUserProfile(int $userIdx, string $userUid, string $nickName);

public function checkNickNameDuplicate(string $nickName): bool;
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/Profile/service/ProfileServiceImplement.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function __construct(
$this->encrypt = $encrypt;
}

public function getUserProfiles(int $userIdx): array
public function getUserProfiles(string $uid): array
{
return [];
return $this->profileRepository->getUserProfileByUserIdxAndActivate($uid, true);
}

public function createUserProfile(int $userIdx, string $userUid, string $nickName) : int
Expand Down
5 changes: 4 additions & 1 deletion src/Domain/User/controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Domain\User\controller;

use App\Domain\Common\controller\ActionBasedController;
use App\Domain\Profile\service\ProfileService;
use App\Domain\User\models\UserCreateRequest;
use App\Domain\User\models\UserLoginRequest;
use App\Domain\User\service\UserService;
Expand All @@ -14,6 +15,7 @@ class UserController extends ActionBasedController
{
private LoggerInterface $logger;
private UserService $userService;
private ProfileService $profileService;

public function __construct(
LoggerInterface $logger,
Expand Down Expand Up @@ -59,7 +61,8 @@ public function userLogin(Request $request, Response $response, array $args) : R
$requestBody = new UserLoginRequest(json_decode($request->getBody()));

$userLoginResult = $this->userService->userLogin($requestBody);
echo json_encode($userLoginResult).PHP_EOL;


return $this->respondWithData($response, $userLoginResult, 200);
}
}
3 changes: 2 additions & 1 deletion src/Domain/User/service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Domain\User\service;

use App\Domain\Profile\entities\Profile;
use App\Domain\User\entities\User;
use App\Domain\User\models\UserCreateRequest;
use App\Domain\User\models\UserLoginRequest;
Expand All @@ -19,7 +20,7 @@ public function userCreateParams(
) : User;

public function userLogin(UserLoginRequest $requestBody) : UserLoginResponse;
public function getUserJwtToken(User $user) : string;
public function getUserJwtToken(User $user, Profile $primaryProfile) : string;

public function getUserByUserId(string $userId) : ?User;
public function getUserByUserIdx(int $userIdx) : ?User;
Expand Down
37 changes: 32 additions & 5 deletions src/Domain/User/service/UserServiceImplement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Application\Common\MemberPasswordEncrypt;
use App\Application\Middleware\JwtHandler;
use App\Domain\Profile\entities\Profile;
use App\Domain\Profile\service\ProfileService;
use App\Domain\User\entities\User;
use App\Domain\User\exceptions\PasswordNotMatchException;
Expand Down Expand Up @@ -106,10 +107,33 @@ public function userLogin(UserLoginRequest $requestBody): UserLoginResponse
throw new PasswordNotMatchException("User not found. may be invalid password or user id", 401);
}

$token = $this->getUserJwtToken($user);

//todo : get users profiles
$profiles = $this->profileService->getUserProfiles($user->getIdx());
$profiles = $this->profileService->getUserProfiles($user->getUid());
$mainProfileExist = false;
$primaryProfile = null;
for($i=0; $i<count($profiles); $i++) {
/** @var Profile $profile */
$profile = $profiles[$i];
if($profile->getIsPrimary()) {
$mainProfileExist = true;
$primaryProfile = $profile;
break;
}
}

if($mainProfileExist === false) {
for($i=0; $i<count($profiles); $i++) {
/** @var Profile $profile */
$profile = $profiles[$i];
$profile->setIsPrimary(true);
$primaryProfile = $profile;
break;
}
}

$token = $this->getUserJwtToken($user, $primaryProfile);



return new UserLoginResponse([
'profiles'=>$profiles,
Expand All @@ -118,15 +142,18 @@ public function userLogin(UserLoginRequest $requestBody): UserLoginResponse
]);
}

public function getUserJwtToken(User $user) : string
public function getUserJwtToken(User $user, ?Profile $primaryProfile) : string
{
$userIdx = $user->getIdx();
$redisTokenKey = "JWT_{$userIdx}";

$isSetRedis = $this->redisClient->get($redisTokenKey);
$token = null;
$tokenParameter = [];
$tokenParameter['userId'] = $user->getId();
$tokenParameter['profileUid'] = $primaryProfile?->getUid();
if (is_null($isSetRedis) || true) {
$token = $this->jwtHandler->createToken($user->getId());
$token = $this->jwtHandler->createToken($tokenParameter);
$token = $this->memberPasswordEncrypt->encrypt($token);
$this->redisClient->setex($redisTokenKey, 86400, $token);
} else {
Expand Down

0 comments on commit e850ab0

Please sign in to comment.