44
55use App \Exceptions \ForbiddenRequestException ;
66use App \Exceptions \InvalidAccessTokenException ;
7+ use App \Exceptions \NotFoundException ;
78use App \Exceptions \WrongCredentialsException ;
9+ use App \Model \Entity \User ;
810use App \Model \Repository \Users ;
911use App \Helpers \RecodexApiHelper ;
1012use App \Helpers \RecodexUser ;
1113use App \Security \AccessManager ;
1214use App \Security \Roles ;
1315use App \Security \TokenScope ;
1416use Nette \Security \AuthenticationException ;
17+ use Exception ;
1518
1619/**
1720 * Endpoints used to log a user in
@@ -42,6 +45,34 @@ class LoginPresenter extends BasePresenter
4245 */
4346 public $ roles ;
4447
48+ /**
49+ * Split the ReCodEx API token (save it to DB and suffix to the newly generated token),
50+ * generate a new token for our frontend and send the response.
51+ * @param User $user The user to log in
52+ * @param string $token The token from ReCodEx API to split and save
53+ */
54+ private function finalizeLogin (User $ user , string $ token ): void
55+ {
56+ // part of the token is stored in the database, suffix goes into our token (payload)
57+ $ tokenSuffix = $ user ->setRecodexToken ($ token );
58+ $ user ->updatedNow ();
59+ $ this ->users ->persist ($ user );
60+
61+ // generate our token for our frontend
62+ $ token = $ this ->accessManager ->issueToken (
63+ $ user ,
64+ null , // no effective role override
65+ [TokenScope::MASTER , TokenScope::REFRESH ],
66+ null , // default expiration
67+ ['suffix ' => $ tokenSuffix ]
68+ );
69+
70+ $ this ->sendSuccessResponse ([
71+ "accessToken " => $ token ,
72+ "user " => $ user ,
73+ ]);
74+ }
75+
4576 /**
4677 * Log in using temp token from ReCodEx.
4778 * @POST
@@ -70,25 +101,8 @@ public function actionDefault()
70101 } else {
71102 $ recodexUser ->updateUser ($ user );
72103 }
73- $ user ->updatedNow ();
74-
75- // part of the token is stored in the database, suffix goes into our token (payload)
76- $ tokenSuffix = $ user ->setRecodexToken ($ recodexResponse ['accessToken ' ]);
77- $ this ->users ->persist ($ user );
78-
79- // generate our token for our frontend
80- $ token = $ this ->accessManager ->issueToken (
81- $ user ,
82- null , // no effective role override
83- [TokenScope::MASTER , TokenScope::REFRESH ],
84- null , // default expiration
85- ['suffix ' => $ tokenSuffix ]
86- );
87104
88- $ this ->sendSuccessResponse ([
89- "accessToken " => $ token ,
90- "user " => $ user ,
91- ]);
105+ $ this ->finalizeLogin ($ user , $ recodexResponse ['accessToken ' ]);
92106 }
93107
94108 /**
@@ -104,23 +118,24 @@ public function checkRefresh()
104118 }
105119
106120 /**
107- * Refresh the access token of current user
121+ * Refresh the access token of current user (as well as the ReCodEx API token).
108122 * @GET
109123 * @LoggedIn
124+ * @throws AuthenticationException
110125 * @throws ForbiddenRequestException
126+ * @throws NotFoundException
127+ * @throws InvalidAccessTokenException
111128 */
112129 public function actionRefresh ()
113130 {
114- $ token = $ this ->getAccessToken ();
131+ $ recodexResponse = $ this ->recodexApi ->refreshToken ();
132+ /** @var RecodexUser */
133+ $ recodexUser = $ recodexResponse ['user ' ];
115134
116- $ user = $ this ->getCurrentUser ();
117- $ this ->users ->flush ();
135+ // Update the user entity with new info from ReCodEx.
136+ $ user = $ this ->users ->findOrThrow ($ recodexUser ->getId ());
137+ $ recodexUser ->updateUser ($ user );
118138
119- $ this ->sendSuccessResponse (
120- [
121- "accessToken " => $ this ->accessManager ->issueRefreshedToken ($ token ),
122- "user " => $ user ,
123- ]
124- );
139+ $ this ->finalizeLogin ($ user , $ recodexResponse ['accessToken ' ]);
125140 }
126141}
0 commit comments