forked from Telodendria/Telodendria
Begin prototyping User.h
This commit is contained in:
parent
0f661f435f
commit
08b36c071c
8 changed files with 203 additions and 74 deletions
3
TODO.txt
3
TODO.txt
|
@ -23,8 +23,11 @@ Milestone: v0.2.0
|
||||||
[ ] User API
|
[ ] User API
|
||||||
|
|
||||||
[ ] Document UserInteractiveAuth (move docs from Matrix)
|
[ ] Document UserInteractiveAuth (move docs from Matrix)
|
||||||
|
[ ] Document User
|
||||||
|
[ ] Move docs from Matrix to User for UserValidate
|
||||||
[ ] Document MemoryHexDump
|
[ ] Document MemoryHexDump
|
||||||
[ ] Document String and remove old functions from Util
|
[ ] Document String and remove old functions from Util
|
||||||
|
[ ] Document DbExists
|
||||||
|
|
||||||
Milestone: v1.0.0
|
Milestone: v1.0.0
|
||||||
-----------------
|
-----------------
|
||||||
|
|
22
src/Db.c
22
src/Db.c
|
@ -724,6 +724,28 @@ DbUnlock(Db * db, DbRef * ref)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
DbExists(Db * db, size_t nArgs,...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
Array *args;
|
||||||
|
char *file;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
va_start(ap, nArgs);
|
||||||
|
args = ArrayFromVarArgs(nArgs, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
file = DbFileName(db, args);
|
||||||
|
|
||||||
|
ret = UtilLastModified(file);
|
||||||
|
|
||||||
|
Free(file);
|
||||||
|
ArrayFree(args);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
HashMap *
|
HashMap *
|
||||||
DbJson(DbRef * ref)
|
DbJson(DbRef * ref)
|
||||||
{
|
{
|
||||||
|
|
54
src/Matrix.c
54
src/Matrix.c
|
@ -358,57 +358,3 @@ MatrixRateLimit(HttpServerContext * context, Db * db)
|
||||||
(void) db;
|
(void) db;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
MatrixUserValidate(char *localpart, char *domain)
|
|
||||||
{
|
|
||||||
size_t maxLen = 255 - strlen(domain) - 1;
|
|
||||||
size_t i = 0;
|
|
||||||
|
|
||||||
while (localpart[i])
|
|
||||||
{
|
|
||||||
char c = localpart[i];
|
|
||||||
|
|
||||||
if (i > maxLen)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
|
|
||||||
(c == '.') || (c == '_') || (c == '=') || (c == '-') ||
|
|
||||||
(c == '/')))
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
MatrixHistoricalUserValidate(char *localpart, char *domain)
|
|
||||||
{
|
|
||||||
size_t maxLen = 255 - strlen(domain) - 1;
|
|
||||||
size_t i = 0;
|
|
||||||
|
|
||||||
while (localpart[i])
|
|
||||||
{
|
|
||||||
char c = localpart[i];
|
|
||||||
|
|
||||||
if (i > maxLen)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(c >= 0x21 && c <= 0x39) || (c >= 0x3B && c <= 0x7E))
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
|
@ -29,6 +29,8 @@
|
||||||
#include <HashMap.h>
|
#include <HashMap.h>
|
||||||
#include <String.h>
|
#include <String.h>
|
||||||
#include <Memory.h>
|
#include <Memory.h>
|
||||||
|
|
||||||
|
#include <User.h>
|
||||||
#include <UserInteractiveAuth.h>
|
#include <UserInteractiveAuth.h>
|
||||||
|
|
||||||
ROUTE_IMPL(RouteRegister, args)
|
ROUTE_IMPL(RouteRegister, args)
|
||||||
|
@ -49,6 +51,8 @@ ROUTE_IMPL(RouteRegister, args)
|
||||||
int inhibitLogin = 0;
|
int inhibitLogin = 0;
|
||||||
char *deviceId = NULL;
|
char *deviceId = NULL;
|
||||||
|
|
||||||
|
Db *db = args->matrixArgs->db;
|
||||||
|
|
||||||
if (MATRIX_PATH_PARTS(args->path) == 0)
|
if (MATRIX_PATH_PARTS(args->path) == 0)
|
||||||
{
|
{
|
||||||
if (HttpRequestMethodGet(args->context) != HTTP_POST)
|
if (HttpRequestMethodGet(args->context) != HTTP_POST)
|
||||||
|
@ -82,15 +86,19 @@ ROUTE_IMPL(RouteRegister, args)
|
||||||
}
|
}
|
||||||
username = StringDuplicate(JsonValueAsString(val));
|
username = StringDuplicate(JsonValueAsString(val));
|
||||||
|
|
||||||
if (!MatrixUserValidate(username, args->matrixArgs->config->serverName))
|
if (!UserValidate(username, args->matrixArgs->config->serverName))
|
||||||
{
|
{
|
||||||
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
||||||
response = MatrixErrorCreate(M_INVALID_USERNAME);
|
response = MatrixErrorCreate(M_INVALID_USERNAME);
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: Check if username exists and throw error if it
|
if (UserExists(db, username))
|
||||||
* does */
|
{
|
||||||
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
||||||
|
response = MatrixErrorCreate(M_USER_IN_USE);
|
||||||
|
goto finish;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
response = UserInteractiveAuth(args->context,
|
response = UserInteractiveAuth(args->context,
|
||||||
|
@ -181,29 +189,29 @@ ROUTE_IMPL(RouteRegister, args)
|
||||||
refreshToken = JsonValueAsBoolean(val);
|
refreshToken = JsonValueAsBoolean(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!username)
|
|
||||||
{
|
|
||||||
username = StringRandom(16);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!inhibitLogin && !deviceId)
|
|
||||||
{
|
|
||||||
deviceId = StringRandom(10);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* These values are already set */
|
/* These values are already set */
|
||||||
(void) password;
|
(void) password;
|
||||||
(void) refreshToken;
|
(void) refreshToken;
|
||||||
(void) inhibitLogin;
|
(void) inhibitLogin;
|
||||||
(void) username;
|
|
||||||
|
|
||||||
/* These may be NULL */
|
/* These may be NULL */
|
||||||
(void) initialDeviceDisplayName;
|
(void) initialDeviceDisplayName;
|
||||||
|
(void) username;
|
||||||
(void) deviceId;
|
(void) deviceId;
|
||||||
|
|
||||||
/* TODO: Register new user here */
|
/* TODO: Register new user here */
|
||||||
|
|
||||||
|
if (!inhibitLogin)
|
||||||
|
{
|
||||||
|
/* TODO: Log in user here and attach auth info to response */
|
||||||
|
}
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
|
Free(username);
|
||||||
|
Free(password);
|
||||||
|
Free(deviceId);
|
||||||
|
Free(initialDeviceDisplayName);
|
||||||
|
|
||||||
JsonFree(request);
|
JsonFree(request);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -221,8 +229,21 @@ finish:
|
||||||
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
||||||
response = MatrixErrorCreate(M_MISSING_PARAM);
|
response = MatrixErrorCreate(M_MISSING_PARAM);
|
||||||
}
|
}
|
||||||
|
else if (!UserValidate(username, args->matrixArgs->config->serverName))
|
||||||
/* TODO: Check if username is available */
|
{
|
||||||
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
||||||
|
response = MatrixErrorCreate(M_INVALID_USERNAME);
|
||||||
|
}
|
||||||
|
else if (UserExists(db, username))
|
||||||
|
{
|
||||||
|
response = HashMapCreate();
|
||||||
|
HashMapSet(response, "available", JsonValueBoolean(1));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
HttpResponseStatus(args->context, HTTP_BAD_REQUEST);
|
||||||
|
response = MatrixErrorCreate(M_USER_IN_USE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (HttpRequestMethodGet(args->context) == HTTP_POST &&
|
else if (HttpRequestMethodGet(args->context) == HTTP_POST &&
|
||||||
(MATRIX_PATH_EQUALS(pathPart, "email") ||
|
(MATRIX_PATH_EQUALS(pathPart, "email") ||
|
||||||
|
|
86
src/User.c
Normal file
86
src/User.c
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person
|
||||||
|
* obtaining a copy of this software and associated documentation files
|
||||||
|
* (the "Software"), to deal in the Software without restriction,
|
||||||
|
* including without limitation the rights to use, copy, modify, merge,
|
||||||
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||||
|
* and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
#include <User.h>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
UserValidate(char *localpart, char *domain)
|
||||||
|
{
|
||||||
|
size_t maxLen = 255 - strlen(domain) - 1;
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
while (localpart[i])
|
||||||
|
{
|
||||||
|
char c = localpart[i];
|
||||||
|
|
||||||
|
if (i > maxLen)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
|
||||||
|
(c == '.') || (c == '_') || (c == '=') || (c == '-') ||
|
||||||
|
(c == '/')))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
UserHistoricalValidate(char *localpart, char *domain)
|
||||||
|
{
|
||||||
|
size_t maxLen = 255 - strlen(domain) - 1;
|
||||||
|
size_t i = 0;
|
||||||
|
|
||||||
|
while (localpart[i])
|
||||||
|
{
|
||||||
|
char c = localpart[i];
|
||||||
|
|
||||||
|
if (i > maxLen)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(c >= 0x21 && c <= 0x39) || (c >= 0x3B && c <= 0x7E))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
UserExists(Db * db, char *name)
|
||||||
|
{
|
||||||
|
return DbExists(db, 2, "users", name);
|
||||||
|
}
|
|
@ -53,6 +53,9 @@ extern DbRef *
|
||||||
extern int
|
extern int
|
||||||
DbUnlock(Db *, DbRef *);
|
DbUnlock(Db *, DbRef *);
|
||||||
|
|
||||||
|
extern int
|
||||||
|
DbExists(Db *, size_t,...);
|
||||||
|
|
||||||
extern HashMap *
|
extern HashMap *
|
||||||
DbJson(DbRef *);
|
DbJson(DbRef *);
|
||||||
|
|
||||||
|
|
|
@ -86,10 +86,6 @@ extern HashMap *
|
||||||
extern HashMap *
|
extern HashMap *
|
||||||
MatrixRateLimit(HttpServerContext *, Db *);
|
MatrixRateLimit(HttpServerContext *, Db *);
|
||||||
|
|
||||||
extern int
|
|
||||||
MatrixUserValidate(char *, char *);
|
|
||||||
|
|
||||||
extern int
|
|
||||||
MatrixHistoricalUserValidate(char *, char *);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
52
src/include/User.h
Normal file
52
src/include/User.h
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person
|
||||||
|
* obtaining a copy of this software and associated documentation files
|
||||||
|
* (the "Software"), to deal in the Software without restriction,
|
||||||
|
* including without limitation the rights to use, copy, modify, merge,
|
||||||
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
||||||
|
* and to permit persons to whom the Software is furnished to do so,
|
||||||
|
* subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef TELODENDRIA_USER_H
|
||||||
|
#define TELODENDRIA_USER_H
|
||||||
|
|
||||||
|
#include <Db.h>
|
||||||
|
|
||||||
|
typedef struct User User;
|
||||||
|
|
||||||
|
extern int
|
||||||
|
UserValidate(char *, char *);
|
||||||
|
|
||||||
|
extern int
|
||||||
|
UserHistoricalValidate(char *, char *);
|
||||||
|
|
||||||
|
extern int
|
||||||
|
UserExists(Db *, char *name);
|
||||||
|
|
||||||
|
extern User *
|
||||||
|
UserCreate(Db *, char *name, char *password);
|
||||||
|
|
||||||
|
extern User *
|
||||||
|
UserLock(Db *, char *name);
|
||||||
|
|
||||||
|
extern int
|
||||||
|
UserUnlock(User *);
|
||||||
|
|
||||||
|
extern void
|
||||||
|
UserLogin(User *, char *name, char *password, char *deviceId, char *deviceDisplayName);
|
||||||
|
|
||||||
|
#endif /* TELODENDRIA_USER_H */
|
Loading…
Reference in a new issue