From b6391da2a23fb2972f582a2fc3dbf60f0ad5f425 Mon Sep 17 00:00:00 2001 From: LoaD Accumulator Date: Tue, 31 Oct 2023 11:27:05 +0100 Subject: [PATCH] [MOD] Separate things out. --- src/RegToken.c | 52 +++++++++++++++++++++++++++++++++++ src/Routes/RouteAdminTokens.c | 43 ++--------------------------- src/include/RegToken.h | 6 ++++ 3 files changed, 60 insertions(+), 41 deletions(-) diff --git a/src/RegToken.c b/src/RegToken.c index 25fbd6d..1c1fe89 100644 --- a/src/RegToken.c +++ b/src/RegToken.c @@ -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) { diff --git a/src/Routes/RouteAdminTokens.c b/src/Routes/RouteAdminTokens.c index ba372ff..c05a5d4 100644 --- a/src/Routes/RouteAdminTokens.c +++ b/src/Routes/RouteAdminTokens.c @@ -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); diff --git a/src/include/RegToken.h b/src/include/RegToken.h index 42ce0a8..c73ff90 100644 --- a/src/include/RegToken.h +++ b/src/include/RegToken.h @@ -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. */