aboutsummaryrefslogtreecommitdiff
path: root/Src/auth/Loginbox/loginCredentials.cpp
blob: 313e41d1dab669c71b9fc2a014cd5e3531eddbdb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "./loginCredentials.h"
#include "./common.h"

#include "../api_auth.h"


LoginCredentials::LoginCredentials(const GUID *pRealm, LPCWSTR pszName, LPCSTR pszSession, LPCSTR pszToken, __time64_t tExpire)
:	ref(1), username(NULL), sessionKey(NULL), token(NULL)
{
	realm = (NULL == pRealm) ? GUID_NULL : *pRealm;
	username = LoginBox_CopyString(pszName);
	sessionKey = LoginBox_CopyAnsiString(pszSession);
	token = LoginBox_CopyAnsiString(pszToken);
	expire = tExpire;
}

LoginCredentials::~LoginCredentials()
{
	LoginBox_FreeStringSecure(username);
	LoginBox_FreeAnsiStringSecure(sessionKey);
	LoginBox_FreeAnsiStringSecure(token);
}

HRESULT LoginCredentials::CreateInstance(const GUID *pRealm, LPCWSTR pszName, LPCSTR pszSession, LPCSTR pszToken, __time64_t tExpire, LoginCredentials **instance)
{
	if (NULL == instance) return E_POINTER;
	*instance = new LoginCredentials(pRealm, pszName, pszSession, pszToken, tExpire);
	if (NULL == *instance) return E_OUTOFMEMORY;
	return S_OK;
}

HRESULT LoginCredentials::CreateFromAuth(api_auth *authApi, const GUID *pRealm, LoginCredentials **instance)
{
	if (NULL == instance) return E_POINTER;
	*instance = NULL;

	if (NULL == authApi) return E_INVALIDARG;
	
	const size_t sessionKeyMax(8192), tokenMax(8192), usernameMax(8192);
	LPSTR sessionKey = LoginBox_MallocAnsiString(sessionKeyMax);
	LPSTR token = LoginBox_MallocAnsiString(tokenMax);
	LPWSTR username = LoginBox_MallocString(usernameMax);
	__time64_t expire;

	HRESULT hr;

	if (NULL == sessionKey || NULL == token || NULL == username)
		hr = E_OUTOFMEMORY;
	else
	{
		INT result = authApi->GetCredentials((NULL != pRealm) ? *pRealm : GUID_NULL, sessionKey, sessionKeyMax, token, tokenMax, username, usernameMax, &expire);
		if (AUTH_SUCCESS == result)
		{
			hr = CreateInstance(pRealm, username, sessionKey, token, expire, instance);
		}
		else
		{
			hr = E_FAIL;
		}
	}
	
	LoginBox_FreeAnsiStringSecure(sessionKey);
	LoginBox_FreeAnsiStringSecure(token);
	LoginBox_FreeStringSecure(username);

	return hr;
}

UINT LoginCredentials::AddRef()
{
	return InterlockedIncrement((LONG*)&ref);
}

UINT LoginCredentials::Release()
{
	if (0 == ref)
		return ref;
	
	LONG r = InterlockedDecrement((LONG*)&ref);
	if (0 == r)
		delete(this);
	
	return r;
}

GUID LoginCredentials::GetRealm()
{
	return realm;
}

__time64_t LoginCredentials::GetExpiration()
{
	return expire;
}

LPCWSTR LoginCredentials::GetUsername()
{
	return username;
}
LPCSTR LoginCredentials::GetSessionKey()
{
	return sessionKey;
}

LPCSTR LoginCredentials::GetToken()
{
	return token;
}