|
| 1 | +/* bzflag |
| 2 | + * Copyright (c) 1993-2024 Tim Riker |
| 3 | + * |
| 4 | + * This package is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the license found in the file |
| 6 | + * named COPYING that should have accompanied this file. |
| 7 | + * |
| 8 | + * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 9 | + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 10 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + */ |
| 12 | + |
| 13 | +/* interface header */ |
| 14 | +#include "ServerAuth.h" |
| 15 | + |
| 16 | +/* system headers */ |
| 17 | +#include <cstring> |
| 18 | + |
| 19 | +/* common implementation headers */ |
| 20 | +#include "TextUtils.h" |
| 21 | +#include "ErrorHandler.h" |
| 22 | + |
| 23 | +ServerAuth::ServerAuth() |
| 24 | +{ |
| 25 | +} |
| 26 | + |
| 27 | +ServerAuth::~ServerAuth() |
| 28 | +{ |
| 29 | +} |
| 30 | + |
| 31 | +void ServerAuth::requestToken(StartupInfo *info) |
| 32 | +{ |
| 33 | + startupInfo = info; |
| 34 | + |
| 35 | + std::string url = info->listServerURL; |
| 36 | + |
| 37 | + std::string msg = "action=GETTOKEN&callsign="; |
| 38 | + msg += TextUtils::url_encode(info->callsign); |
| 39 | + msg += "&password="; |
| 40 | + msg += TextUtils::url_encode(info->password); |
| 41 | + if (info->serverName[0] != '\0') |
| 42 | + { |
| 43 | + msg += "&nameport="; |
| 44 | + msg += info->serverName; |
| 45 | + msg += ':'; |
| 46 | + msg += std::to_string(info->serverPort); |
| 47 | + } |
| 48 | + setPostMode(msg); |
| 49 | + setURL(url); |
| 50 | + addHandle(); |
| 51 | +} |
| 52 | + |
| 53 | +void ServerAuth::finalization(char *_data, unsigned int length, bool good) |
| 54 | +{ |
| 55 | + if (good) |
| 56 | + { |
| 57 | + char *base = (char *)_data; |
| 58 | + char *endS = base + length; |
| 59 | + const char tokenIdentifier[] = "TOKEN: "; |
| 60 | + const char noTokenIdentifier[] = "NOTOK: "; |
| 61 | + const char errorIdentifier[] = "ERROR: "; |
| 62 | + const char noticeIdentifier[] = "NOTICE: "; |
| 63 | + |
| 64 | + while (base < endS) |
| 65 | + { |
| 66 | + // find next newline |
| 67 | + char* scan = base; |
| 68 | + while (scan < endS && *scan != '\n') |
| 69 | + scan++; |
| 70 | + |
| 71 | + // if no newline then no more complete replies |
| 72 | + if (scan >= endS) |
| 73 | + break; |
| 74 | + *scan++ = '\0'; |
| 75 | + |
| 76 | + // look for TOKEN: and save token if found also look for NOTOK: |
| 77 | + // and record "badtoken" into the token string and print an |
| 78 | + // error |
| 79 | + if (strncmp(base, tokenIdentifier, strlen(tokenIdentifier)) == 0) |
| 80 | + { |
| 81 | + strncpy(startupInfo->token, (char *)(base + strlen(tokenIdentifier)), |
| 82 | + TokenLen - 1); |
| 83 | + startupInfo->token[TokenLen - 1] = '\0'; |
| 84 | +#ifdef DEBUG |
| 85 | + std::vector<std::string> args; |
| 86 | + args.push_back(startupInfo->token); |
| 87 | + printError("got token: {1}", &args); |
| 88 | +#endif |
| 89 | + } |
| 90 | + else if (!strncmp(base, noTokenIdentifier, |
| 91 | + strlen(noTokenIdentifier))) |
| 92 | + { |
| 93 | + printError("ERROR: did not get token:"); |
| 94 | + printError(base); |
| 95 | + strcpy(startupInfo->token, "badtoken\0"); |
| 96 | + } |
| 97 | + else if (!strncmp(base, errorIdentifier, strlen(errorIdentifier))) |
| 98 | + { |
| 99 | + printError(base); |
| 100 | + strcpy(startupInfo->token, "badtoken\0"); |
| 101 | + } |
| 102 | + else if (!strncmp(base, noticeIdentifier, strlen(noticeIdentifier))) |
| 103 | + printError(base); |
| 104 | + |
| 105 | + // next reply |
| 106 | + base = scan; |
| 107 | + } |
| 108 | + } |
| 109 | + else |
| 110 | + strcpy(startupInfo->token, "badtoken\0"); |
| 111 | +} |
0 commit comments