|
|
|
|
@@ -20,12 +20,13 @@ import static java.net.HttpURLConnection.*;
|
|
|
|
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
|
|
|
import static java.text.MessageFormat.format;
|
|
|
|
|
import static java.time.temporal.ChronoUnit.DAYS;
|
|
|
|
|
import static javax.security.auth.callback.ConfirmationCallback.OK;
|
|
|
|
|
|
|
|
|
|
import com.sun.net.httpserver.HttpExchange;
|
|
|
|
|
import de.srsoftware.tools.Path;
|
|
|
|
|
import de.srsoftware.tools.PathHandler;
|
|
|
|
|
import de.srsoftware.tools.SessionToken;
|
|
|
|
|
import de.srsoftware.umbrella.core.BaseHandler;
|
|
|
|
|
import de.srsoftware.umbrella.core.Token;
|
|
|
|
|
import de.srsoftware.umbrella.core.UmbrellaException;
|
|
|
|
|
import de.srsoftware.umbrella.user.api.LoginServiceDb;
|
|
|
|
|
import de.srsoftware.umbrella.user.api.UserDb;
|
|
|
|
|
@@ -45,7 +46,7 @@ import org.jose4j.keys.resolvers.HttpsJwksVerificationKeyResolver;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class UserModule extends PathHandler {
|
|
|
|
|
public class UserModule extends BaseHandler {
|
|
|
|
|
|
|
|
|
|
private record State(LoginService loginService, JSONObject config){
|
|
|
|
|
public static State of(LoginService loginService, JSONObject config) {
|
|
|
|
|
@@ -102,7 +103,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
logins.delete(serviceName);
|
|
|
|
|
return sendEmptyResponse(HTTP_OK,ex);
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -171,7 +172,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
}
|
|
|
|
|
return sendContent(ex,users.load(userId));
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
} catch (NumberFormatException ignored) {}
|
|
|
|
|
return super.doGet(path, ex);
|
|
|
|
|
|
|
|
|
|
@@ -193,7 +194,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
Session session = users.load(Token.of(sessionToken.get()));
|
|
|
|
|
requestingUser = users.load(session);
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var head = path.pop();
|
|
|
|
|
@@ -211,7 +212,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
try {
|
|
|
|
|
editedUser = (DbUser) users.load(userId);
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (requestingUser.id() != userId && (!(requestingUser instanceof DbUser dbUser) || !dbUser.permissions().contains(UPDATE_USERS))){
|
|
|
|
|
@@ -230,7 +231,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
try {
|
|
|
|
|
return update(ex, editedUser,json);
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -320,7 +321,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
var connections = logins.listAssignments(user.id()).stream().map(ForeignLogin::toMap);
|
|
|
|
|
return sendContent(ex,connections);
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@@ -342,7 +343,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
try {
|
|
|
|
|
return sendContent(ex,logins.loadLoginService(serviceId).toMap());
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
return sendContent(ex,HTTP_SERVER_ERROR,e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
@@ -391,7 +392,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
var services = logins.listLoginServices().stream().map(LoginService::name);
|
|
|
|
|
return sendContent(ex,services);
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
return sendContent(ex,HTTP_SERVER_ERROR,e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
@@ -403,7 +404,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
var services = logins.listLoginServices().stream().map(LoginService::toMap);
|
|
|
|
|
return sendContent(ex,services);
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
return sendContent(ex,HTTP_SERVER_ERROR,e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
@@ -420,7 +421,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
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 send(ex,e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -435,7 +436,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
users.getSession(targetUser).cookie().addTo(ex);
|
|
|
|
|
return sendContent(ex,targetUser.toMap());
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -480,7 +481,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
var updated = users.save(new DbUser(user.id(), user.name(), user.email(), pass, user.theme(), user.language(), user.permissions(), null));
|
|
|
|
|
return sendContent(ex, updated);
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -495,7 +496,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
var service = logins.save(new LoginService(name,url,clientId,secret, DEFAULT_FIELD));
|
|
|
|
|
return sendContent(ex,service.toMap());
|
|
|
|
|
} catch (UmbrellaException e) {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -509,8 +510,8 @@ public class UserModule extends PathHandler {
|
|
|
|
|
var user = users.load(username, hashedPass);
|
|
|
|
|
users.getSession(user).cookie().addTo(ex);
|
|
|
|
|
return sendContent(ex,user);
|
|
|
|
|
} catch (UmbrellaException ue){
|
|
|
|
|
return sendContent(ex,ue.statusCode(),ue.getMessage());
|
|
|
|
|
} catch (UmbrellaException e){
|
|
|
|
|
return send(ex,e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -526,9 +527,7 @@ public class UserModule extends PathHandler {
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean send(HttpExchange ex, UmbrellaException e) throws IOException {
|
|
|
|
|
return sendContent(ex,e.statusCode(),e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean update(HttpExchange ex, DbUser user, JSONObject json) throws UmbrellaException, IOException {
|
|
|
|
|
var id = user.id();
|
|
|
|
|
|