forked from lda/telodendria
Make JsonValueString() call StrDuplicate(); refactor code to behave properly.
This commit is contained in:
parent
8c96fd8d7d
commit
b60cac53e5
11 changed files with 68 additions and 51 deletions
5
TODO.txt
5
TODO.txt
|
@ -28,8 +28,6 @@ Milestone: v0.2.0
|
||||||
[x] Non-JSON endpoints
|
[x] Non-JSON endpoints
|
||||||
[x] Home page (like Synapse's "it works!")
|
[x] Home page (like Synapse's "it works!")
|
||||||
|
|
||||||
[!] Document HashMapGetKey()
|
|
||||||
- See Refactor below
|
|
||||||
[ ] Document new User functions
|
[ ] Document new User functions
|
||||||
[ ] Document new JSON functions
|
[ ] Document new JSON functions
|
||||||
|
|
||||||
|
@ -39,7 +37,8 @@ Milestone: v0.2.0
|
||||||
done with them.
|
done with them.
|
||||||
[x] Remove HashMapGetKey() function
|
[x] Remove HashMapGetKey() function
|
||||||
[x] HashMap
|
[x] HashMap
|
||||||
[ ] JsonValueString()
|
[x] JsonValueString()
|
||||||
|
[ ] Db
|
||||||
|
|
||||||
Milestone: v0.3.0
|
Milestone: v0.3.0
|
||||||
-----------------
|
-----------------
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
"threads": 2,
|
"threads": 2,
|
||||||
"log": {
|
"log": {
|
||||||
"output": "stdout",
|
"output": "stdout",
|
||||||
"level": "debug",
|
"//level": "debug",
|
||||||
"timestampFormat": "none",
|
"timestampFormat": "none",
|
||||||
"color": true
|
"color": true
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,7 +174,13 @@ JsonValueString(char *string)
|
||||||
}
|
}
|
||||||
|
|
||||||
value->type = JSON_STRING;
|
value->type = JSON_STRING;
|
||||||
value->as.string = string;
|
|
||||||
|
value->as.string = StrDuplicate(string);
|
||||||
|
if (!value->as.string)
|
||||||
|
{
|
||||||
|
Free(value);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
@ -913,6 +919,7 @@ JsonDecodeValue(JsonParserState * state)
|
||||||
}
|
}
|
||||||
strcpy(strValue, state->token);
|
strcpy(strValue, state->token);
|
||||||
value = JsonValueString(strValue);
|
value = JsonValueString(strValue);
|
||||||
|
Free(strValue);
|
||||||
break;
|
break;
|
||||||
case TOKEN_INTEGER:
|
case TOKEN_INTEGER:
|
||||||
value = JsonValueInteger(atol(state->token));
|
value = JsonValueInteger(atol(state->token));
|
||||||
|
|
|
@ -304,8 +304,8 @@ MatrixErrorCreate(MatrixError errorArg)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
HashMapSet(errorObj, "errcode", JsonValueString(StrDuplicate(errcode)));
|
HashMapSet(errorObj, "errcode", JsonValueString(errcode));
|
||||||
HashMapSet(errorObj, "error", JsonValueString(StrDuplicate(error)));
|
HashMapSet(errorObj, "error", JsonValueString(error));
|
||||||
|
|
||||||
return errorObj;
|
return errorObj;
|
||||||
}
|
}
|
||||||
|
@ -378,14 +378,14 @@ MatrixClientWellKnown(char *base, char *identity)
|
||||||
response = HashMapCreate();
|
response = HashMapCreate();
|
||||||
homeserver = HashMapCreate();
|
homeserver = HashMapCreate();
|
||||||
|
|
||||||
HashMapSet(homeserver, "base_url", JsonValueString(StrDuplicate(base)));
|
HashMapSet(homeserver, "base_url", JsonValueString(base));
|
||||||
HashMapSet(response, "m.homeserver", JsonValueObject(homeserver));
|
HashMapSet(response, "m.homeserver", JsonValueObject(homeserver));
|
||||||
|
|
||||||
if (identity)
|
if (identity)
|
||||||
{
|
{
|
||||||
HashMap *identityServer = HashMapCreate();
|
HashMap *identityServer = HashMapCreate();
|
||||||
|
|
||||||
HashMapSet(identityServer, "base_url", JsonValueString(StrDuplicate(identity)));
|
HashMapSet(identityServer, "base_url", JsonValueString(identity));
|
||||||
HashMapSet(response, "m.identity_server", identityServer);
|
HashMapSet(response, "m.identity_server", identityServer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,6 +54,7 @@ ROUTE_IMPL(RouteLogin, args)
|
||||||
|
|
||||||
User *user;
|
User *user;
|
||||||
UserLoginInfo *loginInfo;
|
UserLoginInfo *loginInfo;
|
||||||
|
char *fullUsername;
|
||||||
|
|
||||||
if (MATRIX_PATH_PARTS(args->path) > 0)
|
if (MATRIX_PATH_PARTS(args->path) > 0)
|
||||||
{
|
{
|
||||||
|
@ -68,8 +69,7 @@ ROUTE_IMPL(RouteLogin, args)
|
||||||
enabledFlows = ArrayCreate();
|
enabledFlows = ArrayCreate();
|
||||||
pwdFlow = HashMapCreate();
|
pwdFlow = HashMapCreate();
|
||||||
|
|
||||||
HashMapSet(pwdFlow, "type",
|
HashMapSet(pwdFlow, "type", JsonValueString("m.login.password"));
|
||||||
JsonValueString(StrDuplicate("m.login.password")));
|
|
||||||
ArrayAdd(enabledFlows, JsonValueObject(pwdFlow));
|
ArrayAdd(enabledFlows, JsonValueObject(pwdFlow));
|
||||||
HashMapSet(response, "flows", JsonValueArray(enabledFlows));
|
HashMapSet(response, "flows", JsonValueArray(enabledFlows));
|
||||||
break;
|
break;
|
||||||
|
@ -261,23 +261,18 @@ ROUTE_IMPL(RouteLogin, args)
|
||||||
JsonValueString(loginInfo->refreshToken));
|
JsonValueString(loginInfo->refreshToken));
|
||||||
}
|
}
|
||||||
|
|
||||||
HashMapSet(response, "user_id",
|
fullUsername = StrConcat(4, "@", UserGetName(user), ":",
|
||||||
JsonValueString(
|
args->matrixArgs->config->serverName);
|
||||||
StrConcat(4, "@", UserGetName(user), ":",
|
HashMapSet(response, "user_id", JsonValueString(fullUsername));
|
||||||
args->matrixArgs->config->serverName)));
|
Free(fullUsername);
|
||||||
|
|
||||||
HashMapSet(response, "well_known",
|
HashMapSet(response, "well_known",
|
||||||
JsonValueObject(
|
JsonValueObject(
|
||||||
MatrixClientWellKnown(args->matrixArgs->config->baseUrl,
|
MatrixClientWellKnown(args->matrixArgs->config->baseUrl,
|
||||||
args->matrixArgs->config->identityServer)));
|
args->matrixArgs->config->identityServer)));
|
||||||
|
|
||||||
Free(loginInfo->accessToken->user);
|
UserAccessTokenFree(loginInfo->accessToken);
|
||||||
|
Free(loginInfo->refreshToken);
|
||||||
/*
|
|
||||||
* Don't need to free other members; they're attached to the JSON
|
|
||||||
* response, they will be freed after the response is sent.
|
|
||||||
*/
|
|
||||||
Free(loginInfo->accessToken);
|
|
||||||
Free(loginInfo);
|
Free(loginInfo);
|
||||||
|
|
||||||
UserUnlock(user);
|
UserUnlock(user);
|
||||||
|
|
|
@ -51,7 +51,7 @@ ROUTE_IMPL(RouteMatrix, args)
|
||||||
|
|
||||||
Free(pathPart);
|
Free(pathPart);
|
||||||
|
|
||||||
ArrayAdd(versions, JsonValueString(StrDuplicate("v1.6")));
|
ArrayAdd(versions, JsonValueString("v1.6"));
|
||||||
|
|
||||||
response = HashMapCreate();
|
response = HashMapCreate();
|
||||||
HashMapSet(response, "versions", JsonValueArray(versions));
|
HashMapSet(response, "versions", JsonValueArray(versions));
|
||||||
|
|
|
@ -145,15 +145,15 @@ ROUTE_IMPL(RouteRefresh, args)
|
||||||
DbDelete(db, 3, "tokens", "access", oldAccessToken);
|
DbDelete(db, 3, "tokens", "access", oldAccessToken);
|
||||||
|
|
||||||
/* Update the refresh token to point to the new access token */
|
/* Update the refresh token to point to the new access token */
|
||||||
JsonValueFree(HashMapSet(DbJson(rtRef), "refreshes", JsonValueString(StrDuplicate(newAccessToken->string))));
|
JsonValueFree(HashMapSet(DbJson(rtRef), "refreshes", JsonValueString(newAccessToken->string)));
|
||||||
|
|
||||||
/* Return the new access token and expiration timestamp to the
|
/* Return the new access token and expiration timestamp to the
|
||||||
* client */
|
* client */
|
||||||
response = HashMapCreate();
|
response = HashMapCreate();
|
||||||
HashMapSet(response, "access_token", JsonValueString(StrDuplicate(newAccessToken->string)));
|
HashMapSet(response, "access_token", JsonValueString(newAccessToken->string));
|
||||||
HashMapSet(response, "expires_in_ms", JsonValueInteger(newAccessToken->lifetime));
|
HashMapSet(response, "expires_in_ms", JsonValueInteger(newAccessToken->lifetime));
|
||||||
|
|
||||||
Free(newAccessToken);
|
UserAccessTokenFree(newAccessToken);
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
JsonFree(request);
|
JsonFree(request);
|
||||||
|
|
|
@ -48,6 +48,7 @@ ROUTE_IMPL(RouteRegister, args)
|
||||||
int refreshToken = 0;
|
int refreshToken = 0;
|
||||||
int inhibitLogin = 0;
|
int inhibitLogin = 0;
|
||||||
char *deviceId = NULL;
|
char *deviceId = NULL;
|
||||||
|
char *fullUsername;
|
||||||
|
|
||||||
Db *db = args->matrixArgs->db;
|
Db *db = args->matrixArgs->db;
|
||||||
LogConfig *lc = args->matrixArgs->lc;
|
LogConfig *lc = args->matrixArgs->lc;
|
||||||
|
@ -205,18 +206,13 @@ ROUTE_IMPL(RouteRegister, args)
|
||||||
refreshToken = JsonValueAsBoolean(val);
|
refreshToken = JsonValueAsBoolean(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* These values are already set */
|
|
||||||
(void) refreshToken;
|
|
||||||
(void) inhibitLogin;
|
|
||||||
|
|
||||||
/* These may be NULL */
|
|
||||||
(void) initialDeviceDisplayName;
|
|
||||||
(void) deviceId;
|
|
||||||
|
|
||||||
user = UserCreate(db, username, password);
|
user = UserCreate(db, username, password);
|
||||||
response = HashMapCreate();
|
response = HashMapCreate();
|
||||||
HashMapSet(response, "user_id", JsonValueString(StrConcat(4, "@",
|
|
||||||
UserGetName(user), ":", args->matrixArgs->config->serverName)));
|
fullUsername = StrConcat(4, "@", UserGetName(user), ":", args->matrixArgs->config->serverName);
|
||||||
|
HashMapSet(response, "user_id", JsonValueString(fullUsername));
|
||||||
|
Free(fullUsername);
|
||||||
|
|
||||||
HttpResponseStatus(args->context, HTTP_OK);
|
HttpResponseStatus(args->context, HTTP_OK);
|
||||||
if (!inhibitLogin)
|
if (!inhibitLogin)
|
||||||
{
|
{
|
||||||
|
@ -236,11 +232,8 @@ ROUTE_IMPL(RouteRegister, args)
|
||||||
JsonValueString(loginInfo->refreshToken));
|
JsonValueString(loginInfo->refreshToken));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
UserAccessTokenFree(loginInfo->accessToken);
|
||||||
* Don't need to free members; they're attached to the JSON response,
|
Free(loginInfo->refreshToken);
|
||||||
* they will be freed after the response is sent.
|
|
||||||
*/
|
|
||||||
Free(loginInfo->accessToken);
|
|
||||||
Free(loginInfo);
|
Free(loginInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -89,7 +89,7 @@ BuildFlows(Array * flows)
|
||||||
{
|
{
|
||||||
UiaStage *stage = ArrayGet(stages, i);
|
UiaStage *stage = ArrayGet(stages, i);
|
||||||
|
|
||||||
ArrayAdd(responseStages, JsonValueString(StrDuplicate(stage->type)));
|
ArrayAdd(responseStages, JsonValueString(stage->type));
|
||||||
if (stage->params)
|
if (stage->params)
|
||||||
{
|
{
|
||||||
JsonValueFree(HashMapSet(responseParams, stage->type, JsonValueObject(stage->params)));
|
JsonValueFree(HashMapSet(responseParams, stage->type, JsonValueObject(stage->params)));
|
||||||
|
@ -163,7 +163,7 @@ BuildResponse(Array * flows, char *session, Db * db, HashMap ** response)
|
||||||
{
|
{
|
||||||
char *stage = JsonValueAsString(ArrayGet(dbCompleted, i));
|
char *stage = JsonValueAsString(ArrayGet(dbCompleted, i));
|
||||||
|
|
||||||
ArrayAdd(completed, JsonValueString(StrDuplicate(stage)));
|
ArrayAdd(completed, JsonValueString(stage));
|
||||||
}
|
}
|
||||||
|
|
||||||
HashMapSet(*response, "completed", JsonValueArray(completed));
|
HashMapSet(*response, "completed", JsonValueArray(completed));
|
||||||
|
@ -296,7 +296,10 @@ finish:
|
||||||
{
|
{
|
||||||
UiaStage *stage = ArrayGet(stages, j);
|
UiaStage *stage = ArrayGet(stages, j);
|
||||||
|
|
||||||
Free(stage); /* Members are referenced elsewhere */
|
Free(stage->type);
|
||||||
|
/* stage->params, if not null, is referenced in the
|
||||||
|
* response body. */
|
||||||
|
Free(stage);
|
||||||
}
|
}
|
||||||
ArrayFree(stages);
|
ArrayFree(stages);
|
||||||
}
|
}
|
||||||
|
|
31
src/User.c
31
src/User.c
|
@ -278,7 +278,7 @@ UserLogin(User * user, char *password, char *deviceId, char *deviceDisplayName,
|
||||||
rtRef = DbCreate(user->db, 3, "tokens", "refresh", result->refreshToken);
|
rtRef = DbCreate(user->db, 3, "tokens", "refresh", result->refreshToken);
|
||||||
|
|
||||||
HashMapSet(DbJson(rtRef), "refreshes",
|
HashMapSet(DbJson(rtRef), "refreshes",
|
||||||
JsonValueString(StrDuplicate(result->accessToken->string)));
|
JsonValueString(result->accessToken->string));
|
||||||
DbUnlock(user->db, rtRef);
|
DbUnlock(user->db, rtRef);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,7 +317,7 @@ UserLogin(User * user, char *password, char *deviceId, char *deviceDisplayName,
|
||||||
if (deviceDisplayName)
|
if (deviceDisplayName)
|
||||||
{
|
{
|
||||||
HashMapSet(device, "displayName",
|
HashMapSet(device, "displayName",
|
||||||
JsonValueString(StrDuplicate(deviceDisplayName)));
|
JsonValueString(deviceDisplayName));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -327,11 +327,11 @@ UserLogin(User * user, char *password, char *deviceId, char *deviceDisplayName,
|
||||||
if (result->refreshToken)
|
if (result->refreshToken)
|
||||||
{
|
{
|
||||||
HashMapSet(device, "refreshToken",
|
HashMapSet(device, "refreshToken",
|
||||||
JsonValueString(StrDuplicate(result->refreshToken)));
|
JsonValueString(result->refreshToken));
|
||||||
}
|
}
|
||||||
|
|
||||||
HashMapSet(device, "accessToken",
|
HashMapSet(device, "accessToken",
|
||||||
JsonValueString(StrDuplicate(result->accessToken->string)));
|
JsonValueString(result->accessToken->string));
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -400,11 +400,14 @@ UserSetPassword(User * user, char *password)
|
||||||
salt = StrRandom(16);
|
salt = StrRandom(16);
|
||||||
tmpstr = StrConcat(2, password, salt);
|
tmpstr = StrConcat(2, password, salt);
|
||||||
hash = Sha256(tmpstr);
|
hash = Sha256(tmpstr);
|
||||||
Free(tmpstr);
|
|
||||||
|
|
||||||
JsonValueFree(HashMapSet(json, "salt", JsonValueString(salt)));
|
JsonValueFree(HashMapSet(json, "salt", JsonValueString(salt)));
|
||||||
JsonValueFree(HashMapSet(json, "password", JsonValueString(hash)));
|
JsonValueFree(HashMapSet(json, "password", JsonValueString(hash)));
|
||||||
|
|
||||||
|
Free(salt);
|
||||||
|
Free(hash);
|
||||||
|
Free(tmpstr);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -493,8 +496,8 @@ UserAccessTokenSave(Db * db, UserAccessToken * token)
|
||||||
|
|
||||||
json = DbJson(ref);
|
json = DbJson(ref);
|
||||||
|
|
||||||
HashMapSet(json, "user", JsonValueString(StrDuplicate(token->user)));
|
HashMapSet(json, "user", JsonValueString(token->user));
|
||||||
HashMapSet(json, "device", JsonValueString(StrDuplicate(token->deviceId)));
|
HashMapSet(json, "device", JsonValueString(token->deviceId));
|
||||||
|
|
||||||
if (token->lifetime)
|
if (token->lifetime)
|
||||||
{
|
{
|
||||||
|
@ -504,6 +507,20 @@ UserAccessTokenSave(Db * db, UserAccessToken * token)
|
||||||
return DbUnlock(db, ref);
|
return DbUnlock(db, ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
UserAccessTokenFree(UserAccessToken * token)
|
||||||
|
{
|
||||||
|
if (!token)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Free(token->user);
|
||||||
|
Free(token->string);
|
||||||
|
Free(token->deviceId);
|
||||||
|
Free(token);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
UserDeleteToken(User * user, char *token)
|
UserDeleteToken(User * user, char *token)
|
||||||
{
|
{
|
||||||
|
|
|
@ -87,6 +87,9 @@ extern UserAccessToken *
|
||||||
extern int
|
extern int
|
||||||
UserAccessTokenSave(Db *, UserAccessToken *);
|
UserAccessTokenSave(Db *, UserAccessToken *);
|
||||||
|
|
||||||
|
extern void
|
||||||
|
UserAccessTokenFree(UserAccessToken *);
|
||||||
|
|
||||||
extern int
|
extern int
|
||||||
UserDeleteToken(User *, char *);
|
UserDeleteToken(User *, char *);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue