1212 * limitations under the License.
1313 **/
1414
15+ use App \Jobs \GenerateOTPRegistrationReminder ;
1516use App \libs \OAuth2 \Exceptions \ReloadSessionException ;
1617use App \libs \OAuth2 \Repositories \IOAuth2OTPRepository ;
1718use App \Services \AbstractService ;
@@ -206,7 +207,7 @@ public function loginWithOTP(OAuth2OTP $otpClaim, ?Client $client = null, bool $
206207 )
207208 );
208209
209- throw new AuthenticationException ("Non existent OTP . " );
210+ throw new AuthenticationException ("Non existent single-use code . " );
210211 }
211212
212213 $ otp ->logRedeemAttempt ();
@@ -216,28 +217,28 @@ public function loginWithOTP(OAuth2OTP $otpClaim, ?Client $client = null, bool $
216217 return $ this ->tx_service ->transaction (function () use ($ otp , $ otpClaim , $ client , $ remember ) {
217218
218219 if (!$ otp ->isAlive ()) {
219- throw new AuthenticationException ("OTP is expired. " );
220+ throw new AuthenticationException ("Single-use code is expired. " );
220221 }
221222
222223 if (!$ otp ->isValid ()) {
223- throw new AuthenticationException ("OTP is not valid. " );
224+ throw new AuthenticationException ("Single-use code is not valid. " );
224225 }
225226
226227 if ($ otp ->getValue () != $ otpClaim ->getValue ()) {
227- throw new AuthenticationException ("OTP mismatch. " );
228+ throw new AuthenticationException ("Single-use code mismatch. " );
228229 }
229230
230231 if (!empty ($ otpClaim ->getScope ()) && !$ otp ->allowScope ($ otpClaim ->getScope ()))
231- throw new InvalidOTPException ("OTP Requested scopes escalates former scopes. " );
232+ throw new InvalidOTPException ("Single-use code requested scopes escalates former scopes. " );
232233
233234 if (($ otp ->hasClient () && is_null ($ client )) ||
234235 ($ otp ->hasClient () && !is_null ($ client ) && $ client ->getClientId () != $ otp ->getClient ()->getClientId ())) {
235- throw new AuthenticationException ("OTP audience mismatch. " );
236+ throw new AuthenticationException ("Single-use code audience mismatch. " );
236237 }
237238
238239 $ user = $ this ->getUserByUsername ($ otp ->getUserName ());
239-
240- if (is_null ( $ user ) ) {
240+ $ new_user = is_null ( $ user );
241+ if ($ new_user ) {
241242 // we need to create a new one ( auto register)
242243
243244 Log::debug (sprintf ("AuthService::loginWithOTP user %s does not exists ... " , $ otp ->getUserName ()));
@@ -268,16 +269,20 @@ public function loginWithOTP(OAuth2OTP $otpClaim, ?Client $client = null, bool $
268269 foreach ($ grants2Revoke as $ otp2Revoke ){
269270 try {
270271 Log::debug (sprintf ("AuthService::loginWithOTP revoking otp %s " , $ otp2Revoke ->getValue ()));
271- if ($ otp2Revoke ->getValue () !== $ otpClaim ->getValue ())
272+ if ($ otp2Revoke ->getValue () !== $ otpClaim ->getValue ())
272273 $ otp2Revoke ->redeem ();
273- }
274- catch (Exception $ ex ){
274+ } catch (Exception $ ex ) {
275275 Log::warning ($ ex );
276276 }
277277 }
278278
279279 Auth::login ($ user , $ remember );
280280
281+ if (!$ user ->hasPasswordSet () && !$ new_user ) {
282+ // trigger background job
283+ GenerateOTPRegistrationReminder::dispatch ($ user );
284+ }
285+
281286 return $ otp ;
282287 });
283288 }
0 commit comments