xref: /haiku/src/apps/haikudepot/server/WebAppInterface.h (revision 344ded80d400028c8f561b4b876257b94c12db4a)
1 /*
2  * Copyright 2014, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2016-2024, 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 <Locker.h>
13 #include <String.h>
14 #include <package/PackageVersion.h>
15 
16 #include "AccessToken.h"
17 #include "DepotInfo.h"
18 #include "PackageInfo.h"
19 #include "PasswordRequirements.h"
20 #include "UserCredentials.h"
21 #include "UserDetail.h"
22 #include "UserUsageConditions.h"
23 
24 
25 class BDataIO;
26 class BMessage;
27 using BPackageKit::BPackageVersion;
28 
29 
30 /*! These are error codes that are sent back to the client from the server */
31 
32 #define ERROR_CODE_NONE							0
33 #define ERROR_CODE_VALIDATION					-32800
34 #define ERROR_CODE_OBJECTNOTFOUND				-32801
35 #define ERROR_CODE_CAPTCHABADRESPONSE			-32802
36 #define ERROR_CODE_AUTHORIZATIONFAILURE			-32803
37 #define ERROR_CODE_BADPKGICON					-32804
38 #define ERROR_CODE_LIMITEXCEEDED				-32805
39 #define ERROR_CODE_AUTHORIZATIONRULECONFLICT	-32806
40 
41 /*! This constant can be used to indicate the lack of a rating. */
42 
43 #define RATING_NONE -1
44 
45 
46 class WebAppInterface {
47 public:
48 								WebAppInterface();
49 	virtual						~WebAppInterface();
50 
51 			void				SetCredentials(const UserCredentials& value);
52 			const BString&		Nickname();
53 
54 			status_t			GetChangelog(
55 									const BString& packageName,
56 									BMessage& message);
57 
58 			status_t			RetrieveUserRatingSummaryForPackage(
59 									const BString& packageName,
60                                     const BString& webAppRepositoryCode,
61                                     BMessage& message);
62 
63 			status_t			RetrieveUserRatingsForPackageForDisplay(
64 									const BString& packageName,
65 									const BString& webAppRepositoryCode,
66 									const BString& webAppRepositorySourceCode,
67 									int resultOffset, int maxResults,
68 									BMessage& message);
69 
70 			status_t			RetrieveUserRatingForPackageAndVersionByUser(
71 									const BString& packageName,
72 									const BPackageVersion& version,
73 									const BString& architecture,
74 									const BString& webAppRepositoryCode,
75 									const BString& webAppRepositorySourceCode,
76 									const BString& userNickname,
77 									BMessage& message);
78 
79 			status_t			CreateUserRating(
80 									const BString& packageName,
81 									const BPackageVersion& version,
82 									const BString& architecture,
83 									const BString& webAppRepositoryCode,
84 									const BString& webAppRepositorySourceCode,
85 									const BString& naturalLanguageCode,
86 										// This is the "ID" in the ICU system; the term `code`
87 										// is used with the server system.
88 									const BString& comment,
89 									const BString& stability,
90 									int rating,
91 									BMessage& message);
92 
93 			status_t			UpdateUserRating(
94 									const BString& ratingID,
95 									const BString& naturalLanguageCode,
96 										// This is the "ID" in the ICU system; the term `code`
97 										// is used with the server system.
98 									const BString& comment,
99 									const BString& stability,
100 									int rating, bool active,
101 									BMessage& message);
102 
103 			status_t			RetrieveUserDetailForCredentials(
104 									const UserCredentials& credentials,
105 									BMessage& message);
106 
107 			status_t			RetrieveCurrentUserDetail(
108 									BMessage& message);
109 
110 			status_t			RetrieveUserUsageConditions(
111 									const BString& code,
112 									UserUsageConditions& conditions);
113 
114 			status_t			AgreeUserUsageConditions(const BString& code,
115 									BMessage& responsePayload);
116 
117 			status_t			RetrieveScreenshot(
118 									const BString& code,
119 									int32 width, int32 height,
120 									BDataIO* stream);
121 
122 			status_t			RequestCaptcha(BMessage& message);
123 
124 			status_t			CreateUser(const BString& nickName,
125 									const BString& passwordClear,
126 									const BString& email,
127 									const BString& captchaToken,
128 									const BString& captchaResponse,
129 									const BString& naturalLanguageCode,
130 										// This is the "ID" in the ICU system; the term `code`
131 										// is used with the server system.
132 									const BString& userUsageConditionsCode,
133 									BMessage& message);
134 
135 			status_t			AuthenticateUserRetainingAccessToken();
136 
137 			status_t			AuthenticateUser(const BString& nickName,
138 									const BString& passwordClear,
139 									BMessage& message);
140 
141 			status_t			IncrementViewCounter(
142 									const PackageInfoRef package,
143 									const DepotInfoRef depot,
144 									BMessage& message);
145 
146 			status_t			RetrievePasswordRequirements(
147 									PasswordRequirements& passwordRequirements);
148 
149 	static	int32				ErrorCodeFromResponse(
150 									BMessage& responseEnvelopeMessage);
151 
152 	static	status_t			UnpackUserDetail(
153 									BMessage& responseEnvelopeMessage,
154 									UserDetail& userDetail);
155 
156 	static	status_t			UnpackAccessToken(
157 									BMessage& responseEnvelopeMessage,
158 									AccessToken& accessToken);
159 private:
160 			UserCredentials		_Credentials();
161 
162 			AccessToken			_ObtainValidAccessToken();
163 
164 			status_t			_AuthenticateUserRetainingAccessToken(const BString& nickName,
165 									const BString& passwordClear);
166 
167 			status_t			_RetrievePasswordRequirementsMeta(
168 									BMessage& message);
169 
170 			status_t			_RetrieveUserUsageConditionsMeta(
171 									const BString& code, BMessage& message);
172 			status_t			_RetrieveUserUsageConditionsCopy(
173 									const BString& code, BDataIO* stream);
174 
175 			status_t			_SendJsonRequest(const char* urlPathComponents,
176 									const BString& jsonString, uint32 flags,
177 									BMessage& reply);
178 			status_t			_SendJsonRequest(const char* urlPathComponents,
179 									BPositionIO* requestData,
180 									size_t requestDataSize, uint32 flags,
181 									BMessage& reply);
182 	static	status_t			_SendJsonRequest(const char* urlPathComponents,
183 									const AccessToken& accessToken,
184 									BPositionIO* requestData,
185 									size_t requestDataSize, uint32 flags,
186 									BMessage& reply);
187 
188 			status_t			_SendRawGetRequest(
189 									const BString urlPathComponents,
190 									BDataIO* stream);
191 	static	void				_LogPayload(BPositionIO* requestData,
192 									size_t size);
193 	static	off_t				_LengthAndSeekToZero(BPositionIO* data);
194 
195 private:
196 			UserCredentials		fCredentials;
197 			AccessToken			fAccessToken;
198 			BLocker				fLock;
199 };
200 
201 
202 #endif // WEB_APP_INTERFACE_H
203