xref: /haiku/src/apps/haikudepot/server/WebAppInterface.h (revision 4b347fccb28bb9f7242ba8bff1f49671247a7a75)
119c15fecSAndrew Lindesay /*
219c15fecSAndrew Lindesay  * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>.
3b6356b91SAndrew Lindesay  * Copyright 2016-2023, 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>
12*4b347fccSAndrew Lindesay #include <Locker.h>
1319c15fecSAndrew Lindesay #include <String.h>
1419c15fecSAndrew Lindesay #include <package/PackageVersion.h>
1519c15fecSAndrew Lindesay 
16*4b347fccSAndrew Lindesay #include "AccessToken.h"
17133ebab6SAndrew Lindesay #include "PackageInfo.h"
18b6356b91SAndrew Lindesay #include "PasswordRequirements.h"
19d2d4866dSAndrew Lindesay #include "UserCredentials.h"
20d2d4866dSAndrew Lindesay #include "UserDetail.h"
2101339a54SAndrew Lindesay #include "UserUsageConditions.h"
2219c15fecSAndrew Lindesay 
2319c15fecSAndrew Lindesay 
2419c15fecSAndrew Lindesay class BDataIO;
2519c15fecSAndrew Lindesay class BMessage;
2619c15fecSAndrew Lindesay using BPackageKit::BPackageVersion;
2719c15fecSAndrew Lindesay 
2819c15fecSAndrew Lindesay 
29a9edb9bfSAndrew Lindesay /*! These are error codes that are sent back to the client from the server */
30a9edb9bfSAndrew Lindesay 
31a9edb9bfSAndrew Lindesay #define ERROR_CODE_NONE							0
32a9edb9bfSAndrew Lindesay #define ERROR_CODE_VALIDATION					-32800
33a9edb9bfSAndrew Lindesay #define ERROR_CODE_OBJECTNOTFOUND				-32801
34a9edb9bfSAndrew Lindesay #define ERROR_CODE_CAPTCHABADRESPONSE			-32802
35a9edb9bfSAndrew Lindesay #define ERROR_CODE_AUTHORIZATIONFAILURE			-32803
36a9edb9bfSAndrew Lindesay #define ERROR_CODE_BADPKGICON					-32804
37a9edb9bfSAndrew Lindesay #define ERROR_CODE_LIMITEXCEEDED				-32805
38a9edb9bfSAndrew Lindesay #define ERROR_CODE_AUTHORIZATIONRULECONFLICT	-32806
39a9edb9bfSAndrew Lindesay 
40018d5927SAndrew Lindesay /*! This constant can be used to indicate the lack of a rating. */
41018d5927SAndrew Lindesay 
42018d5927SAndrew Lindesay #define RATING_NONE -1
43018d5927SAndrew Lindesay 
44a9edb9bfSAndrew Lindesay 
4519c15fecSAndrew Lindesay class WebAppInterface {
4619c15fecSAndrew Lindesay public:
4719c15fecSAndrew Lindesay 								WebAppInterface();
4819c15fecSAndrew Lindesay 	virtual						~WebAppInterface();
4919c15fecSAndrew Lindesay 
50*4b347fccSAndrew Lindesay 			void				SetCredentials(const UserCredentials& value);
51*4b347fccSAndrew Lindesay 			const BString&		Nickname();
5219c15fecSAndrew Lindesay 
5380a272eeSAndrew Lindesay 			status_t			GetChangelog(
5419c15fecSAndrew Lindesay 									const BString& packageName,
5519c15fecSAndrew Lindesay 									BMessage& message);
5619c15fecSAndrew Lindesay 
57*4b347fccSAndrew Lindesay 			status_t			RetrieveUserRatingsForPackageForDisplay(
5819c15fecSAndrew Lindesay 									const BString& packageName,
59051ee9d8SAndrew Lindesay 									const BString& webAppRepositoryCode,
60f6e22563SAndrew Lindesay 									const BString& webAppRepositorySourceCode,
6119c15fecSAndrew Lindesay 									int resultOffset, int maxResults,
6219c15fecSAndrew Lindesay 									BMessage& message);
6319c15fecSAndrew Lindesay 
64*4b347fccSAndrew Lindesay 			status_t			RetrieveUserRatingForPackageAndVersionByUser(
6519c15fecSAndrew Lindesay 									const BString& packageName,
6619c15fecSAndrew Lindesay 									const BPackageVersion& version,
6719c15fecSAndrew Lindesay 									const BString& architecture,
68f6e22563SAndrew Lindesay 									const BString& webAppRepositoryCode,
69f6e22563SAndrew Lindesay 									const BString& webAppRepositorySourceCode,
70d2d4866dSAndrew Lindesay 									const BString& userNickname,
7119c15fecSAndrew Lindesay 									BMessage& message);
7219c15fecSAndrew Lindesay 
7319c15fecSAndrew Lindesay 			status_t			CreateUserRating(
7419c15fecSAndrew Lindesay 									const BString& packageName,
75a9edb9bfSAndrew Lindesay 									const BPackageVersion& version,
7619c15fecSAndrew Lindesay 									const BString& architecture,
77f6e22563SAndrew Lindesay 									const BString& webAppRepositoryCode,
78f6e22563SAndrew Lindesay 									const BString& webAppRepositorySourceCode,
7919c15fecSAndrew Lindesay 									const BString& languageCode,
8019c15fecSAndrew Lindesay 									const BString& comment,
8119c15fecSAndrew Lindesay 									const BString& stability,
8219c15fecSAndrew Lindesay 									int rating,
8319c15fecSAndrew Lindesay 									BMessage& message);
8419c15fecSAndrew Lindesay 
8519c15fecSAndrew Lindesay 			status_t			UpdateUserRating(
8619c15fecSAndrew Lindesay 									const BString& ratingID,
8719c15fecSAndrew Lindesay 									const BString& languageCode,
8819c15fecSAndrew Lindesay 									const BString& comment,
8919c15fecSAndrew Lindesay 									const BString& stability,
9019c15fecSAndrew Lindesay 									int rating, bool active,
9119c15fecSAndrew Lindesay 									BMessage& message);
9219c15fecSAndrew Lindesay 
93d2d4866dSAndrew Lindesay 			status_t			RetrieveUserDetailForCredentials(
94d2d4866dSAndrew Lindesay 									const UserCredentials& credentials,
95d17c92f7SAndrew Lindesay 									BMessage& message);
96d2d4866dSAndrew Lindesay 
97d2d4866dSAndrew Lindesay 			status_t			RetrieveCurrentUserDetail(
98d17c92f7SAndrew Lindesay 									BMessage& message);
99d2d4866dSAndrew Lindesay 
10001339a54SAndrew Lindesay 			status_t			RetrieveUserUsageConditions(
10101339a54SAndrew Lindesay 									const BString& code,
10201339a54SAndrew Lindesay 									UserUsageConditions& conditions);
10301339a54SAndrew Lindesay 
10410cd325cSAndrew Lindesay 			status_t			AgreeUserUsageConditions(const BString& code,
10510cd325cSAndrew Lindesay 									BMessage& responsePayload);
10610cd325cSAndrew Lindesay 
10719c15fecSAndrew Lindesay 			status_t			RetrieveScreenshot(
10819c15fecSAndrew Lindesay 									const BString& code,
10919c15fecSAndrew Lindesay 									int32 width, int32 height,
11019c15fecSAndrew Lindesay 									BDataIO* stream);
11119c15fecSAndrew Lindesay 
11219c15fecSAndrew Lindesay 			status_t			RequestCaptcha(BMessage& message);
11319c15fecSAndrew Lindesay 
11419c15fecSAndrew Lindesay 			status_t			CreateUser(const BString& nickName,
11519c15fecSAndrew Lindesay 									const BString& passwordClear,
11619c15fecSAndrew Lindesay 									const BString& email,
11719c15fecSAndrew Lindesay 									const BString& captchaToken,
11819c15fecSAndrew Lindesay 									const BString& captchaResponse,
11919c15fecSAndrew Lindesay 									const BString& languageCode,
1200c82f64bSAndrew Lindesay 									const BString& userUsageConditionsCode,
12119c15fecSAndrew Lindesay 									BMessage& message);
12219c15fecSAndrew Lindesay 
123*4b347fccSAndrew Lindesay 			status_t			AuthenticateUserRetainingAccessToken();
124*4b347fccSAndrew Lindesay 
12519c15fecSAndrew Lindesay 			status_t			AuthenticateUser(const BString& nickName,
12619c15fecSAndrew Lindesay 									const BString& passwordClear,
12719c15fecSAndrew Lindesay 									BMessage& message);
12819c15fecSAndrew Lindesay 
129133ebab6SAndrew Lindesay 			status_t			IncrementViewCounter(
130133ebab6SAndrew Lindesay 									const PackageInfoRef package,
131133ebab6SAndrew Lindesay 									const DepotInfoRef depot,
132133ebab6SAndrew Lindesay 									BMessage& message);
133133ebab6SAndrew Lindesay 
134b6356b91SAndrew Lindesay 			status_t			RetrievePasswordRequirements(
135b6356b91SAndrew Lindesay 									PasswordRequirements& passwordRequirements);
136b6356b91SAndrew Lindesay 
137d17c92f7SAndrew Lindesay 	static	int32				ErrorCodeFromResponse(
138d17c92f7SAndrew Lindesay 									BMessage& responseEnvelopeMessage);
139a9edb9bfSAndrew Lindesay 
140d17c92f7SAndrew Lindesay 	static	status_t			UnpackUserDetail(
141d2d4866dSAndrew Lindesay 									BMessage& responseEnvelopeMessage,
142d2d4866dSAndrew Lindesay 									UserDetail& userDetail);
143*4b347fccSAndrew Lindesay 
144*4b347fccSAndrew Lindesay 	static	status_t			UnpackAccessToken(
145*4b347fccSAndrew Lindesay 									BMessage& responseEnvelopeMessage,
146*4b347fccSAndrew Lindesay 									AccessToken& accessToken);
147d17c92f7SAndrew Lindesay private:
148*4b347fccSAndrew Lindesay 			UserCredentials		_Credentials();
149*4b347fccSAndrew Lindesay 
150*4b347fccSAndrew Lindesay 			AccessToken			_ObtainValidAccessToken();
151*4b347fccSAndrew Lindesay 
152*4b347fccSAndrew Lindesay 			status_t			_AuthenticateUserRetainingAccessToken(const BString& nickName,
153*4b347fccSAndrew Lindesay 									const BString& passwordClear);
154d17c92f7SAndrew Lindesay 
155b6356b91SAndrew Lindesay 			status_t			_RetrievePasswordRequirementsMeta(
156b6356b91SAndrew Lindesay 									BMessage& message);
157d2d4866dSAndrew Lindesay 
15801339a54SAndrew Lindesay 			status_t			_RetrieveUserUsageConditionsMeta(
15901339a54SAndrew Lindesay 									const BString& code, BMessage& message);
16001339a54SAndrew Lindesay 			status_t			_RetrieveUserUsageConditionsCopy(
16101339a54SAndrew Lindesay 									const BString& code, BDataIO* stream);
162d2d4866dSAndrew Lindesay 
16321df7324SAndrew Lindesay 			status_t			_SendJsonRequest(const char* urlPathComponents,
16488575af1SAndrew Lindesay 									const BString& jsonString, uint32 flags,
165*4b347fccSAndrew Lindesay 									BMessage& reply);
16621df7324SAndrew Lindesay 			status_t			_SendJsonRequest(const char* urlPathComponents,
167d2d4866dSAndrew Lindesay 									BPositionIO* requestData,
168d2d4866dSAndrew Lindesay 									size_t requestDataSize, uint32 flags,
169*4b347fccSAndrew Lindesay 									BMessage& reply);
170*4b347fccSAndrew Lindesay 	static	status_t			_SendJsonRequest(const char* urlPathComponents,
171*4b347fccSAndrew Lindesay 									const AccessToken& accessToken,
172*4b347fccSAndrew Lindesay 									BPositionIO* requestData,
173*4b347fccSAndrew Lindesay 									size_t requestDataSize, uint32 flags,
174*4b347fccSAndrew Lindesay 									BMessage& reply);
175d2d4866dSAndrew Lindesay 
176d2d4866dSAndrew Lindesay 			status_t			_SendRawGetRequest(
177d2d4866dSAndrew Lindesay 									const BString urlPathComponents,
178d2d4866dSAndrew Lindesay 									BDataIO* stream);
17988575af1SAndrew Lindesay 	static	void				_LogPayload(BPositionIO* requestData,
18088575af1SAndrew Lindesay 									size_t size);
18188575af1SAndrew Lindesay 	static	off_t				_LengthAndSeekToZero(BPositionIO* data);
18219c15fecSAndrew Lindesay 
18319c15fecSAndrew Lindesay private:
184d2d4866dSAndrew Lindesay 			UserCredentials		fCredentials;
185*4b347fccSAndrew Lindesay 			AccessToken			fAccessToken;
186*4b347fccSAndrew Lindesay 			BLocker				fLock;
18719c15fecSAndrew Lindesay };
18819c15fecSAndrew Lindesay 
18919c15fecSAndrew Lindesay 
19019c15fecSAndrew Lindesay #endif // WEB_APP_INTERFACE_H
191