xref: /haiku/src/apps/haikudepot/server/WebAppInterface.h (revision 47c05920fde47c2618efccd24bd82f1e79cdf05a)
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			RetrieveUserRatingsForPackageForDisplay(
59 									const BString& packageName,
60 									const BString& webAppRepositoryCode,
61 									const BString& webAppRepositorySourceCode,
62 									int resultOffset, int maxResults,
63 									BMessage& message);
64 
65 			status_t			RetrieveUserRatingForPackageAndVersionByUser(
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& naturalLanguageCode,
81 										// This is the "ID" in the ICU system; the term `code`
82 										// is used with the server system.
83 									const BString& comment,
84 									const BString& stability,
85 									int rating,
86 									BMessage& message);
87 
88 			status_t			UpdateUserRating(
89 									const BString& ratingID,
90 									const BString& naturalLanguageCode,
91 										// This is the "ID" in the ICU system; the term `code`
92 										// is used with the server system.
93 									const BString& comment,
94 									const BString& stability,
95 									int rating, bool active,
96 									BMessage& message);
97 
98 			status_t			RetrieveUserDetailForCredentials(
99 									const UserCredentials& credentials,
100 									BMessage& message);
101 
102 			status_t			RetrieveCurrentUserDetail(
103 									BMessage& message);
104 
105 			status_t			RetrieveUserUsageConditions(
106 									const BString& code,
107 									UserUsageConditions& conditions);
108 
109 			status_t			AgreeUserUsageConditions(const BString& code,
110 									BMessage& responsePayload);
111 
112 			status_t			RetrieveScreenshot(
113 									const BString& code,
114 									int32 width, int32 height,
115 									BDataIO* stream);
116 
117 			status_t			RequestCaptcha(BMessage& message);
118 
119 			status_t			CreateUser(const BString& nickName,
120 									const BString& passwordClear,
121 									const BString& email,
122 									const BString& captchaToken,
123 									const BString& captchaResponse,
124 									const BString& naturalLanguageCode,
125 										// This is the "ID" in the ICU system; the term `code`
126 										// is used with the server system.
127 									const BString& userUsageConditionsCode,
128 									BMessage& message);
129 
130 			status_t			AuthenticateUserRetainingAccessToken();
131 
132 			status_t			AuthenticateUser(const BString& nickName,
133 									const BString& passwordClear,
134 									BMessage& message);
135 
136 			status_t			IncrementViewCounter(
137 									const PackageInfoRef package,
138 									const DepotInfoRef depot,
139 									BMessage& message);
140 
141 			status_t			RetrievePasswordRequirements(
142 									PasswordRequirements& passwordRequirements);
143 
144 	static	int32				ErrorCodeFromResponse(
145 									BMessage& responseEnvelopeMessage);
146 
147 	static	status_t			UnpackUserDetail(
148 									BMessage& responseEnvelopeMessage,
149 									UserDetail& userDetail);
150 
151 	static	status_t			UnpackAccessToken(
152 									BMessage& responseEnvelopeMessage,
153 									AccessToken& accessToken);
154 private:
155 			UserCredentials		_Credentials();
156 
157 			AccessToken			_ObtainValidAccessToken();
158 
159 			status_t			_AuthenticateUserRetainingAccessToken(const BString& nickName,
160 									const BString& passwordClear);
161 
162 			status_t			_RetrievePasswordRequirementsMeta(
163 									BMessage& message);
164 
165 			status_t			_RetrieveUserUsageConditionsMeta(
166 									const BString& code, BMessage& message);
167 			status_t			_RetrieveUserUsageConditionsCopy(
168 									const BString& code, BDataIO* stream);
169 
170 			status_t			_SendJsonRequest(const char* urlPathComponents,
171 									const BString& jsonString, uint32 flags,
172 									BMessage& reply);
173 			status_t			_SendJsonRequest(const char* urlPathComponents,
174 									BPositionIO* requestData,
175 									size_t requestDataSize, uint32 flags,
176 									BMessage& reply);
177 	static	status_t			_SendJsonRequest(const char* urlPathComponents,
178 									const AccessToken& accessToken,
179 									BPositionIO* requestData,
180 									size_t requestDataSize, uint32 flags,
181 									BMessage& reply);
182 
183 			status_t			_SendRawGetRequest(
184 									const BString urlPathComponents,
185 									BDataIO* stream);
186 	static	void				_LogPayload(BPositionIO* requestData,
187 									size_t size);
188 	static	off_t				_LengthAndSeekToZero(BPositionIO* data);
189 
190 private:
191 			UserCredentials		fCredentials;
192 			AccessToken			fAccessToken;
193 			BLocker				fLock;
194 };
195 
196 
197 #endif // WEB_APP_INTERFACE_H
198