-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCSampleProvider.cpp
More file actions
329 lines (297 loc) · 10.3 KB
/
CSampleProvider.cpp
File metadata and controls
329 lines (297 loc) · 10.3 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// CSampleProvider implements ICredentialProvider, which is the main
// interface that logonUI uses to decide which tiles to display.
// In this sample, we will display one tile that uses each of the nine
// available UI controls.
#include <initguid.h>
#include <string>
#include <tuple>
#include "CSampleProvider.h"
#include "CSampleCredential.h"
#include "guid.h"
#include "locker_app.h"
#include <iostream>
#include "util.h"
CSampleProvider::CSampleProvider():
_cRef(1),
_pCredential(nullptr),
_pCredProviderUserArray(nullptr),
_upAdviseContext(0),
_cred_provider_events(nullptr),
_is_auto_login(FALSE)
{
DllAddRef();
}
CSampleProvider::~CSampleProvider()
{
if (_pCredential != nullptr)
{
_pCredential->Release();
_pCredential = nullptr;
}
if (_pCredProviderUserArray != nullptr)
{
_pCredProviderUserArray->Release();
_pCredProviderUserArray = nullptr;
}
DllRelease();
}
// SetUsageScenario is the provider's cue that it's going to be asked for tiles
// in a subsequent call.
HRESULT CSampleProvider::SetUsageScenario(
CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus,
DWORD /*dwFlags*/)
{
HRESULT hr;
// Decide which scenarios to support here. Returning E_NOTIMPL simply tells the caller
// that we're not designed for that scenario.
switch (cpus)
{
case CPUS_LOGON:
case CPUS_UNLOCK_WORKSTATION:
// The reason why we need _fRecreateEnumeratedCredentials is because ICredentialProviderSetUserArray::SetUserArray() is called after ICredentialProvider::SetUsageScenario(),
// while we need the ICredentialProviderUserArray during enumeration in ICredentialProvider::GetCredentialCount()
_cpus = cpus;
_fRecreateEnumeratedCredentials = true;
hr = S_OK;
break;
case CPUS_CHANGE_PASSWORD:
case CPUS_CREDUI:
hr = E_NOTIMPL;
break;
default:
hr = E_INVALIDARG;
break;
}
return hr;
}
// SetSerialization takes the kind of buffer that you would normally return to LogonUI for
// an authentication attempt. It's the opposite of ICredentialProviderCredential::GetSerialization.
// GetSerialization is implement by a credential and serializes that credential. Instead,
// SetSerialization takes the serialization and uses it to create a tile.
//
// SetSerialization is called for two main scenarios. The first scenario is in the credui case
// where it is prepopulating a tile with credentials that the user chose to store in the OS.
// The second situation is in a remote logon case where the remote client may wish to
// prepopulate a tile with a username, or in some cases, completely populate the tile and
// use it to logon without showing any UI.
//
// If you wish to see an example of SetSerialization, please see either the SampleCredentialProvider
// sample or the SampleCredUICredentialProvider sample. [The logonUI team says, "The original sample that
// this was built on top of didn't have SetSerialization. And when we decided SetSerialization was
// important enough to have in the sample, it ended up being a non-trivial amount of work to integrate
// it into the main sample. We felt it was more important to get these samples out to you quickly than to
// hold them in order to do the work to integrate the SetSerialization changes from SampleCredentialProvider
// into this sample.]
HRESULT CSampleProvider::SetSerialization(
_In_ CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION const * /*pcpcs*/)
{
return E_NOTIMPL;
}
// Called by LogonUI to give you a callback. Providers often use the callback if they
// some event would cause them to need to change the set of tiles that they enumerated.
HRESULT CSampleProvider::Advise(
_In_ ICredentialProviderEvents * pcpe,
_In_ UINT_PTR upAdviseContext)
{
odprintf("CSampleProvider::Advise \n");
// init socket communication thread here
init_locker_thread(this);
this->_cred_provider_events = pcpe;
this->_upAdviseContext = upAdviseContext;
return E_NOTIMPL;
}
// Called by LogonUI when the ICredentialProviderEvents callback is no longer valid.
HRESULT CSampleProvider::UnAdvise()
{
odprintf("CSampleProvider::UnAdvise \n");
deinit_locker_thread();
this->_cred_provider_events = nullptr;
this->_upAdviseContext = NULL;
return E_NOTIMPL;
}
// Called by LogonUI to determine the number of fields in your tiles. This
// does mean that all your tiles must have the same number of fields.
// This number must include both visible and invisible fields. If you want a tile
// to have different fields from the other tiles you enumerate for a given usage
// scenario you must include them all in this count and then hide/show them as desired
// using the field descriptors.
HRESULT CSampleProvider::GetFieldDescriptorCount(
_Out_ DWORD *pdwCount)
{
*pdwCount = SFI_NUM_FIELDS;
return S_OK;
}
// Gets the field descriptor for a particular field.
HRESULT CSampleProvider::GetFieldDescriptorAt(
DWORD dwIndex,
_Outptr_result_nullonfailure_ CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR **ppcpfd)
{
HRESULT hr;
*ppcpfd = nullptr;
// Verify dwIndex is a valid field.
if ((dwIndex < SFI_NUM_FIELDS) && ppcpfd)
{
hr = FieldDescriptorCoAllocCopy(s_rgCredProvFieldDescriptors[dwIndex], ppcpfd);
}
else
{
hr = E_INVALIDARG;
}
return hr;
}
// Sets pdwCount to the number of tiles that we wish to show at this time.
// Sets pdwDefault to the index of the tile which should be used as the default.
// The default tile is the tile which will be shown in the zoomed view by default. If
// more than one provider specifies a default the last used cred prov gets to pick
// the default. If *pbAutoLogonWithDefault is TRUE, LogonUI will immediately call
// GetSerialization on the credential you've specified as the default and will submit
// that credential for authentication without showing any further UI.
HRESULT CSampleProvider::GetCredentialCount(
_Out_ DWORD *pdwCount,
_Out_ DWORD *pdwDefault,
_Out_ BOOL *pbAutoLogonWithDefault)
{
*pdwDefault = CREDENTIAL_PROVIDER_NO_DEFAULT;
*pbAutoLogonWithDefault = FALSE;
PCWSTR pezPassword = nullptr;
std::wstring temp;
odprintf("CSampleProvider::GetCredentialCount \n");
if (_is_auto_login && !this->received_password.empty())
{
*pbAutoLogonWithDefault = TRUE;
*pdwDefault = 0; //Now just support one credential
temp = s2ws(this->received_password);
pezPassword = temp.c_str();
}
if (_fRecreateEnumeratedCredentials)
{
_fRecreateEnumeratedCredentials = false;
_ReleaseEnumeratedCredentials();
_CreateEnumeratedCredentials(pezPassword);
}
_is_auto_login = FALSE;
this->received_password.clear();
*pdwCount = 1;
return S_OK;
}
// Returns the credential at the index specified by dwIndex. This function is called by logonUI to enumerate
// the tiles.
HRESULT CSampleProvider::GetCredentialAt(
DWORD dwIndex,
_Outptr_result_nullonfailure_ ICredentialProviderCredential **ppcpc)
{
HRESULT hr = E_INVALIDARG;
*ppcpc = nullptr;
odprintf(" CSampleProvider::GetCredentialAt \n");
if ((dwIndex == 0) && ppcpc)
{
hr = _pCredential->QueryInterface(IID_PPV_ARGS(ppcpc));
}
return hr;
}
// This function will be called by LogonUI after SetUsageScenario succeeds.
// Sets the User Array with the list of users to be enumerated on the logon screen.
HRESULT CSampleProvider::SetUserArray(_In_ ICredentialProviderUserArray *users)
{
if (_pCredProviderUserArray)
{
_pCredProviderUserArray->Release();
}
_pCredProviderUserArray = users;
_pCredProviderUserArray->AddRef();
return S_OK;
}
void CSampleProvider::_CreateEnumeratedCredentials(_In_ PCWSTR pezPassword)
{
switch (_cpus)
{
case CPUS_LOGON:
case CPUS_UNLOCK_WORKSTATION:
{
OutputDebugString(L"CSampleProvider::_CreateEnumeratedCredentials");
_EnumerateCredentials(pezPassword);
break;
}
default:
break;
}
}
void CSampleProvider::_ReleaseEnumeratedCredentials()
{
if (_pCredential != nullptr)
{
_pCredential->Release();
_pCredential = nullptr;
}
}
HRESULT CSampleProvider::_EnumerateCredentials(_In_ PCWSTR pezPassword)
{
HRESULT hr = E_UNEXPECTED;
if (_pCredProviderUserArray != nullptr)
{
DWORD dwUserCount;
_pCredProviderUserArray->GetCount(&dwUserCount);
if (dwUserCount > 0)
{
ICredentialProviderUser *pCredUser;
hr = _pCredProviderUserArray->GetAt(0, &pCredUser);
if (SUCCEEDED(hr))
{
_pCredential = new(std::nothrow) CSampleCredential();
if (_pCredential != nullptr)
{
OutputDebugString(L"CSampleProvider::_EnumerateCredentials");
hr = _pCredential->Initialize(_cpus, s_rgCredProvFieldDescriptors, s_rgFieldStatePairs, pCredUser, pezPassword);
if (FAILED(hr))
{
_pCredential->Release();
_pCredential = nullptr;
}
}
else
{
hr = E_OUTOFMEMORY;
}
pCredUser->Release();
}
}
}
return hr;
}
HRESULT CSampleProvider::_ReEnumerateOneCredentials(
_In_ std::string Username,
_In_ std::string Password,
_In_ std::string Domain
)
{
HRESULT hr = NOERROR;
odprintf("CSampleProvider::_ReEnumerateOneCredentials \n");
this->_fRecreateEnumeratedCredentials = true;
this->received_password = Password;
this->_is_auto_login = TRUE;
return hr;
}
// Boilerplate code to create our provider.
HRESULT CSample_CreateInstance(_In_ REFIID riid, _Outptr_ void **ppv)
{
HRESULT hr;
CSampleProvider *pProvider = new(std::nothrow) CSampleProvider();
if (pProvider)
{
hr = pProvider->QueryInterface(riid, ppv);
pProvider->Release();
}
else
{
hr = E_OUTOFMEMORY;
}
return hr;
}