1603e0bdfSNiels Sascha Reedijk /* 2*4b347fccSAndrew Lindesay * Copyright 2010-2023 Haiku Inc. All rights reserved. 3603e0bdfSNiels Sascha Reedijk * Distributed under the terms of the MIT License. 4603e0bdfSNiels Sascha Reedijk */ 5603e0bdfSNiels Sascha Reedijk #ifndef _B_HTTP_AUTHENTICATION_H_ 6603e0bdfSNiels Sascha Reedijk #define _B_HTTP_AUTHENTICATION_H_ 7603e0bdfSNiels Sascha Reedijk 8603e0bdfSNiels Sascha Reedijk 9603e0bdfSNiels Sascha Reedijk #include <Locker.h> 10603e0bdfSNiels Sascha Reedijk #include <String.h> 11603e0bdfSNiels Sascha Reedijk #include <Url.h> 12603e0bdfSNiels Sascha Reedijk 13ce64ffdbSNiels Sascha Reedijk 14ce64ffdbSNiels Sascha Reedijk namespace BPrivate { 15ce64ffdbSNiels Sascha Reedijk 16ce64ffdbSNiels Sascha Reedijk namespace Network { 1770b63f18SNiels Sascha Reedijk 18ce64ffdbSNiels Sascha Reedijk 19603e0bdfSNiels Sascha Reedijk // HTTP authentication method 20603e0bdfSNiels Sascha Reedijk enum BHttpAuthenticationMethod { 21603e0bdfSNiels Sascha Reedijk B_HTTP_AUTHENTICATION_NONE = 0, 22603e0bdfSNiels Sascha Reedijk // No authentication 23603e0bdfSNiels Sascha Reedijk B_HTTP_AUTHENTICATION_BASIC = 1, 24603e0bdfSNiels Sascha Reedijk // Basic base64 authentication method (unsecure) 25603e0bdfSNiels Sascha Reedijk B_HTTP_AUTHENTICATION_DIGEST = 2, 26603e0bdfSNiels Sascha Reedijk // Digest authentication 27*4b347fccSAndrew Lindesay B_HTTP_AUTHENTICATION_IE_DIGEST = 4, 28603e0bdfSNiels Sascha Reedijk // Slightly modified digest authentication to mimic old IE one 29*4b347fccSAndrew Lindesay B_HTTP_AUTHENTICATION_BEARER = 5 30*4b347fccSAndrew Lindesay // Bearer authentication used to convey a token 31603e0bdfSNiels Sascha Reedijk }; 32603e0bdfSNiels Sascha Reedijk 33603e0bdfSNiels Sascha Reedijk 34603e0bdfSNiels Sascha Reedijk enum BHttpAuthenticationAlgorithm { 35603e0bdfSNiels Sascha Reedijk B_HTTP_AUTHENTICATION_ALGORITHM_NONE, 36603e0bdfSNiels Sascha Reedijk B_HTTP_AUTHENTICATION_ALGORITHM_MD5, 37603e0bdfSNiels Sascha Reedijk B_HTTP_AUTHENTICATION_ALGORITHM_MD5_SESS 38603e0bdfSNiels Sascha Reedijk }; 39603e0bdfSNiels Sascha Reedijk 40603e0bdfSNiels Sascha Reedijk 41603e0bdfSNiels Sascha Reedijk enum BHttpAuthenticationQop { 42603e0bdfSNiels Sascha Reedijk B_HTTP_QOP_NONE, 43603e0bdfSNiels Sascha Reedijk B_HTTP_QOP_AUTH, 44603e0bdfSNiels Sascha Reedijk B_HTTP_QOP_AUTHINT 45603e0bdfSNiels Sascha Reedijk }; 46603e0bdfSNiels Sascha Reedijk 47603e0bdfSNiels Sascha Reedijk 48603e0bdfSNiels Sascha Reedijk class BHttpAuthentication { 49603e0bdfSNiels Sascha Reedijk public: 50603e0bdfSNiels Sascha Reedijk BHttpAuthentication(); 51603e0bdfSNiels Sascha Reedijk BHttpAuthentication(const BString& username, 52603e0bdfSNiels Sascha Reedijk const BString& password); 53603e0bdfSNiels Sascha Reedijk BHttpAuthentication( 54603e0bdfSNiels Sascha Reedijk const BHttpAuthentication& other); 55603e0bdfSNiels Sascha Reedijk BHttpAuthentication& operator=( 56603e0bdfSNiels Sascha Reedijk const BHttpAuthentication& other); 57603e0bdfSNiels Sascha Reedijk 58603e0bdfSNiels Sascha Reedijk // Field modification 59603e0bdfSNiels Sascha Reedijk void SetUserName(const BString& username); 60603e0bdfSNiels Sascha Reedijk void SetPassword(const BString& password); 61*4b347fccSAndrew Lindesay void SetToken(const BString& token); 62603e0bdfSNiels Sascha Reedijk void SetMethod( 63603e0bdfSNiels Sascha Reedijk BHttpAuthenticationMethod type); 64603e0bdfSNiels Sascha Reedijk status_t Initialize(const BString& wwwAuthenticate); 65603e0bdfSNiels Sascha Reedijk 66603e0bdfSNiels Sascha Reedijk // Field access 67603e0bdfSNiels Sascha Reedijk const BString& UserName() const; 68603e0bdfSNiels Sascha Reedijk const BString& Password() const; 69*4b347fccSAndrew Lindesay const BString& Token() const; 70603e0bdfSNiels Sascha Reedijk BHttpAuthenticationMethod Method() const; 71603e0bdfSNiels Sascha Reedijk 72603e0bdfSNiels Sascha Reedijk BString Authorization(const BUrl& url, 73603e0bdfSNiels Sascha Reedijk const BString& method) const; 74603e0bdfSNiels Sascha Reedijk 75603e0bdfSNiels Sascha Reedijk // Base64 encoding 76603e0bdfSNiels Sascha Reedijk // TODO: Move to a common place. We may have multiple implementations 77603e0bdfSNiels Sascha Reedijk // in the Haiku tree... 78603e0bdfSNiels Sascha Reedijk static BString Base64Encode(const BString& string); 79603e0bdfSNiels Sascha Reedijk static BString Base64Decode(const BString& string); 80603e0bdfSNiels Sascha Reedijk 81603e0bdfSNiels Sascha Reedijk 82603e0bdfSNiels Sascha Reedijk private: 83603e0bdfSNiels Sascha Reedijk BString _DigestResponse(const BString& uri, 84603e0bdfSNiels Sascha Reedijk const BString& method) const; 85603e0bdfSNiels Sascha Reedijk // TODO: Rename these? _H seems to return a hash value, 86603e0bdfSNiels Sascha Reedijk // _KD returns a hash value of the "data" prepended by 87603e0bdfSNiels Sascha Reedijk // the "secret" string... 88603e0bdfSNiels Sascha Reedijk BString _H(const BString& value) const; 89603e0bdfSNiels Sascha Reedijk BString _KD(const BString& secret, 90603e0bdfSNiels Sascha Reedijk const BString& data) const; 91603e0bdfSNiels Sascha Reedijk 92603e0bdfSNiels Sascha Reedijk private: 93603e0bdfSNiels Sascha Reedijk BHttpAuthenticationMethod fAuthenticationMethod; 94603e0bdfSNiels Sascha Reedijk BString fUserName; 95603e0bdfSNiels Sascha Reedijk BString fPassword; 96*4b347fccSAndrew Lindesay BString fToken; 97603e0bdfSNiels Sascha Reedijk 98603e0bdfSNiels Sascha Reedijk BString fRealm; 99603e0bdfSNiels Sascha Reedijk BString fDigestNonce; 100603e0bdfSNiels Sascha Reedijk mutable BString fDigestCnonce; 101603e0bdfSNiels Sascha Reedijk mutable int fDigestNc; 102603e0bdfSNiels Sascha Reedijk BString fDigestOpaque; 103603e0bdfSNiels Sascha Reedijk bool fDigestStale; 104603e0bdfSNiels Sascha Reedijk BHttpAuthenticationAlgorithm fDigestAlgorithm; 105603e0bdfSNiels Sascha Reedijk BHttpAuthenticationQop fDigestQop; 106603e0bdfSNiels Sascha Reedijk 107603e0bdfSNiels Sascha Reedijk BString fAuthorizationString; 108603e0bdfSNiels Sascha Reedijk 109603e0bdfSNiels Sascha Reedijk mutable BLocker fLock; 110603e0bdfSNiels Sascha Reedijk }; 111603e0bdfSNiels Sascha Reedijk 11270b63f18SNiels Sascha Reedijk 113ce64ffdbSNiels Sascha Reedijk } // namespace Network 114ce64ffdbSNiels Sascha Reedijk 115ce64ffdbSNiels Sascha Reedijk } // namespace BPrivate 11670b63f18SNiels Sascha Reedijk 117ce64ffdbSNiels Sascha Reedijk 118603e0bdfSNiels Sascha Reedijk #endif // _B_HTTP_AUTHENTICATION_H_ 119