1 /* 2 * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>. 3 * Copyright 2016-2023, Andrew Lindesay <apl@lindesay.co.nz>. 4 * All rights reserved. Distributed under the terms of the MIT License. 5 */ 6 #ifndef WEB_APP_INTERFACE_H 7 #define WEB_APP_INTERFACE_H 8 9 10 #include <Application.h> 11 #include <JsonWriter.h> 12 #include <String.h> 13 #include <package/PackageVersion.h> 14 15 #include "PackageInfo.h" 16 #include "PasswordRequirements.h" 17 #include "UserCredentials.h" 18 #include "UserDetail.h" 19 #include "UserUsageConditions.h" 20 21 22 class BDataIO; 23 class BMessage; 24 using BPackageKit::BPackageVersion; 25 26 27 /*! These are error codes that are sent back to the client from the server */ 28 29 #define ERROR_CODE_NONE 0 30 #define ERROR_CODE_VALIDATION -32800 31 #define ERROR_CODE_OBJECTNOTFOUND -32801 32 #define ERROR_CODE_CAPTCHABADRESPONSE -32802 33 #define ERROR_CODE_AUTHORIZATIONFAILURE -32803 34 #define ERROR_CODE_BADPKGICON -32804 35 #define ERROR_CODE_LIMITEXCEEDED -32805 36 #define ERROR_CODE_AUTHORIZATIONRULECONFLICT -32806 37 38 /*! This constant can be used to indicate the lack of a rating. */ 39 40 #define RATING_NONE -1 41 42 43 class WebAppInterface { 44 public: 45 WebAppInterface(); 46 WebAppInterface(const WebAppInterface& other); 47 virtual ~WebAppInterface(); 48 49 WebAppInterface& operator=(const WebAppInterface& other); 50 51 void SetAuthorization(const UserCredentials& value); 52 const BString& Nickname() const; 53 54 status_t GetChangelog( 55 const BString& packageName, 56 BMessage& message); 57 58 status_t RetreiveUserRatingsForPackageForDisplay( 59 const BString& packageName, 60 const BString& webAppRepositoryCode, 61 const BString& webAppRepositorySourceCode, 62 int resultOffset, int maxResults, 63 BMessage& message); 64 65 status_t RetreiveUserRatingForPackageAndVersionByUser( 66 const BString& packageName, 67 const BPackageVersion& version, 68 const BString& architecture, 69 const BString& webAppRepositoryCode, 70 const BString& webAppRepositorySourceCode, 71 const BString& userNickname, 72 BMessage& message); 73 74 status_t CreateUserRating( 75 const BString& packageName, 76 const BPackageVersion& version, 77 const BString& architecture, 78 const BString& webAppRepositoryCode, 79 const BString& webAppRepositorySourceCode, 80 const BString& languageCode, 81 const BString& comment, 82 const BString& stability, 83 int rating, 84 BMessage& message); 85 86 status_t UpdateUserRating( 87 const BString& ratingID, 88 const BString& languageCode, 89 const BString& comment, 90 const BString& stability, 91 int rating, bool active, 92 BMessage& message); 93 94 status_t RetrieveUserDetailForCredentials( 95 const UserCredentials& credentials, 96 BMessage& message); 97 98 status_t RetrieveCurrentUserDetail( 99 BMessage& message); 100 101 status_t RetrieveUserUsageConditions( 102 const BString& code, 103 UserUsageConditions& conditions); 104 105 status_t AgreeUserUsageConditions(const BString& code, 106 BMessage& responsePayload); 107 108 status_t RetrieveScreenshot( 109 const BString& code, 110 int32 width, int32 height, 111 BDataIO* stream); 112 113 status_t RequestCaptcha(BMessage& message); 114 115 status_t CreateUser(const BString& nickName, 116 const BString& passwordClear, 117 const BString& email, 118 const BString& captchaToken, 119 const BString& captchaResponse, 120 const BString& languageCode, 121 const BString& userUsageConditionsCode, 122 BMessage& message); 123 124 status_t AuthenticateUser(const BString& nickName, 125 const BString& passwordClear, 126 BMessage& message); 127 128 status_t IncrementViewCounter( 129 const PackageInfoRef package, 130 const DepotInfoRef depot, 131 BMessage& message); 132 133 status_t RetrievePasswordRequirements( 134 PasswordRequirements& passwordRequirements); 135 136 static int32 ErrorCodeFromResponse( 137 BMessage& responseEnvelopeMessage); 138 139 static status_t UnpackUserDetail( 140 BMessage& responseEnvelopeMessage, 141 UserDetail& userDetail); 142 private: 143 144 status_t _RetrievePasswordRequirementsMeta( 145 BMessage& message); 146 147 status_t _RetrieveUserUsageConditionsMeta( 148 const BString& code, BMessage& message); 149 status_t _RetrieveUserUsageConditionsCopy( 150 const BString& code, BDataIO* stream); 151 152 status_t _SendJsonRequest(const char* urlPathComponents, 153 const BString& jsonString, uint32 flags, 154 BMessage& reply) const; 155 status_t _SendJsonRequest(const char* urlPathComponents, 156 UserCredentials credentials, 157 BPositionIO* requestData, 158 size_t requestDataSize, uint32 flags, 159 BMessage& reply) const; 160 status_t _SendJsonRequest(const char* urlPathComponents, 161 BPositionIO* requestData, 162 size_t requestDataSize, uint32 flags, 163 BMessage& reply) const; 164 165 status_t _SendRawGetRequest( 166 const BString urlPathComponents, 167 BDataIO* stream); 168 static void _LogPayload(BPositionIO* requestData, 169 size_t size); 170 static off_t _LengthAndSeekToZero(BPositionIO* data); 171 172 private: 173 UserCredentials fCredentials; 174 static int fRequestIndex; 175 }; 176 177 178 #endif // WEB_APP_INTERFACE_H 179