Attach device ID to authenticated user.

Now RouteWhoAmI can use UserAuthenticate just like the other endpoints.
This commit is contained in:
Jordan Bancino 2023-05-11 03:03:40 +00:00
parent c1c57fd4cf
commit 4d9c907b58
4 changed files with 31 additions and 19 deletions

View file

@ -22,12 +22,12 @@ Milestone: v0.3.0
[ ] Debug OpenSSL [ ] Debug OpenSSL
[~] Client-Server API [~] Client-Server API
[~] 4: Account management [x] 4: Account management
[x] Deactivate [x] Deactivate
[x] Make sure UserLogin() fails if user is deactivated. [x] Make sure UserLogin() fails if user is deactivated.
[~] Whoami [x] Whoami
[ ] Attach device id to user object [x] Attach device id to user object
[ ] Use UserAuthenticate() [x] Use UserAuthenticate()
[~] 9: User Data [~] 9: User Data
[ ] 10: Security (Rate Limiting) [ ] 10: Security (Rate Limiting)

View file

@ -37,9 +37,7 @@ ROUTE_IMPL(RouteWhoami, path, argp)
Db *db = args->matrixArgs->db; Db *db = args->matrixArgs->db;
HashMap *response = NULL; HashMap *response = NULL;
HashMap *tokenJson = NULL; User *user = NULL;
DbRef *ref;
char *token; char *token;
char *userID; char *userID;
@ -65,25 +63,20 @@ ROUTE_IMPL(RouteWhoami, path, argp)
} }
/* Authenticate with our token */ /* Authenticate with our token */
if (!DbExists(db, 3, "tokens", "access", token)) user = UserAuthenticate(db, token);
if (!user)
{ {
HttpResponseStatus(args->context, HTTP_UNAUTHORIZED); HttpResponseStatus(args->context, HTTP_UNAUTHORIZED);
response = MatrixErrorCreate(M_UNKNOWN_TOKEN); response = MatrixErrorCreate(M_UNKNOWN_TOKEN);
goto finish; goto finish;
} }
ref = DbLock(db, 3, "tokens", "access", token);
tokenJson = DbJson(ref);
response = HashMapCreate(); response = HashMapCreate();
userID = StrConcat(4, "@", userID = StrConcat(4, "@", UserGetName(user), ":", config->serverName);
JsonValueAsString(HashMapGet(tokenJson, "user")), deviceID = StrDuplicate(UserGetDeviceId(user));
":", config->serverName);
deviceID = StrDuplicate(JsonValueAsString(HashMapGet(tokenJson, "device"))); UserUnlock(user);
DbUnlock(db, ref);
HashMapSet(response, "device_id", JsonValueString(deviceID)); HashMapSet(response, "device_id", JsonValueString(deviceID));
HashMapSet(response, "user_id", JsonValueString(userID)); HashMapSet(response, "user_id", JsonValueString(userID));

View file

@ -36,6 +36,7 @@ struct User
DbRef *ref; DbRef *ref;
char *name; char *name;
char *deviceId;
}; };
int int
@ -114,6 +115,7 @@ UserLock(Db * db, char *name)
user->db = db; user->db = db;
user->ref = ref; user->ref = ref;
user->name = StrDuplicate(name); user->name = StrDuplicate(name);
user->deviceId = NULL;
return user; return user;
} }
@ -157,8 +159,7 @@ UserAuthenticate(Db * db, char *accessToken)
return NULL; return NULL;
} }
/* TODO: Attach deviceId to User */ user->deviceId = StrDuplicate(deviceId);
(void) deviceId;
DbUnlock(db, atRef); DbUnlock(db, atRef);
return user; return user;
@ -175,6 +176,7 @@ UserUnlock(User * user)
} }
Free(user->name); Free(user->name);
Free(user->deviceId);
ret = DbUnlock(user->db, user->ref); ret = DbUnlock(user->db, user->ref);
Free(user); Free(user);
@ -342,6 +344,12 @@ UserGetName(User * user)
return user ? user->name : NULL; return user ? user->name : NULL;
} }
char *
UserGetDeviceId(User * user)
{
return user ? user->deviceId : NULL;
}
int int
UserCheckPassword(User * user, char *password) UserCheckPassword(User * user, char *password)
{ {

View file

@ -168,6 +168,17 @@ extern UserLoginInfo * UserLogin(User *, char *, char *, char *, int);
*/ */
extern char * UserGetName(User *); extern char * UserGetName(User *);
/**
* Get the device ID attached to a user object, or NULL if the user
* reference was not obtained using
* .Fn UserAuthenticate .
* If
* .Fn UserLogin
* is used, the return value will have the device ID in it, but the
* device ID is not set on the user reference.
*/
extern char * UserGetDeviceId(User *);
/** /**
* Take a password and verify it against a user object. Telodendria * Take a password and verify it against a user object. Telodendria
* does not store passwords in plain text, so this function hashes the * does not store passwords in plain text, so this function hashes the