22
33declare (strict_types=1 );
44
5- namespace Drupal \os2loop_login_hack \Controller ;
5+ namespace Drupal \os2loop_cura_login \Controller ;
66
77use Drupal \Component \Datetime \TimeInterface ;
8+ use Drupal \Core \Config \ImmutableConfig ;
89use Drupal \Core \Controller \ControllerBase ;
9- use Drupal \Core \Entity \ EntityTypeManagerInterface ;
10+ use Drupal \Core \Logger \ RfcLogLevel ;
1011use Drupal \Core \Routing \TrustedRedirectResponse ;
1112use Drupal \Core \Url ;
1213use Drupal \user \Entity \User ;
1314use Drupal \user \UserStorageInterface ;
1415use Firebase \JWT \JWT ;
1516use Firebase \JWT \Key ;
1617use Psr \Log \LoggerInterface ;
18+ use Psr \Log \LoggerTrait ;
19+ use Psr \Log \LogLevel ;
1720use Symfony \Component \DependencyInjection \Attribute \Autowire ;
1821use Symfony \Component \HttpFoundation \Exception \BadRequestException ;
1922use Symfony \Component \HttpFoundation \JsonResponse ;
2225use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
2326
2427/**
25- * Returns responses for os2loop_login_hack routes.
28+ * Returns responses for os2loop_cura_login routes.
2629 */
27- final class Os2loopLoginHackController extends ControllerBase {
28- private const JWT_KEY = 'os2loop_login_hack ' ;
30+ final class Os2loopCuraLoginController extends ControllerBase {
31+ use LoggerTrait;
32+
33+ private const JWT_KEY = 'os2loop_cura_login ' ;
2934
3035 /**
3136 * The user storage.
3237 */
3338 private readonly UserStorageInterface $ userStorage ;
3439
40+ /**
41+ * The module config.
42+ */
43+ private readonly ImmutableConfig $ config ;
44+
3545 /**
3646 * Constructor.
3747 */
3848 public function __construct (
39- EntityTypeManagerInterface $ entityTypeManager ,
4049 private readonly TimeInterface $ time ,
41- #[Autowire(service: 'logger.channel.os2loop_login_hack ' )]
50+ #[Autowire(service: 'logger.channel.os2loop_cura_login ' )]
4251 private readonly LoggerInterface $ logger ,
4352 ) {
44- $ this ->userStorage = $ entityTypeManager ->getStorage ('user ' );
53+ $ this ->userStorage = $ this ->entityTypeManager ()->getStorage ('user ' );
54+ $ this ->config = $ this ->config ('os2loop_cura_login.settings ' );
4555 }
4656
4757 /**
4858 * Start user authentication.
4959 */
5060 public function start (Request $ request ): Response {
5161 try {
52- $ this ->logger -> info ('Request: @request ' , [
62+ $ this ->info ('Request: @request ' , [
5363 '@request ' => json_encode ([
5464 'method ' => $ request ->getMethod (),
5565 'query ' => $ request ->query ->all (),
5666 'content ' => (string ) $ request ->getContent (),
5767 ]),
5868 ]);
5969
60- return new Response ('https://example.com/cura-login ' );
70+ $ jwt = Request::METHOD_POST === $ request ->getMethod ()
71+ ? $ request ->getContent ()
72+ : $ request ->query ->getString ($ this ->config ->get ('token_param_name ' ) ?? 'token ' );
73+
74+ $ payload = (array ) JWT ::decode ($ jwt , new Key ($ this ->config ->get ('signing_secret ' ), $ this ->config ->get ('signing_algorithm ' )));
6175
62- $ data = json_decode ($ request ->getContent (), associative: TRUE , flags: JSON_THROW_ON_ERROR );
63- $ username = $ data ['username ' ] ?? NULL ;
76+ $ username = $ payload ['username ' ] ?? NULL ;
6477 if (empty ($ username )) {
6578 throw new BadRequestHttpException ('Missing username ' );
6679 }
@@ -87,7 +100,7 @@ public function start(Request $request): Response {
87100 ];
88101 $ jwt = JWT ::encode ($ payload , self ::JWT_KEY , 'HS256 ' );
89102
90- $ url = Url::fromRoute ('os2loop_login_hack .authenticate ' , [
103+ $ url = Url::fromRoute ('os2loop_cura_login .authenticate ' , [
91104 'username ' => $ username ,
92105 'jwt ' => $ jwt ,
93106 ])->setAbsolute ()->toString (TRUE )->getGeneratedUrl ();
@@ -98,7 +111,7 @@ public function start(Request $request): Response {
98111 ]);
99112 }
100113 catch (\Exception $ exception ) {
101- $ this ->logger -> error ('start: @message ' , ['@message ' => $ exception ->getMessage (), $ exception ]);
114+ $ this ->error ('start: @message ' , ['@message ' => $ exception ->getMessage (), $ exception ]);
102115 throw new BadRequestException ($ exception ->getMessage ());
103116 }
104117 }
@@ -136,7 +149,7 @@ public function authenticate(Request $request): Response {
136149 return new TrustedRedirectResponse ($ url );
137150 }
138151 catch (\Exception $ exception ) {
139- $ this ->logger -> error ('start: @message ' , ['@message ' => $ exception ->getMessage (), $ exception ]);
152+ $ this ->error ('start: @message ' , ['@message ' => $ exception ->getMessage (), $ exception ]);
140153 throw new BadRequestException ($ exception ->getMessage ());
141154 }
142155 }
@@ -174,4 +187,23 @@ private function getUserinfo(User $user): array {
174187 ];
175188 }
176189
190+ public function log ($ level , \Stringable |string $ message , array $ context = []): void
191+ {
192+ // Lifted from LoggerChannel
193+ $ levels = [
194+ LogLevel::EMERGENCY => RfcLogLevel::EMERGENCY ,
195+ LogLevel::ALERT => RfcLogLevel::ALERT ,
196+ LogLevel::CRITICAL => RfcLogLevel::CRITICAL ,
197+ LogLevel::ERROR => RfcLogLevel::ERROR ,
198+ LogLevel::WARNING => RfcLogLevel::WARNING ,
199+ LogLevel::NOTICE => RfcLogLevel::NOTICE ,
200+ LogLevel::INFO => RfcLogLevel::INFO ,
201+ LogLevel::DEBUG => RfcLogLevel::DEBUG ,
202+ ];
203+ $ rfcLogLevel = $ levels [$ level ] ?? RfcLogLevel::ERROR ;
204+ if ((int )$ this ->config ->get ('log_level ' ) >= $ rfcLogLevel ) {
205+ $ this ->logger ->log ($ level , $ message , $ context );
206+ }
207+ }
208+
177209}
0 commit comments