-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor login, use "send email to login"
- Loading branch information
Showing
14 changed files
with
279 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
interweb-server/src/main/java/de/l3s/interweb/server/config/ExceptionMappers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package de.l3s.interweb.server.config; | ||
|
||
import jakarta.ws.rs.BadRequestException; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.jboss.resteasy.reactive.RestResponse; | ||
import org.jboss.resteasy.reactive.server.ServerExceptionMapper; | ||
|
||
public class ExceptionMappers { | ||
@ServerExceptionMapper | ||
public RestResponse<HttpError> mapException(BadRequestException x) { | ||
return RestResponse.status(Response.Status.BAD_REQUEST, HttpError.of(x)); | ||
} | ||
|
||
public static final class HttpError { | ||
private String message; | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public static HttpError of(Exception e) { | ||
HttpError error = new HttpError(); | ||
error.message = e.getMessage(); | ||
return error; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return message; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
interweb-server/src/main/java/de/l3s/interweb/server/features/user/UserToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package de.l3s.interweb.server.features.user; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
|
||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
import io.quarkus.hibernate.reactive.panache.PanacheEntityBase; | ||
import io.quarkus.security.credential.Credential; | ||
import io.smallrye.mutiny.Uni; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
|
||
import de.l3s.interweb.core.util.StringUtils; | ||
|
||
@Entity | ||
@Cacheable | ||
@Table(name = "user_token") | ||
public class UserToken extends PanacheEntityBase implements Credential { | ||
|
||
public enum Type { | ||
login(Duration.ofHours(6), 32); | ||
|
||
private final Duration duration; | ||
private final int size; | ||
|
||
Type(Duration duration, int size) { | ||
this.duration = duration; | ||
this.size = size; | ||
} | ||
} | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
public Long id; | ||
|
||
@JsonIgnore | ||
@ManyToOne(optional = false, fetch = FetchType.EAGER) | ||
public User user; | ||
|
||
@NotEmpty | ||
@NotNull | ||
public String type; | ||
|
||
@NotEmpty | ||
@NotNull | ||
public String token; | ||
|
||
@CreationTimestamp | ||
public Instant created; | ||
|
||
public UserToken() { | ||
// required for Panache | ||
} | ||
|
||
public static UserToken generate(Type type) { | ||
UserToken key = new UserToken(); | ||
key.type = type.name(); | ||
key.token = StringUtils.randomAlphanumeric(type.size); | ||
return key; | ||
} | ||
|
||
public static Uni<UserToken> findByToken(Type type, String token) { | ||
return find("type = ?1 and token = ?2 and created > ?3", type.name(), token, Instant.now().minus(type.duration)).firstResult(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.