Skip to content

Commit

Permalink
Merge pull request #48 from participating-online/feature/27-issue-wit…
Browse files Browse the repository at this point in the history
…h-login

Fixed invalid user error
  • Loading branch information
jgrim authored Nov 26, 2023
2 parents 0db6da1 + b63d19f commit f6e5f88
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
Expand Down Expand Up @@ -43,13 +44,13 @@ protected void doFilterInternal(
}

if (userName != null && SecurityContextHolder.getContext().getAuthentication() == null) {
Person person = personRepository.findOneByName(userName);
if (person == null) {
final Optional<Person> person = personRepository.findOneByName(userName);
if (person.isEmpty()) {
throw new UsernameNotFoundException("Invalid name");
}

if (jwtUtil.validateToken(token, person)) {
final JwtPerson authenticationToken = new JwtPerson(person, person.getAuthorities());
if (jwtUtil.validateToken(token, person.get())) {
final JwtPerson authenticationToken = new JwtPerson(person.get(), person.get().getAuthorities());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ GetCaptchaResponse captcha() {
@PostMapping("login")
LoginResponse login(@Valid @RequestBody final Login loginForm) {

final Person person = personRepository.findOneByName(loginForm.username_or_email());
final Person person = personRepository.findOneByName(loginForm.username_or_email())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED));
// @todo verify password
final String token = jwtUtil.generateToken(person);
return LoginResponse.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ GetPersonDetailsResponse show(@Valid final GetPersonDetails getPersonDetailsForm
Person person = null;
if (getPersonDetailsForm.person_id() != null) {
userId = (long) getPersonDetailsForm.person_id();
person = personRepository.findById(userId).orElseThrow();
person = personRepository.findById(userId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "invalid_id_given"));
} else if (getPersonDetailsForm.username() != null) {
person = personRepository.findOneByName(getPersonDetailsForm.username());
person = personRepository.findOneByName(getPersonDetailsForm.username())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "invalid_id_given"));
} else {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "no_id_given");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.sublinks.sublinksapi.utils.models.Mention;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
Expand All @@ -32,20 +33,19 @@ public void onApplicationEvent(CommentCreatedEvent event) {
final List<Mention> mentions = mentionUtils.getPersonMentions(comment.getCommentBody());
if (mentions != null) {
for (Mention mention : mentions) {
Person recipient = personRepository.findOneByName(mention.name());
Optional<Person> recipient = personRepository.findOneByName(mention.name());

if (recipient == null || Objects.equals(recipient, comment.getPerson())) {
if (recipient.isEmpty() || Objects.equals(recipient.get(), comment.getPerson())) {
continue;
}

PersonMention personMention = PersonMention.builder()
.comment(comment)
.recipient(recipient)
.recipient(recipient.get())
.build();

personMentionService.createPersonMention(personMention);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.sublinks.sublinksapi.person.repositories;

import com.sublinks.sublinksapi.person.dto.Person;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PersonRepository extends JpaRepository<Person, Long> {

Person findOneByName(String name);
Optional<Person> findOneByName(String name);
}

0 comments on commit f6e5f88

Please sign in to comment.