xref: /haiku/src/apps/haikudepot/server/WebAppInterface.h (revision 4c07199d8201fcf267e90be0d24b76799d03cea6)
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 <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& 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			AuthenticateUserRetainingAccessToken();
125 
126 			status_t			AuthenticateUser(const BString& nickName,
127 									const BString& passwordClear,
128 									BMessage& message);
129 
130 			status_t			IncrementViewCounter(
131 									const PackageInfoRef package,
132 									const DepotInfoRef depot,
133 									BMessage& message);
134 
135 			status_t			RetrievePasswordRequirements(
136 									PasswordRequirements& passwordRequirements);
137 
138 	static	int32				ErrorCodeFromResponse(
139 									BMessage& responseEnvelopeMessage);
140 
141 	static	status_t			UnpackUserDetail(
142 									BMessage& responseEnvelopeMessage,
143 									UserDetail& userDetail);
144 
145 	static	status_t			UnpackAccessToken(
146 									BMessage& responseEnvelopeMessage,
147 									AccessToken& accessToken);
148 private:
149 			UserCredentials		_Credentials();
150 
151 			AccessToken			_ObtainValidAccessToken();
152 
153 			status_t			_AuthenticateUserRetainingAccessToken(const BString& nickName,
154 									const BString& passwordClear);
155 
156 			status_t			_RetrievePasswordRequirementsMeta(
157 									BMessage& message);
158 
159 			status_t			_RetrieveUserUsageConditionsMeta(
160 									const BString& code, BMessage& message);
161 			status_t			_RetrieveUserUsageConditionsCopy(
162 									const BString& code, BDataIO* stream);
163 
164 			status_t			_SendJsonRequest(const char* urlPathComponents,
165 									const BString& jsonString, uint32 flags,
166 									BMessage& reply);
167 			status_t			_SendJsonRequest(const char* urlPathComponents,
168 									BPositionIO* requestData,
169 									size_t requestDataSize, uint32 flags,
170 									BMessage& reply);
171 	static	status_t			_SendJsonRequest(const char* urlPathComponents,
172 									const AccessToken& accessToken,
173 									BPositionIO* requestData,
174 									size_t requestDataSize, uint32 flags,
175 									BMessage& reply);
176 
177 			status_t			_SendRawGetRequest(
178 									const BString urlPathComponents,
179 									BDataIO* stream);
180 	static	void				_LogPayload(BPositionIO* requestData,
181 									size_t size);
182 	static	off_t				_LengthAndSeekToZero(BPositionIO* data);
183 
184 private:
185 			UserCredentials		fCredentials;
186 			AccessToken			fAccessToken;
187 			BLocker				fLock;
188 };
189 
190 
191 #endif // WEB_APP_INTERFACE_H
192