aboutsummaryrefslogtreecommitdiff
path: root/Src/auth/Loginbox/commandWebAuth.cpp
blob: 487ba748e9d12e20f4d5f03d6ad9a401d049ff71 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "./commandWebAuth.h"
#include "./resultWebAuth.h"
#include "./common.h"

#include "../api.h"

#include "../../omBrowser/obj_ombrowser.h"
#include <api/service/waservicefactory.h>

LoginCommandWebAuth::LoginCommandWebAuth() 
	: ref(1), targetUrl(NULL)
{
	
}

LoginCommandWebAuth::~LoginCommandWebAuth()
{
	LoginBox_FreeString(targetUrl);
}

HRESULT LoginCommandWebAuth::CreateInstance(LoginCommandWebAuth **instance)
{
	if (NULL == instance) return E_POINTER;
	*instance = new LoginCommandWebAuth();
	if (NULL == *instance) return E_OUTOFMEMORY;
	return S_OK;
}

ULONG LoginCommandWebAuth::AddRef()
{
	return InterlockedIncrement((LONG*)&ref);
}

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

HRESULT LoginCommandWebAuth::GetType(GUID *commandUid)
{
	if (NULL == commandUid) return E_INVALIDARG;
	*commandUid = LCUID_WEBAUTH;
	return S_OK;
}

HRESULT LoginCommandWebAuth::SetParameter(LPCWSTR pszKey, LPCWSTR pszValue)
{
	if (CSTR_EQUAL == CompareString(CSTR_INVARIANT, NORM_IGNORECASE, pszKey, -1, L"url", -1))
	{
		LoginBox_FreeString(targetUrl);
		targetUrl = LoginBox_CopyString(pszValue);
	}
	return S_OK;
}

HRESULT LoginCommandWebAuth::IsValid()
{
	if (NULL == targetUrl || L'\0' == *targetUrl)
		return S_FALSE;

	HRESULT hr = S_FALSE;
	
	waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(OBJ_OmBrowser);
	if (NULL != sf)
	{
		obj_ombrowser *browserMngr = (obj_ombrowser*)sf->getInterface();
		if (NULL != browserMngr) 
		{
			hr = S_OK;
			browserMngr->Release();
		}
		sf->Release();
	}
	return hr;
}

HRESULT LoginCommandWebAuth::IsIdentical(LoginCommand *test)
{
	if (NULL == test)
		return E_INVALIDARG;

	GUID typeId;
	if (FAILED(test->GetType(&typeId)) || FALSE == IsEqualGUID(LCUID_WEBAUTH, typeId))
		return S_FALSE;
	
	LoginCommandWebAuth *testWeb = (LoginCommandWebAuth*)test;

	if(S_OK != LoginBox_IsStrEqInvI(targetUrl, testWeb->targetUrl))
		return S_FALSE;
	
	return S_OK;
}

HRESULT LoginCommandWebAuth::BeginLogin(LoginData *data, LoginResult::Callback callback, void *user, LoginResult **result)
{
	HRESULT hr;
	LoginResultWebAuth *webAuth;

	hr = LoginResultWebAuth::CreateInstance(targetUrl, data, callback, user, &webAuth);
	
	if (SUCCEEDED(hr))
	{
		if (NULL != result)
			*result = webAuth;
		else
			webAuth->Release();
	}
	else
	{
		if (NULL != result) 
			*result = NULL;
	}

	return hr;
}

HRESULT LoginCommandWebAuth::EndLogin(LoginResult *result, INT *authCode, LoginCredentials **credentials)
{
	if (NULL == result)
		return E_INVALIDARG;

	HRESULT hr = result->IsCompleted();
	if (S_OK != hr)
	{
		HANDLE completed;
		hr = result->GetWaitHandle(&completed);
		if (SUCCEEDED(hr))
		{
			WaitForSingleObjectEx(completed, INFINITE, TRUE);
			CloseHandle(completed);
		}
	}
	
	if (SUCCEEDED(hr))
	{
		LoginResultWebAuth *webAuth;
		hr = result->QueryInterface(LCUID_WEBAUTH, (void**)&webAuth);
		if(SUCCEEDED(hr))
		{
			hr = webAuth->GetResult(authCode, credentials);
			webAuth->Release();
		}
	}

	return hr;
}

HRESULT LoginCommandWebAuth::RequestAbort(LoginResult *result, BOOL drop)
{
	if (NULL == result) return E_INVALIDARG;
	return result->RequestAbort(drop);
}