haiku/headers/private/netservices/HttpAuthentication.h
Andrew Lindesay 4b347fccb2 HaikuDepot: Token Based Authentication
This switches the application over from using basic
authentication to using token-based authentication in
preparation for later using Open-ID based
authentication flows. The application version is also
bumped in order that the server can detect this version
at some later date in the future when it no longer
supports basic authentication itself.

Change-Id: I7addde1d57503c58d6bcd54908f22f66830c0c59
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6944
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
2023-09-25 06:20:32 +00:00

119 lines
2.9 KiB
C++

/*
* Copyright 2010-2023 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _B_HTTP_AUTHENTICATION_H_
#define _B_HTTP_AUTHENTICATION_H_
#include <Locker.h>
#include <String.h>
#include <Url.h>
namespace BPrivate {
namespace Network {
// HTTP authentication method
enum BHttpAuthenticationMethod {
B_HTTP_AUTHENTICATION_NONE = 0,
// No authentication
B_HTTP_AUTHENTICATION_BASIC = 1,
// Basic base64 authentication method (unsecure)
B_HTTP_AUTHENTICATION_DIGEST = 2,
// Digest authentication
B_HTTP_AUTHENTICATION_IE_DIGEST = 4,
// Slightly modified digest authentication to mimic old IE one
B_HTTP_AUTHENTICATION_BEARER = 5
// Bearer authentication used to convey a token
};
enum BHttpAuthenticationAlgorithm {
B_HTTP_AUTHENTICATION_ALGORITHM_NONE,
B_HTTP_AUTHENTICATION_ALGORITHM_MD5,
B_HTTP_AUTHENTICATION_ALGORITHM_MD5_SESS
};
enum BHttpAuthenticationQop {
B_HTTP_QOP_NONE,
B_HTTP_QOP_AUTH,
B_HTTP_QOP_AUTHINT
};
class BHttpAuthentication {
public:
BHttpAuthentication();
BHttpAuthentication(const BString& username,
const BString& password);
BHttpAuthentication(
const BHttpAuthentication& other);
BHttpAuthentication& operator=(
const BHttpAuthentication& other);
// Field modification
void SetUserName(const BString& username);
void SetPassword(const BString& password);
void SetToken(const BString& token);
void SetMethod(
BHttpAuthenticationMethod type);
status_t Initialize(const BString& wwwAuthenticate);
// Field access
const BString& UserName() const;
const BString& Password() const;
const BString& Token() const;
BHttpAuthenticationMethod Method() const;
BString Authorization(const BUrl& url,
const BString& method) const;
// Base64 encoding
// TODO: Move to a common place. We may have multiple implementations
// in the Haiku tree...
static BString Base64Encode(const BString& string);
static BString Base64Decode(const BString& string);
private:
BString _DigestResponse(const BString& uri,
const BString& method) const;
// TODO: Rename these? _H seems to return a hash value,
// _KD returns a hash value of the "data" prepended by
// the "secret" string...
BString _H(const BString& value) const;
BString _KD(const BString& secret,
const BString& data) const;
private:
BHttpAuthenticationMethod fAuthenticationMethod;
BString fUserName;
BString fPassword;
BString fToken;
BString fRealm;
BString fDigestNonce;
mutable BString fDigestCnonce;
mutable int fDigestNc;
BString fDigestOpaque;
bool fDigestStale;
BHttpAuthenticationAlgorithm fDigestAlgorithm;
BHttpAuthenticationQop fDigestQop;
BString fAuthorizationString;
mutable BLocker fLock;
};
} // namespace Network
} // namespace BPrivate
#endif // _B_HTTP_AUTHENTICATION_H_