Skip to content

Commit

Permalink
Merge pull request #22 from citrus-framework/fix_controller_response
Browse files Browse the repository at this point in the history
レスポンス処理の共通化
  • Loading branch information
take64 authored Jul 20, 2023
2 parents 23c689e + 72fbd77 commit b42d362
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store

.idea/
.scannerwork/
src/Library/smarty3/
Expand Down
28 changes: 6 additions & 22 deletions src/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ class Authentication extends Configurable
public const SESSION_KEY = 'authentication';

/** @var string 認証テーブル名 */
public static $AUTHORIZE_TABLE_NAME = 'users';
public static string $AUTHORIZE_TABLE_NAME = 'users';

/** @var string token生成アルゴリズム */
public static $TOKEN_ALGO = 'sha256';
public static string $TOKEN_ALGO = 'sha256';

/** @var int ログイン維持時間(秒) */
public static $KEEP_SECOND = (60 * 60 * 24);
public static int $KEEP_SECOND = (60 * 60 * 24);

/** @var Protocol 認証タイプインスタンス */
public $protocol = null;
/** @var Protocol|null 認証タイプインスタンス */
public Protocol|null $protocol = null;



Expand All @@ -65,8 +65,6 @@ public function loadConfigures(array $configures = []): Configurable
return $this;
}



/**
* 認証処理
*
Expand All @@ -83,8 +81,6 @@ public function authorize(AuthItem $item): bool
return $this->protocol->authorize($item);
}



/**
* 認証解除処理
*
Expand All @@ -100,8 +96,6 @@ public function deAuthorize(): bool
return $this->protocol->deAuthorize();
}



/**
* 認証のチェック
* 認証できていれば期間の延長
Expand All @@ -119,8 +113,6 @@ public function isAuthenticated(AuthItem|null $item = null): bool
return $this->protocol->isAuthenticated($item);
}



/**
* ログイントークンの生成
*
Expand All @@ -144,14 +136,12 @@ public static function generateToken(string|null $key = null): string
);

// tokenキー
$key = ($key ?? Session::$sessionId);
$key ??= Session::$sessionId;

// token生成し返却
return hash(self::$TOKEN_ALGO, $key);
}



/**
* ログイン維持制限時間の生成
*
Expand All @@ -162,8 +152,6 @@ public static function generateKeepAt(): string
return Dates::now()->addSecond(self::$KEEP_SECOND)->format('Y-m-d H:i:s');
}



/**
* {@inheritDoc}
*/
Expand All @@ -172,8 +160,6 @@ protected function configureKey(): string
return 'authentication';
}



/**
* {@inheritDoc}
*/
Expand All @@ -184,8 +170,6 @@ protected function configureDefaults(): array
];
}



/**
* {@inheritDoc}
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Citrus\CitrusException;
use Citrus\Http\Server\Request;
use Citrus\Http\Server\Response;
use Citrus\Http\Server\ResponseTo;
use Citrus\Logger;
use Citrus\Message;
use Citrus\Message\MessageItem;
Expand All @@ -37,7 +38,7 @@ class ApiController extends BaseController
public function run(Router|null $router = null): void
{
// ルーター
$router = ($router ?? Router::sharedInstance()->factory());
$router ??= Router::sharedInstance()->factory();
$this->router = $router;

try
Expand All @@ -46,6 +47,7 @@ public function run(Router|null $router = null): void

$request = Request::generate();
$this->initialize($request);
/** @var ResponseTo $response */
$response = $this->$action_name($request);
$this->release($request);
if (true === Message::exists())
Expand All @@ -62,10 +64,8 @@ public function run(Router|null $router = null): void
Message::removeAll();
}

$response_json = json_encode($response);

// 出力
echo $response_json;
echo $response->toJson();
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Controller/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public function signin(Request $request): Response
return AuthResponse::withToken($item->token);
}



/**
* ユーザー情報
*
Expand Down Expand Up @@ -81,7 +79,6 @@ public function user(Request $request): Response
return AuthResponse::withItem($item);
}


/**
* 認証チェック
*/
Expand Down
6 changes: 2 additions & 4 deletions src/Controller/AuthResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class AuthResponse extends Response
use Binders;

/** @var String 認証用トークン */
public $token;
public string $token;

/** @var array 認証用アイテム */
public $user;
public array $user;



Expand All @@ -48,8 +48,6 @@ public static function withToken(string $token): self
return $self;
}



/**
* user返却用レスポンスの生成
*
Expand Down

0 comments on commit b42d362

Please sign in to comment.