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