2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-05-12 01:04:09 +04:00
|
|
|
|
|
|
|
#ifndef nsHttpAuthCache_h__
|
|
|
|
#define nsHttpAuthCache_h__
|
|
|
|
|
|
|
|
#include "nsError.h"
|
2009-02-23 04:05:28 +03:00
|
|
|
#include "nsTArray.h"
|
2018-03-03 02:04:25 +03:00
|
|
|
#include "nsClassHashtable.h"
|
2001-12-05 02:49:06 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2018-03-03 02:04:25 +03:00
|
|
|
#include "nsHashKeys.h"
|
2017-08-17 02:48:52 +03:00
|
|
|
#include "nsStringFwd.h"
|
2012-12-05 11:33:20 +04:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
|
2014-01-08 02:05:56 +04:00
|
|
|
namespace mozilla {
|
2015-10-29 12:43:00 +03:00
|
|
|
|
|
|
|
class OriginAttributesPattern;
|
|
|
|
|
2014-01-08 02:05:56 +04:00
|
|
|
namespace net {
|
|
|
|
|
2003-05-19 21:47:22 +04:00
|
|
|
struct nsHttpAuthPath {
|
|
|
|
struct nsHttpAuthPath* mNext;
|
|
|
|
char mPath[1];
|
|
|
|
};
|
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpAuthIdentity
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpAuthIdentity {
|
|
|
|
public:
|
|
|
|
nsHttpAuthIdentity() : mUser(nullptr), mPass(nullptr), mDomain(nullptr) {}
|
2018-04-13 16:01:28 +03:00
|
|
|
nsHttpAuthIdentity(const char16_t* domain, const char16_t* user,
|
|
|
|
const char16_t* password)
|
2018-06-14 11:19:07 +03:00
|
|
|
: mUser(nullptr), mPass{nullptr}, mDomain{nullptr} {
|
2018-04-13 16:01:28 +03:00
|
|
|
DebugOnly<nsresult> rv = Set(domain, user, password);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2003-03-26 08:05:49 +03:00
|
|
|
}
|
|
|
|
~nsHttpAuthIdentity() { Clear(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* Domain() const { return mDomain; }
|
|
|
|
const char16_t* User() const { return mUser; }
|
|
|
|
const char16_t* Password() const { return mPass; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-04-18 09:37:32 +03:00
|
|
|
[[nodiscard]] nsresult Set(const char16_t* domain, const char16_t* user,
|
|
|
|
const char16_t* password);
|
|
|
|
[[nodiscard]] nsresult Set(const nsHttpAuthIdentity& other) {
|
2016-12-27 10:22:07 +03:00
|
|
|
return Set(other.mDomain, other.mUser, other.mPass);
|
|
|
|
}
|
2003-03-26 08:05:49 +03:00
|
|
|
void Clear();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Equals(const nsHttpAuthIdentity& other) const;
|
|
|
|
bool IsEmpty() const { return !mUser; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
private:
|
|
|
|
// allocated as one contiguous blob, starting at mUser.
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* mUser;
|
|
|
|
char16_t* mPass;
|
|
|
|
char16_t* mDomain;
|
2003-03-26 08:05:49 +03:00
|
|
|
};
|
|
|
|
|
2001-12-05 02:49:06 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpAuthEntry
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpAuthEntry {
|
|
|
|
public:
|
2003-03-26 08:05:49 +03:00
|
|
|
const char* Realm() const { return mRealm; }
|
|
|
|
const char* Creds() const { return mCreds; }
|
|
|
|
const char* Challenge() const { return mChallenge; }
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* Domain() const { return mIdent.Domain(); }
|
|
|
|
const char16_t* User() const { return mIdent.User(); }
|
|
|
|
const char16_t* Pass() const { return mIdent.Password(); }
|
2003-05-19 21:47:22 +04:00
|
|
|
nsHttpAuthPath* RootPath() { return mRoot; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
const nsHttpAuthIdentity& Identity() const { return mIdent; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-04-18 09:37:32 +03:00
|
|
|
[[nodiscard]] nsresult AddPath(const char* aPath);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
nsCOMPtr<nsISupports> mMetaData;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-12-05 02:49:06 +03:00
|
|
|
private:
|
2018-04-13 16:01:28 +03:00
|
|
|
nsHttpAuthEntry(const char* path, const char* realm, const char* creds,
|
|
|
|
const char* challenge, const nsHttpAuthIdentity* ident,
|
|
|
|
nsISupports* metadata)
|
|
|
|
: mRoot(nullptr),
|
|
|
|
mTail(nullptr),
|
|
|
|
mRealm(nullptr),
|
2018-06-14 11:19:07 +03:00
|
|
|
mCreds{nullptr},
|
|
|
|
mChallenge{nullptr} {
|
2018-04-13 16:01:28 +03:00
|
|
|
DebugOnly<nsresult> rv =
|
|
|
|
Set(path, realm, creds, challenge, ident, metadata);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2003-03-26 08:05:49 +03:00
|
|
|
}
|
2001-12-05 02:49:06 +03:00
|
|
|
~nsHttpAuthEntry();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-04-18 09:37:32 +03:00
|
|
|
[[nodiscard]] nsresult Set(const char* path, const char* realm,
|
|
|
|
const char* creds, const char* challenge,
|
|
|
|
const nsHttpAuthIdentity* ident,
|
|
|
|
nsISupports* metadata);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
nsHttpAuthIdentity mIdent;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-05-19 21:47:22 +04:00
|
|
|
nsHttpAuthPath* mRoot; // root pointer
|
|
|
|
nsHttpAuthPath* mTail; // tail pointer
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-05-19 21:47:22 +04:00
|
|
|
// allocated together in one blob, starting with mRealm.
|
2003-03-26 08:05:49 +03:00
|
|
|
char* mRealm;
|
|
|
|
char* mCreds;
|
|
|
|
char* mChallenge;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-12-05 02:49:06 +03:00
|
|
|
friend class nsHttpAuthNode;
|
2003-03-26 08:05:49 +03:00
|
|
|
friend class nsHttpAuthCache;
|
2020-02-11 19:20:00 +03:00
|
|
|
friend class mozilla::DefaultDelete<nsHttpAuthEntry>; // needs to call the
|
|
|
|
// destructor
|
2001-12-05 02:49:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpAuthNode
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpAuthNode {
|
|
|
|
private:
|
2020-02-11 19:20:00 +03:00
|
|
|
using EntryList = nsTArray<UniquePtr<nsHttpAuthEntry>>;
|
|
|
|
|
2001-12-05 02:49:06 +03:00
|
|
|
nsHttpAuthNode();
|
|
|
|
~nsHttpAuthNode();
|
|
|
|
|
|
|
|
// path can be null, in which case we'll search for an entry
|
|
|
|
// with a null path.
|
2003-03-26 08:05:49 +03:00
|
|
|
nsHttpAuthEntry* LookupEntryByPath(const char* path);
|
2001-12-05 02:49:06 +03:00
|
|
|
|
|
|
|
// realm must not be null
|
2003-03-26 08:05:49 +03:00
|
|
|
nsHttpAuthEntry* LookupEntryByRealm(const char* realm);
|
2020-02-11 19:20:00 +03:00
|
|
|
EntryList::const_iterator LookupEntryItrByRealm(const char* realm) const;
|
2001-12-05 02:49:06 +03:00
|
|
|
|
|
|
|
// if a matching entry is found, then credentials will be changed.
|
2020-04-18 09:37:32 +03:00
|
|
|
[[nodiscard]] nsresult SetAuthEntry(const char* path, const char* realm,
|
|
|
|
const char* credentials,
|
|
|
|
const char* challenge,
|
|
|
|
const nsHttpAuthIdentity* ident,
|
|
|
|
nsISupports* metadata);
|
2001-12-05 02:49:06 +03:00
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
void ClearAuthEntry(const char* realm);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t EntryCount() { return mList.Length(); }
|
2001-12-05 02:49:06 +03:00
|
|
|
|
|
|
|
private:
|
2020-02-11 19:20:00 +03:00
|
|
|
EntryList mList;
|
2001-12-05 02:49:06 +03:00
|
|
|
|
|
|
|
friend class nsHttpAuthCache;
|
2020-01-13 22:18:56 +03:00
|
|
|
friend class mozilla::DefaultDelete<nsHttpAuthNode>; // needs to call the
|
|
|
|
// destructor
|
2001-12-05 02:49:06 +03:00
|
|
|
};
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpAuthCache
|
2001-12-05 02:49:06 +03:00
|
|
|
// (holds a hash table from host:port to nsHttpAuthNode)
|
2001-05-12 01:04:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpAuthCache {
|
|
|
|
public:
|
|
|
|
nsHttpAuthCache();
|
|
|
|
~nsHttpAuthCache();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-04-08 03:34:35 +04:00
|
|
|
// |scheme|, |host|, and |port| are required
|
2001-12-05 02:49:06 +03:00
|
|
|
// |path| can be null
|
|
|
|
// |entry| is either null or a weak reference
|
2020-04-18 09:37:32 +03:00
|
|
|
[[nodiscard]] nsresult GetAuthEntryForPath(const char* scheme,
|
|
|
|
const char* host, int32_t port,
|
|
|
|
const char* path,
|
|
|
|
nsACString const& originSuffix,
|
|
|
|
nsHttpAuthEntry** entry);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-04-08 03:34:35 +04:00
|
|
|
// |scheme|, |host|, and |port| are required
|
2001-12-05 02:49:06 +03:00
|
|
|
// |realm| must not be null
|
|
|
|
// |entry| is either null or a weak reference
|
2020-04-18 09:37:32 +03:00
|
|
|
[[nodiscard]] nsresult GetAuthEntryForDomain(const char* scheme,
|
|
|
|
const char* host, int32_t port,
|
|
|
|
const char* realm,
|
|
|
|
nsACString const& originSuffix,
|
|
|
|
nsHttpAuthEntry** entry);
|
2001-12-05 02:49:06 +03:00
|
|
|
|
2004-04-08 03:34:35 +04:00
|
|
|
// |scheme|, |host|, and |port| are required
|
2001-12-05 02:49:06 +03:00
|
|
|
// |path| can be null
|
|
|
|
// |realm| must not be null
|
|
|
|
// if |credentials|, |user|, |pass|, and |challenge| are each
|
|
|
|
// null, then the entry is deleted.
|
2020-04-18 09:37:32 +03:00
|
|
|
[[nodiscard]] nsresult SetAuthEntry(
|
|
|
|
const char* scheme, const char* host, int32_t port, const char* directory,
|
|
|
|
const char* realm, const char* credentials, const char* challenge,
|
|
|
|
nsACString const& originSuffix, const nsHttpAuthIdentity* ident,
|
|
|
|
nsISupports* metadata);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-04-08 03:34:35 +04:00
|
|
|
void ClearAuthEntry(const char* scheme, const char* host, int32_t port,
|
2015-10-29 12:43:00 +03:00
|
|
|
const char* realm, nsACString const& originSuffix);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-05-16 17:30:42 +04:00
|
|
|
// expire all existing auth list entries including proxy auths.
|
2019-12-02 16:21:06 +03:00
|
|
|
void ClearAll();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-06-10 17:44:08 +03:00
|
|
|
// For testing only.
|
|
|
|
void CollectKeys(nsTArray<nsCString>& aValue);
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
private:
|
2004-04-08 03:34:35 +04:00
|
|
|
nsHttpAuthNode* LookupAuthNode(const char* scheme, const char* host,
|
2015-10-29 12:43:00 +03:00
|
|
|
int32_t port, nsACString const& originSuffix,
|
2004-04-08 03:34:35 +04:00
|
|
|
nsCString& key);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-29 12:43:00 +03:00
|
|
|
class OriginClearObserver : public nsIObserver {
|
2018-04-30 19:46:04 +03:00
|
|
|
virtual ~OriginClearObserver() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-12-05 11:33:20 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
2015-10-29 12:43:00 +03:00
|
|
|
explicit OriginClearObserver(nsHttpAuthCache* aOwner) : mOwner(aOwner) {}
|
2012-12-05 11:33:20 +04:00
|
|
|
nsHttpAuthCache* mOwner;
|
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-29 12:43:00 +03:00
|
|
|
void ClearOriginData(OriginAttributesPattern const& pattern);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
private:
|
2018-03-03 02:04:25 +03:00
|
|
|
using AuthNodeTable = nsClassHashtable<nsCStringHashKey, nsHttpAuthNode>;
|
|
|
|
AuthNodeTable mDB; // "host:port" --> nsHttpAuthNode
|
2015-10-29 12:43:00 +03:00
|
|
|
RefPtr<OriginClearObserver> mObserver;
|
2001-05-12 01:04:09 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
2014-01-08 02:05:56 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
#endif // nsHttpAuthCache_h__
|