1 /* 2 * Copyright 2010-2014 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _B_HTTP_AUTHENTICATION_H_ 6 #define _B_HTTP_AUTHENTICATION_H_ 7 8 9 #include <Locker.h> 10 #include <String.h> 11 #include <Url.h> 12 13 14 #ifndef LIBNETAPI_DEPRECATED 15 namespace BPrivate { 16 17 namespace Network { 18 #endif 19 20 // HTTP authentication method 21 enum BHttpAuthenticationMethod { 22 B_HTTP_AUTHENTICATION_NONE = 0, 23 // No authentication 24 B_HTTP_AUTHENTICATION_BASIC = 1, 25 // Basic base64 authentication method (unsecure) 26 B_HTTP_AUTHENTICATION_DIGEST = 2, 27 // Digest authentication 28 B_HTTP_AUTHENTICATION_IE_DIGEST = 4 29 // Slightly modified digest authentication to mimic old IE one 30 }; 31 32 33 enum BHttpAuthenticationAlgorithm { 34 B_HTTP_AUTHENTICATION_ALGORITHM_NONE, 35 B_HTTP_AUTHENTICATION_ALGORITHM_MD5, 36 B_HTTP_AUTHENTICATION_ALGORITHM_MD5_SESS 37 }; 38 39 40 enum BHttpAuthenticationQop { 41 B_HTTP_QOP_NONE, 42 B_HTTP_QOP_AUTH, 43 B_HTTP_QOP_AUTHINT 44 }; 45 46 47 class BHttpAuthentication { 48 public: 49 BHttpAuthentication(); 50 BHttpAuthentication(const BString& username, 51 const BString& password); 52 BHttpAuthentication( 53 const BHttpAuthentication& other); 54 BHttpAuthentication& operator=( 55 const BHttpAuthentication& other); 56 57 // Field modification 58 void SetUserName(const BString& username); 59 void SetPassword(const BString& password); 60 void SetMethod( 61 BHttpAuthenticationMethod type); 62 status_t Initialize(const BString& wwwAuthenticate); 63 64 // Field access 65 const BString& UserName() const; 66 const BString& Password() const; 67 BHttpAuthenticationMethod Method() const; 68 69 BString Authorization(const BUrl& url, 70 const BString& method) const; 71 72 // Base64 encoding 73 // TODO: Move to a common place. We may have multiple implementations 74 // in the Haiku tree... 75 static BString Base64Encode(const BString& string); 76 static BString Base64Decode(const BString& string); 77 78 79 private: 80 BString _DigestResponse(const BString& uri, 81 const BString& method) const; 82 // TODO: Rename these? _H seems to return a hash value, 83 // _KD returns a hash value of the "data" prepended by 84 // the "secret" string... 85 BString _H(const BString& value) const; 86 BString _KD(const BString& secret, 87 const BString& data) const; 88 89 private: 90 BHttpAuthenticationMethod fAuthenticationMethod; 91 BString fUserName; 92 BString fPassword; 93 94 BString fRealm; 95 BString fDigestNonce; 96 mutable BString fDigestCnonce; 97 mutable int fDigestNc; 98 BString fDigestOpaque; 99 bool fDigestStale; 100 BHttpAuthenticationAlgorithm fDigestAlgorithm; 101 BHttpAuthenticationQop fDigestQop; 102 103 BString fAuthorizationString; 104 105 mutable BLocker fLock; 106 }; 107 108 #ifndef LIBNETAPI_DEPRECATED 109 } // namespace Network 110 111 } // namespace BPrivate 112 #endif 113 114 #endif // _B_HTTP_AUTHENTICATION_H_ 115