File tree Expand file tree Collapse file tree 5 files changed +26
-7
lines changed
java/com/starter/springboot/services Expand file tree Collapse file tree 5 files changed +26
-7
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ Spring Boot project with demonstration of OTP authentication technique.
55
66## Running the project
77
8- 1 . Create database with name ** test **
8+ 1 . Create database with name ** otp **
992 . Open terminal and navigate to your project
10103 . Type command ** mvn install**
11114 . Type command ** mvn spring-boot: run **
@@ -34,12 +34,13 @@ Response: `{ id_token: "token_hash" }`
3434
35351 . Generate OTP and send it to e-mail <br >
3636Route: /api/otp/generate <br >
37- Method: GET <br ><br >
37+ Method: POST <br ><br >
38+ Empty request body in this case.
3839
39402 . Validate OTP <br >
4041Route: /api/otp/validate <br >
4142Method: POST <br >
42- Example Payload: { "otp": "your otp number" }
43+ Example Request Payload: { "otp": "your otp number" }
4344<br >
4445
4546
Original file line number Diff line number Diff line change @@ -17,16 +17,19 @@ public class OtpService {
1717
1818 private OtpGenerator otpGenerator ;
1919 private EmailService emailService ;
20+ private UserService userService ;
2021
2122 /**
2223 * Constructor dependency injector
2324 * @param otpGenerator - otpGenerator dependency
2425 * @param emailService - email service dependency
26+ * @param userService - user service dependency
2527 */
26- public OtpService (OtpGenerator otpGenerator , EmailService emailService )
28+ public OtpService (OtpGenerator otpGenerator , EmailService emailService , UserService userService )
2729 {
2830 this .otpGenerator = otpGenerator ;
2931 this .emailService = emailService ;
32+ this .userService = userService ;
3033 }
3134
3235 /**
@@ -48,7 +51,7 @@ public Boolean generateOtp(String key)
4851 LOGGER .info ("Generated OTP: {}" , otpValue );
4952
5053 // fetch user e-mail from database
51- String userEmail =
"[email protected] " ;
54+ String userEmail = userService . findEmailByUsername ( key ) ;
5255 List <String > recipients = new ArrayList <>();
5356 recipients .add (userEmail );
5457
Original file line number Diff line number Diff line change 66import org .springframework .stereotype .Service ;
77
88import java .util .List ;
9+ import java .util .Optional ;
910
1011@ Service
1112public class UserService {
@@ -22,4 +23,18 @@ public List<User> findAllUsers() {
2223 return this .userRepository .findAll ();
2324 }
2425
26+ /**
27+ * Method for getting e-mail by username (key)
28+ *
29+ * @param username - provided username
30+ * @return e-mail
31+ */
32+ public String findEmailByUsername (String username )
33+ {
34+ Optional <User > user = userRepository .findByUsername (username );
35+ if (user .isPresent ()) {
36+ return user .get ().getEmail ();
37+ }
38+ return null ;
39+ }
2540}
Original file line number Diff line number Diff line change 33 restart :
44 enabled : true
55 datasource :
6- url : jdbc:mysql://localhost/test ?useUnicode=true&characterEncoding=utf8&useSSL=false
6+ url : jdbc:mysql://localhost/otp ?useUnicode=true&characterEncoding=utf8&useSSL=false
77 name :
88 username : root
99 password : root
Original file line number Diff line number Diff line change 11"id";"first_name";"last_name";"username";"email";"password";"enabled";"last_password_reset_date"
2- "1";"Administrator";"";"admin";"admin@admin .com";"$2a$10$gSAhZrxMllrbgj/kkK9UceBPpChGWJA7SYIb1Mqo.n5aNLq1/oRrC";true;NULL
2+ "1";"Administrator";"";"admin";"heril.muratovic@logate .com";"$2a$10$gSAhZrxMllrbgj/kkK9UceBPpChGWJA7SYIb1Mqo.n5aNLq1/oRrC";true;NULL
You can’t perform that action at this time.
0 commit comments