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