119c15fecSAndrew Lindesay /* 219c15fecSAndrew Lindesay * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>. 3bf866d5eSAndrew Lindesay * Copyright 2016-2024, Andrew Lindesay <apl@lindesay.co.nz>. 419c15fecSAndrew Lindesay * All rights reserved. Distributed under the terms of the MIT License. 519c15fecSAndrew Lindesay */ 619c15fecSAndrew Lindesay #ifndef WEB_APP_INTERFACE_H 719c15fecSAndrew Lindesay #define WEB_APP_INTERFACE_H 819c15fecSAndrew Lindesay 919c15fecSAndrew Lindesay 1019c15fecSAndrew Lindesay #include <Application.h> 11a9edb9bfSAndrew Lindesay #include <JsonWriter.h> 124b347fccSAndrew Lindesay #include <Locker.h> 1319c15fecSAndrew Lindesay #include <String.h> 1419c15fecSAndrew Lindesay #include <package/PackageVersion.h> 1519c15fecSAndrew Lindesay 164b347fccSAndrew Lindesay #include "AccessToken.h" 1713bbb4d8SAndrew Lindesay #include "DepotInfo.h" 18133ebab6SAndrew Lindesay #include "PackageInfo.h" 19b6356b91SAndrew Lindesay #include "PasswordRequirements.h" 20d2d4866dSAndrew Lindesay #include "UserCredentials.h" 21d2d4866dSAndrew Lindesay #include "UserDetail.h" 2201339a54SAndrew Lindesay #include "UserUsageConditions.h" 2319c15fecSAndrew Lindesay 2419c15fecSAndrew Lindesay 2519c15fecSAndrew Lindesay class BDataIO; 2619c15fecSAndrew Lindesay class BMessage; 2719c15fecSAndrew Lindesay using BPackageKit::BPackageVersion; 2819c15fecSAndrew Lindesay 2919c15fecSAndrew Lindesay 30a9edb9bfSAndrew Lindesay /*! These are error codes that are sent back to the client from the server */ 31a9edb9bfSAndrew Lindesay 32a9edb9bfSAndrew Lindesay #define ERROR_CODE_NONE 0 33a9edb9bfSAndrew Lindesay #define ERROR_CODE_VALIDATION -32800 34a9edb9bfSAndrew Lindesay #define ERROR_CODE_OBJECTNOTFOUND -32801 35a9edb9bfSAndrew Lindesay #define ERROR_CODE_CAPTCHABADRESPONSE -32802 36a9edb9bfSAndrew Lindesay #define ERROR_CODE_AUTHORIZATIONFAILURE -32803 37a9edb9bfSAndrew Lindesay #define ERROR_CODE_BADPKGICON -32804 38a9edb9bfSAndrew Lindesay #define ERROR_CODE_LIMITEXCEEDED -32805 39a9edb9bfSAndrew Lindesay #define ERROR_CODE_AUTHORIZATIONRULECONFLICT -32806 40a9edb9bfSAndrew Lindesay 41018d5927SAndrew Lindesay /*! This constant can be used to indicate the lack of a rating. */ 42018d5927SAndrew Lindesay 43018d5927SAndrew Lindesay #define RATING_NONE -1 44018d5927SAndrew Lindesay 45a9edb9bfSAndrew Lindesay 4619c15fecSAndrew Lindesay class WebAppInterface { 4719c15fecSAndrew Lindesay public: 4819c15fecSAndrew Lindesay WebAppInterface(); 4919c15fecSAndrew Lindesay virtual ~WebAppInterface(); 5019c15fecSAndrew Lindesay 514b347fccSAndrew Lindesay void SetCredentials(const UserCredentials& value); 524b347fccSAndrew Lindesay const BString& Nickname(); 5319c15fecSAndrew Lindesay 5480a272eeSAndrew Lindesay status_t GetChangelog( 5519c15fecSAndrew Lindesay const BString& packageName, 5619c15fecSAndrew Lindesay BMessage& message); 5719c15fecSAndrew Lindesay 58*4b3c808eSAndrew Lindesay status_t RetrieveUserRatingSummaryForPackage( 59*4b3c808eSAndrew Lindesay const BString& packageName, 60*4b3c808eSAndrew Lindesay const BString& webAppRepositoryCode, 61*4b3c808eSAndrew Lindesay BMessage& message); 62*4b3c808eSAndrew Lindesay 634b347fccSAndrew Lindesay status_t RetrieveUserRatingsForPackageForDisplay( 6419c15fecSAndrew Lindesay const BString& packageName, 65051ee9d8SAndrew Lindesay const BString& webAppRepositoryCode, 66f6e22563SAndrew Lindesay const BString& webAppRepositorySourceCode, 6719c15fecSAndrew Lindesay int resultOffset, int maxResults, 6819c15fecSAndrew Lindesay BMessage& message); 6919c15fecSAndrew Lindesay 704b347fccSAndrew Lindesay status_t RetrieveUserRatingForPackageAndVersionByUser( 7119c15fecSAndrew Lindesay const BString& packageName, 7219c15fecSAndrew Lindesay const BPackageVersion& version, 7319c15fecSAndrew Lindesay const BString& architecture, 74f6e22563SAndrew Lindesay const BString& webAppRepositoryCode, 75f6e22563SAndrew Lindesay const BString& webAppRepositorySourceCode, 76d2d4866dSAndrew Lindesay const BString& userNickname, 7719c15fecSAndrew Lindesay BMessage& message); 7819c15fecSAndrew Lindesay 7919c15fecSAndrew Lindesay status_t CreateUserRating( 8019c15fecSAndrew Lindesay const BString& packageName, 81a9edb9bfSAndrew Lindesay const BPackageVersion& version, 8219c15fecSAndrew Lindesay const BString& architecture, 83f6e22563SAndrew Lindesay const BString& webAppRepositoryCode, 84f6e22563SAndrew Lindesay const BString& webAppRepositorySourceCode, 85bf866d5eSAndrew Lindesay const BString& naturalLanguageCode, 86bf866d5eSAndrew Lindesay // This is the "ID" in the ICU system; the term `code` 87bf866d5eSAndrew Lindesay // is used with the server system. 8819c15fecSAndrew Lindesay const BString& comment, 8919c15fecSAndrew Lindesay const BString& stability, 9019c15fecSAndrew Lindesay int rating, 9119c15fecSAndrew Lindesay BMessage& message); 9219c15fecSAndrew Lindesay 9319c15fecSAndrew Lindesay status_t UpdateUserRating( 9419c15fecSAndrew Lindesay const BString& ratingID, 95bf866d5eSAndrew Lindesay const BString& naturalLanguageCode, 96bf866d5eSAndrew Lindesay // This is the "ID" in the ICU system; the term `code` 97bf866d5eSAndrew Lindesay // is used with the server system. 9819c15fecSAndrew Lindesay const BString& comment, 9919c15fecSAndrew Lindesay const BString& stability, 10019c15fecSAndrew Lindesay int rating, bool active, 10119c15fecSAndrew Lindesay BMessage& message); 10219c15fecSAndrew Lindesay 103d2d4866dSAndrew Lindesay status_t RetrieveUserDetailForCredentials( 104d2d4866dSAndrew Lindesay const UserCredentials& credentials, 105d17c92f7SAndrew Lindesay BMessage& message); 106d2d4866dSAndrew Lindesay 107d2d4866dSAndrew Lindesay status_t RetrieveCurrentUserDetail( 108d17c92f7SAndrew Lindesay BMessage& message); 109d2d4866dSAndrew Lindesay 11001339a54SAndrew Lindesay status_t RetrieveUserUsageConditions( 11101339a54SAndrew Lindesay const BString& code, 11201339a54SAndrew Lindesay UserUsageConditions& conditions); 11301339a54SAndrew Lindesay 11410cd325cSAndrew Lindesay status_t AgreeUserUsageConditions(const BString& code, 11510cd325cSAndrew Lindesay BMessage& responsePayload); 11610cd325cSAndrew Lindesay 11719c15fecSAndrew Lindesay status_t RetrieveScreenshot( 11819c15fecSAndrew Lindesay const BString& code, 11919c15fecSAndrew Lindesay int32 width, int32 height, 12019c15fecSAndrew Lindesay BDataIO* stream); 12119c15fecSAndrew Lindesay 12219c15fecSAndrew Lindesay status_t RequestCaptcha(BMessage& message); 12319c15fecSAndrew Lindesay 12419c15fecSAndrew Lindesay status_t CreateUser(const BString& nickName, 12519c15fecSAndrew Lindesay const BString& passwordClear, 12619c15fecSAndrew Lindesay const BString& email, 12719c15fecSAndrew Lindesay const BString& captchaToken, 12819c15fecSAndrew Lindesay const BString& captchaResponse, 129bf866d5eSAndrew Lindesay const BString& naturalLanguageCode, 130bf866d5eSAndrew Lindesay // This is the "ID" in the ICU system; the term `code` 131bf866d5eSAndrew Lindesay // is used with the server system. 1320c82f64bSAndrew Lindesay const BString& userUsageConditionsCode, 13319c15fecSAndrew Lindesay BMessage& message); 13419c15fecSAndrew Lindesay 1354b347fccSAndrew Lindesay status_t AuthenticateUserRetainingAccessToken(); 1364b347fccSAndrew Lindesay 13719c15fecSAndrew Lindesay status_t AuthenticateUser(const BString& nickName, 13819c15fecSAndrew Lindesay const BString& passwordClear, 13919c15fecSAndrew Lindesay BMessage& message); 14019c15fecSAndrew Lindesay 141133ebab6SAndrew Lindesay status_t IncrementViewCounter( 142133ebab6SAndrew Lindesay const PackageInfoRef package, 143133ebab6SAndrew Lindesay const DepotInfoRef depot, 144133ebab6SAndrew Lindesay BMessage& message); 145133ebab6SAndrew Lindesay 146b6356b91SAndrew Lindesay status_t RetrievePasswordRequirements( 147b6356b91SAndrew Lindesay PasswordRequirements& passwordRequirements); 148b6356b91SAndrew Lindesay 149d17c92f7SAndrew Lindesay static int32 ErrorCodeFromResponse( 150d17c92f7SAndrew Lindesay BMessage& responseEnvelopeMessage); 151a9edb9bfSAndrew Lindesay 152d17c92f7SAndrew Lindesay static status_t UnpackUserDetail( 153d2d4866dSAndrew Lindesay BMessage& responseEnvelopeMessage, 154d2d4866dSAndrew Lindesay UserDetail& userDetail); 1554b347fccSAndrew Lindesay 1564b347fccSAndrew Lindesay static status_t UnpackAccessToken( 1574b347fccSAndrew Lindesay BMessage& responseEnvelopeMessage, 1584b347fccSAndrew Lindesay AccessToken& accessToken); 159d17c92f7SAndrew Lindesay private: 1604b347fccSAndrew Lindesay UserCredentials _Credentials(); 1614b347fccSAndrew Lindesay 1624b347fccSAndrew Lindesay AccessToken _ObtainValidAccessToken(); 1634b347fccSAndrew Lindesay 1644b347fccSAndrew Lindesay status_t _AuthenticateUserRetainingAccessToken(const BString& nickName, 1654b347fccSAndrew Lindesay const BString& passwordClear); 166d17c92f7SAndrew Lindesay 167b6356b91SAndrew Lindesay status_t _RetrievePasswordRequirementsMeta( 168b6356b91SAndrew Lindesay BMessage& message); 169d2d4866dSAndrew Lindesay 17001339a54SAndrew Lindesay status_t _RetrieveUserUsageConditionsMeta( 17101339a54SAndrew Lindesay const BString& code, BMessage& message); 17201339a54SAndrew Lindesay status_t _RetrieveUserUsageConditionsCopy( 17301339a54SAndrew Lindesay const BString& code, BDataIO* stream); 174d2d4866dSAndrew Lindesay 17521df7324SAndrew Lindesay status_t _SendJsonRequest(const char* urlPathComponents, 17688575af1SAndrew Lindesay const BString& jsonString, uint32 flags, 1774b347fccSAndrew Lindesay BMessage& reply); 17821df7324SAndrew Lindesay status_t _SendJsonRequest(const char* urlPathComponents, 179d2d4866dSAndrew Lindesay BPositionIO* requestData, 180d2d4866dSAndrew Lindesay size_t requestDataSize, uint32 flags, 1814b347fccSAndrew Lindesay BMessage& reply); 1824b347fccSAndrew Lindesay static status_t _SendJsonRequest(const char* urlPathComponents, 1834b347fccSAndrew Lindesay const AccessToken& accessToken, 1844b347fccSAndrew Lindesay BPositionIO* requestData, 1854b347fccSAndrew Lindesay size_t requestDataSize, uint32 flags, 1864b347fccSAndrew Lindesay BMessage& reply); 187d2d4866dSAndrew Lindesay 188d2d4866dSAndrew Lindesay status_t _SendRawGetRequest( 189d2d4866dSAndrew Lindesay const BString urlPathComponents, 190d2d4866dSAndrew Lindesay BDataIO* stream); 19188575af1SAndrew Lindesay static void _LogPayload(BPositionIO* requestData, 19288575af1SAndrew Lindesay size_t size); 19388575af1SAndrew Lindesay static off_t _LengthAndSeekToZero(BPositionIO* data); 19419c15fecSAndrew Lindesay 19519c15fecSAndrew Lindesay private: 196d2d4866dSAndrew Lindesay UserCredentials fCredentials; 1974b347fccSAndrew Lindesay AccessToken fAccessToken; 1984b347fccSAndrew Lindesay BLocker fLock; 19919c15fecSAndrew Lindesay }; 20019c15fecSAndrew Lindesay 20119c15fecSAndrew Lindesay 20219c15fecSAndrew Lindesay #endif // WEB_APP_INTERFACE_H 203