|
|
|
@ -3,13 +3,14 @@ package de.srsoftware.umbrella.user; |
|
|
|
|
|
|
|
|
|
|
|
import static de.srsoftware.tools.Optionals.*; |
|
|
|
import static de.srsoftware.tools.Optionals.*; |
|
|
|
import static de.srsoftware.umbrella.core.Constants.*; |
|
|
|
import static de.srsoftware.umbrella.core.Constants.*; |
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.core.Paths.LIST; |
|
|
|
import static de.srsoftware.umbrella.core.Paths.LOGOUT; |
|
|
|
import static de.srsoftware.umbrella.core.Paths.LOGOUT; |
|
|
|
import static de.srsoftware.umbrella.core.ResponseCode.*; |
|
|
|
import static de.srsoftware.umbrella.core.ResponseCode.*; |
|
|
|
import static de.srsoftware.umbrella.user.Constants.*; |
|
|
|
import static de.srsoftware.umbrella.user.Constants.*; |
|
|
|
import static de.srsoftware.umbrella.user.Paths.LOGIN; |
|
|
|
import static de.srsoftware.umbrella.user.Paths.LOGIN; |
|
|
|
import static de.srsoftware.umbrella.user.Paths.WHOAMI; |
|
|
|
import static de.srsoftware.umbrella.user.Paths.WHOAMI; |
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.user.model.DbUser.PERMISSION.LIST_USERS; |
|
|
|
import static de.srsoftware.umbrella.user.model.DbUser.PERMISSION.UPDATE_USERS; |
|
|
|
import static de.srsoftware.umbrella.user.model.DbUser.PERMISSION.UPDATE_USERS; |
|
|
|
import static java.lang.System.Logger.Level.DEBUG; |
|
|
|
|
|
|
|
import static java.lang.System.Logger.Level.WARNING; |
|
|
|
import static java.lang.System.Logger.Level.WARNING; |
|
|
|
import static java.time.temporal.ChronoUnit.DAYS; |
|
|
|
import static java.time.temporal.ChronoUnit.DAYS; |
|
|
|
|
|
|
|
|
|
|
|
@ -24,9 +25,9 @@ import java.io.IOException; |
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
import java.time.Instant; |
|
|
|
import java.time.Instant; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
|
|
|
|
import org.json.JSONObject; |
|
|
|
import org.json.JSONObject; |
|
|
|
import org.sqlite.core.DB; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class UserModule extends PathHandler { |
|
|
|
public class UserModule extends PathHandler { |
|
|
|
@ -63,12 +64,20 @@ public class UserModule extends PathHandler { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public boolean doGet(Path path, HttpExchange ex) throws IOException { |
|
|
|
public boolean doGet(Path path, HttpExchange ex) throws IOException { |
|
|
|
var p = path.toString(); |
|
|
|
UmbrellaUser user = null; |
|
|
|
switch (p){ |
|
|
|
var sessionToken = SessionToken.from(ex).map(Token::of); |
|
|
|
case LOGOUT: return logout(ex); |
|
|
|
if (sessionToken.isPresent()) try { |
|
|
|
case WHOAMI: return getUser(ex); |
|
|
|
user = users.load(users.load(sessionToken.get())); |
|
|
|
|
|
|
|
} catch (UmbrellaException e) { |
|
|
|
|
|
|
|
LOG.log(WARNING,e); |
|
|
|
} |
|
|
|
} |
|
|
|
return super.doGet(path,ex); |
|
|
|
addCors(ex); |
|
|
|
|
|
|
|
return switch (path.toString()) { |
|
|
|
|
|
|
|
case LIST -> getUserList(ex, user); |
|
|
|
|
|
|
|
case LOGOUT -> logout(ex, sessionToken); |
|
|
|
|
|
|
|
case WHOAMI -> getUser(ex, user); |
|
|
|
|
|
|
|
default -> super.doGet(path, ex); |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
@ -127,6 +136,19 @@ public class UserModule extends PathHandler { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean getUserList(HttpExchange ex, UmbrellaUser user) throws IOException { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (user instanceof DbUser dbUser && dbUser.permissions().contains(LIST_USERS)){ |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
var list = users.list(0, null).stream().map(UmbrellaUser::toMap).toList(); |
|
|
|
|
|
|
|
return sendContent(ex,list); |
|
|
|
|
|
|
|
} catch (UmbrellaException e) { |
|
|
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return sendContent(ex,FORBIDDEN,"You are not allowed to list users!"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean patchPassword(HttpExchange ex, UmbrellaUser requestingUser) throws IOException { |
|
|
|
private boolean patchPassword(HttpExchange ex, UmbrellaUser requestingUser) throws IOException { |
|
|
|
if (!(requestingUser instanceof DbUser user)) return sendContent(ex,SERVER_ERROR,"DbUser expected"); |
|
|
|
if (!(requestingUser instanceof DbUser user)) return sendContent(ex,SERVER_ERROR,"DbUser expected"); |
|
|
|
JSONObject json; |
|
|
|
JSONObject json; |
|
|
|
@ -160,22 +182,12 @@ public class UserModule extends PathHandler { |
|
|
|
return super.doPost(path, ex); |
|
|
|
return super.doPost(path, ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean getUser(HttpExchange ex) throws IOException { |
|
|
|
private boolean getUser(HttpExchange ex, UmbrellaUser user) throws IOException { |
|
|
|
addCors(ex); |
|
|
|
if (user != null) return sendContent(ex,OK,user); |
|
|
|
var sessionToken = SessionToken.from(ex); |
|
|
|
return sendEmptyResponse(UNAUTHORIZED,ex); |
|
|
|
if (sessionToken.isEmpty()) return sendEmptyResponse(UNAUTHORIZED,ex); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
Session session = users.load(Token.of(sessionToken.get())); |
|
|
|
|
|
|
|
UmbrellaUser user = users.load(session); |
|
|
|
|
|
|
|
return sendContent(ex,OK,user); |
|
|
|
|
|
|
|
} catch (UmbrellaException e) { |
|
|
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean logout(HttpExchange ex) throws IOException { |
|
|
|
public boolean logout(HttpExchange ex, Optional<Token> optToken) throws IOException { |
|
|
|
addCors(ex); |
|
|
|
|
|
|
|
var optToken = SessionToken.from(ex).map(Token::of); |
|
|
|
|
|
|
|
if (optToken.isPresent()){ |
|
|
|
if (optToken.isPresent()){ |
|
|
|
var token = optToken.get(); |
|
|
|
var token = optToken.get(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
|