// wmsoapauth2.h : Declaration of the Cwmsoapauth2

#pragma once
#include "resource.h"       // main symbols

#include "wmsoapauthsvc.h"
#include "..\wmsoapmsg\wmsoapmsg.h"

#define MD5_HASH_SIZE 16

// Cwmsoapauth2

class ATL_NO_VTABLE Cwmsoapauth2 : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<Cwmsoapauth2, &CLSID_wmsoapauth2>,
	public IDispatchImpl<Iwmsoapauth2, &IID_Iwmsoapauth2, &LIBID_wmsoapauthsvcLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
	Cwmsoapauth2()
	{
	}

DECLARE_REGISTRY_RESOURCEID(IDR_WMSOAPAUTH2)


BEGIN_COM_MAP(Cwmsoapauth2)
	COM_INTERFACE_ENTRY(Iwmsoapauth2)
	COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()


	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}
	
	void FinalRelease() 
	{
	}

public:
	STDMETHOD(OnProcessHeader)(/*[in]*/ VARIANT SOAPMsgInterface, /*[out, retval]*/ VARIANT_BOOL *pResult);
	STDMETHOD(OnTestMUHeaderBlock)(/*[in]*/ BSTR bstrNamespace, /*[in]*/ BSTR bstrLocalName, /*[out, retval]*/ VARIANT_BOOL *pResult);

private:
	bool Authenticate(Iwmsoapmsg2 *pIwmsoapmsg);
	bool Challenge(Iwmsoapmsg2 *pIwmsoapmsg);
	bool InitChallenge(Iwmsoapmsg2 *pIwmsoapmsg);
	bool Response(Iwmsoapmsg2 *pIwmsoapmsg);
	bool ValidateRealmUser(Iwmsoapmsg2 *pIwmsoapmsg);
	bool NewNonce(WMNonce *pNonce);
	bool CalcDigest(void *pBuffer);
	bool CheckNonceAging();
	// auth string storage
	unsigned char	m_Auth[MD5_HASH_SIZE];
	bool	m_bAuthFound;
	// status code for response header
	string	m_strAuthStatus;
	// client nonce
	string	m_strCN;
	bool	m_bCNFound;
	// server nonce
	string	m_strSN;
	bool	m_bSNFound;
	// client id
	string	m_strUserID;
	bool	m_bUserIDFound;
	// realm
	string	m_strRealm;
	bool	m_bRealmFound;
	// user account key
	string	m_strAccount;
	// authentication state flag
	bool	m_bAuthSucceeded;
};

OBJECT_ENTRY_AUTO(__uuidof(wmsoapauth2), Cwmsoapauth2)
