[MOD] Separate things out.

This commit is contained in:
lda 2023-10-31 11:27:05 +01:00
parent c8b529d94b
commit b6391da2a2
Signed by: lda
GPG key ID: 6898757653ABE3E6
3 changed files with 60 additions and 41 deletions

View file

@ -198,6 +198,58 @@ RegTokenVerify(char *token)
return 1;
}
HashMap *
RegTokenJSON(RegTokenInfo * info)
{
char *creator;
char *tokenname;
UInt64 created;
UInt64 expires;
Int64 used;
Int64 uses;
Int64 remaining;
HashMap *jsoninfo = HashMapCreate();
tokenname = info->name;
created = info->created;
expires = info->expires;
uses = info->uses;
used = info->used;
remaining = Int64Sub(uses, used);
if (Int64Eq(uses, Int64Neg(Int64Create(0, 1))))
{
/* If uses == -1(infinite uses), just set it too
* to -1 */
remaining = uses;
}
if (!(creator = info->owner))
{
/* The owner can be null if Telodendria created it.
* Since users can't contain a space, it is in this case set to
* "Telodendria Server". */
creator = "Telodendria Server";
}
HashMapSet(jsoninfo, "name", JsonValueString(tokenname));
HashMapSet(jsoninfo, "created_by", JsonValueString(creator));
HashMapSet(jsoninfo, "created_on", JsonValueInteger(created));
HashMapSet(jsoninfo, "expires_on", JsonValueInteger(expires));
HashMapSet(jsoninfo, "used", JsonValueInteger(used));
/* #26 says the following:
* "The number of uses *remaining* for the token [...]"
* You therefore can't easily set the uses value here, hence why we
* are using `remaining' */
HashMapSet(jsoninfo, "uses", JsonValueInteger(remaining));
return jsoninfo;
}
RegTokenInfo *
RegTokenCreate(Db * db, char *name, char *owner, UInt64 expires, Int64 uses, int privileges)
{

View file

@ -90,49 +90,10 @@ ROUTE_IMPL(RouteAdminTokens, path, argp)
/* TODO: Move this inside it's own `RegTokenJSON`
* function */
char *tokenname = ArrayGet(tokens, i);
char *creator;
UInt64 created;
UInt64 expires;
Int64 used;
Int64 uses;
Int64 remaining;
RegTokenInfo *info = RegTokenGetInfo(db, tokenname);
HashMap *jsoninfo = HashMapCreate();
created = info->created;
expires = info->expires;
uses = info->uses;
used = info->used;
remaining = Int64Sub(uses, used);
if (Int64Eq(uses, Int64Neg(Int64Create(0, 1))))
{
/* If uses == -1(infinite uses), just set it too
* to -1 */
remaining = uses;
}
if (!(creator = info->owner))
{
/* The owner can be null if Telodendria created it.
* Since users can't contain a space, it is in this
* case set to "Telodendria Server". */
creator = "Telodendria Server";
}
HashMapSet(jsoninfo, "name", JsonValueString(tokenname));
HashMapSet(jsoninfo, "created_by", JsonValueString(creator));
HashMapSet(jsoninfo, "created_on", JsonValueInteger(created));
HashMapSet(jsoninfo, "expires_on", JsonValueInteger(expires));
HashMapSet(jsoninfo, "used", JsonValueInteger(used));
/* #26 says the following:
* "The number of uses *remaining* for the token [...]"
* You therefore can't easily set the uses value here. */
HashMapSet(jsoninfo, "uses", JsonValueInteger(remaining));
HashMap *jsoninfo = RegTokenJSON(info);
RegTokenClose(info);
RegTokenFree(info);

View file

@ -106,6 +106,12 @@ extern void RegTokenUse(RegTokenInfo *);
*/
extern int RegTokenExists(Db *, char *);
/**
* Returns a JSON object corresponding to a valid TokenInfo (see
* #26)
*/
extern HashMap * RegTokenJSON(RegTokenInfo *);
/**
* Delete the specified registration token from the database.
*/