[MOD] Use j2s to generate TokenInfo-s instead of hardcode

This commit is contained in:
lda 2023-11-01 21:11:49 +01:00
parent cdc056f9e9
commit 6edacc8b32
Signed by: lda
GPG key ID: 6898757653ABE3E6
4 changed files with 40 additions and 48 deletions

25
Schema/AdminToken.json Normal file
View file

@ -0,0 +1,25 @@
{
"header": "Schema\/AdminToken.h",
"types": {
"TokenRequest": {
"fields": {
"name": { "type": "string" },
"max_uses": { "type": "integer" },
"lifetime": { "type": "integer" }
},
"type": "struct"
},
"TokenInfo": {
"fields": {
"name": { "type": "string" },
"created_by": { "type": "string" },
"created_on": { "type": "integer" },
"expires_on": { "type": "integer" },
"used": { "type": "integer" },
"uses": { "type": "integer" }
},
"type": "struct"
}
},
"guard": "TELODENDRIA_ADMINTOKEN_H"
}

View file

@ -1,14 +0,0 @@
{
"header": "Schema\/TokenRequest.h",
"types": {
"TokenRequest": {
"fields": {
"name": { "type": "string" },
"max_uses": { "type": "integer" },
"lifetime": { "type": "integer" }
},
"type": "struct"
}
},
"guard": "TELODENDRIA_TOKENREQUEST_H"
}

View file

@ -33,6 +33,8 @@
#include <User.h> #include <User.h>
#include <Cytoplasm/Int64.h> #include <Cytoplasm/Int64.h>
#include <Schema/AdminToken.h>
int int
RegTokenValid(RegTokenInfo * token) RegTokenValid(RegTokenInfo * token)
{ {
@ -201,53 +203,32 @@ RegTokenVerify(char *token)
HashMap * HashMap *
RegTokenJSON(RegTokenInfo * info) RegTokenJSON(RegTokenInfo * info)
{ {
char *creator; TokenInfo tokinfo;
char *tokenname;
UInt64 created;
UInt64 expires;
Int64 used; tokinfo.name = info->name;
Int64 uses; tokinfo.created_on = info->created;
tokinfo.expires_on = info->expires;
Int64 remaining; tokinfo.uses = info->uses;
tokinfo.used = info->used;
HashMap *jsoninfo = HashMapCreate(); tokinfo.uses = Int64Sub(info->uses, info->used);
if (Int64Eq(info->uses, Int64Neg(Int64Create(0, 1))))
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 /* If uses == -1(infinite uses), just set it too
* to -1 */ * to -1 */
remaining = uses; tokinfo.uses = info->uses;
} }
if (!(creator = info->owner)) if (!(tokinfo.created_by = info->owner))
{ {
/* The owner can be null if Telodendria created it. /* The owner can be null if Telodendria created it.
* Since users can't contain a space, it is in this case set to * Since users can't contain a space, it is in this case set to
* "Telodendria Server". */ * "Telodendria Server". */
creator = "Telodendria Server"; tokinfo.created_by = "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)); return TokenInfoToJson(&tokinfo);
/* #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 * RegTokenInfo *

View file

@ -28,7 +28,7 @@
#include <Cytoplasm/Str.h> #include <Cytoplasm/Str.h>
#include <Cytoplasm/Memory.h> #include <Cytoplasm/Memory.h>
#include <Schema/TokenRequest.h> #include <Schema/AdminToken.h>
#include <RegToken.h> #include <RegToken.h>
#include <User.h> #include <User.h>