-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSampleProvider.cpp
371 lines (333 loc) · 12.3 KB
/
CSampleProvider.cpp
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//
// 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 are wrapping the default password provider with
// an extra small text and combobox. We pass nearly all requests to the
// wrapped provider, except for the ones that are for fields we're
// responsible for ourselves. As far as the owner is concerned, we are a
// unique provider, so they never know we're wrapping another provider.
#include <credentialprovider.h>
#include "CSampleProvider.h"
#include "CSampleCredential.h"
#include "guid.h"
// CSampleProvider ////////////////////////////////////////////////////////
CSampleProvider::CSampleProvider():
_cRef(1)
{
DllAddRef();
_rgpCredentials = NULL;
_dwCredentialCount = 0;
_pWrappedProvider = NULL;
_dwWrappedDescriptorCount = 0;
}
CSampleProvider::~CSampleProvider()
{
_CleanUpAllCredentials();
if (_pWrappedProvider)
{
_pWrappedProvider->Release();
}
DllRelease();
}
// Cleans up all credentials, including the memory used to allocate the array.
void CSampleProvider::_CleanUpAllCredentials()
{
// Iterate and clean up the array, if it exists.
if (_rgpCredentials != NULL)
{
for (DWORD lcv = 0; lcv < _dwCredentialCount; lcv++)
{
if (_rgpCredentials[lcv] != NULL)
{
_rgpCredentials[lcv]->Release();
_rgpCredentials[lcv] = NULL;
}
}
delete [] _rgpCredentials;
_rgpCredentials = NULL;
}
}
// Ordinarily we would look at the CPUS and decide whether or not we support this scenario.
// However, in this scenario we're going to create our internal provider and let it answer
// questions like this for us.
HRESULT CSampleProvider::SetUsageScenario(
__in CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus,
__in DWORD dwFlags
)
{
HRESULT hr;
// Create the password credential provider and query its interface for an
// ICredentialProvider we can use. Once it's up and running, ask it about the
// usage scenario being provided.
IUnknown *pUnknown = NULL;
hr = CoCreateInstance(CLSID_PasswordCredentialProvider, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pUnknown));
if (SUCCEEDED(hr))
{
hr = pUnknown->QueryInterface(IID_PPV_ARGS(&(_pWrappedProvider)));
if (SUCCEEDED(hr))
{
hr = _pWrappedProvider->SetUsageScenario(cpus, dwFlags);
}
pUnknown->Release();
}
if (FAILED(hr))
{
if (_pWrappedProvider != NULL)
{
_pWrappedProvider->Release();
_pWrappedProvider = NULL;
}
}
return hr;
}
// We pass this along to the wrapped provider.
HRESULT CSampleProvider::SetSerialization(
__in const CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION* pcpcs
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedProvider != NULL)
{
hr = _pWrappedProvider->SetSerialization(pcpcs);
}
return hr;
}
// Called by LogonUI to give you a callback. We pass this along to the wrapped provider.
HRESULT CSampleProvider::Advise(
__in ICredentialProviderEvents* pcpe,
__in UINT_PTR upAdviseContext
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedProvider != NULL)
{
hr = _pWrappedProvider->Advise(pcpe, upAdviseContext);
}
return hr;
}
// Called by LogonUI when the ICredentialProviderEvents callback is no longer valid.
// We pass this along to the wrapped provider.
HRESULT CSampleProvider::UnAdvise()
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedProvider != NULL)
{
hr = _pWrappedProvider->UnAdvise();
}
return hr;
}
// 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. We pass this along to the wrapped provider and then append
// our own credential count.
HRESULT CSampleProvider::GetFieldDescriptorCount(
__out DWORD* pdwCount
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedProvider != NULL)
{
hr = _pWrappedProvider->GetFieldDescriptorCount(&(_dwWrappedDescriptorCount));
if (SUCCEEDED(hr))
{
// Note that we need to add our own credential count to the wrapped credential's
// total count.
*pdwCount = _dwWrappedDescriptorCount + SFI_NUM_FIELDS;
}
}
return hr;
}
// Gets the field descriptor for a particular field. If this descriptor refers to one owned
// by our wrapped provider, we'll pass it along. Otherwise we provide our own.
HRESULT CSampleProvider::GetFieldDescriptorAt(
__in DWORD dwIndex,
__deref_out CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR** ppcpfd
)
{
HRESULT hr = E_UNEXPECTED;
if (_pWrappedProvider != NULL)
{
if (ppcpfd != NULL)
{
// If this field maps to one in the wrapped provider, hand it off.
if (dwIndex < _dwWrappedDescriptorCount)
{
hr = _pWrappedProvider->GetFieldDescriptorAt(dwIndex, ppcpfd);
}
// Otherwise, check to see if it's ours and then handle it here.
else
{
// Offset into the descriptor count so we can index our own fields.
dwIndex -= _dwWrappedDescriptorCount;
// Verify dwIndex is still a valid field.
if (dwIndex < SFI_NUM_FIELDS)
{
hr = FieldDescriptorCoAllocCopy(s_rgCredProvFieldDescriptors[dwIndex], ppcpfd);
(*ppcpfd)->dwFieldID += _dwWrappedDescriptorCount;
}
else
{
hr = E_INVALIDARG;
}
}
}
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 tile the last cred prov used can select
// the default tile.
// 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.
// While we're here, we'll create credentials to wrap each of the credentials created by
// our wrapped provider. The key is to make everything transparent to the owner.
HRESULT CSampleProvider::GetCredentialCount(
__out DWORD* pdwCount,
__out_range(<,*pdwCount) DWORD* pdwDefault,
__out BOOL* pbAutoLogonWithDefault
)
{
HRESULT hr = E_UNEXPECTED;
DWORD dwDefault = 0;
BOOL bAutoLogonWithDefault = FALSE;
// Make sure we've created the provider.
if (_pWrappedProvider != NULL)
{
// This probably shouldn't happen, but in the event that this gets called after
// we've already been through once, we want to clean up everything before
// allocating new stuff all over again.
if (_rgpCredentials != NULL)
{
_CleanUpAllCredentials();
}
// We need to know how many fields each credential has in order to initialize
// our wrapper credentials, so we might as well do that here before anything else.
DWORD count;
hr = GetFieldDescriptorCount(&(count));
if (SUCCEEDED(hr))
{
// Grab the credential count of the wrapped provider. We'll simply wrap each.
hr = _pWrappedProvider->GetCredentialCount(&(_dwCredentialCount), &(dwDefault), &(bAutoLogonWithDefault));
if (SUCCEEDED(hr))
{
// Create an array of credentials for use.
_rgpCredentials = new CSampleCredential*[_dwCredentialCount];
if (_rgpCredentials != NULL)
{
// Iterate each credential and make a wrapper.
for (DWORD lcv = 0; SUCCEEDED(hr) && (lcv < _dwCredentialCount); lcv++)
{
// Allocate memory for the new credential.
_rgpCredentials[lcv] = new CSampleCredential();
if (_rgpCredentials[lcv] != NULL)
{
ICredentialProviderCredential *pCredential;
hr = _pWrappedProvider->GetCredentialAt(lcv, &(pCredential));
if (SUCCEEDED(hr))
{
// Set the Field State Pair and Field Descriptors for ppc's
// fields to the defaults (s_rgCredProvFieldDescriptors,
// and s_rgFieldStatePairs) and the value of SFI_USERNAME
// to pwzUsername.
hr = _rgpCredentials[lcv]->Initialize(s_rgCredProvFieldDescriptors, s_rgFieldStatePairs, pCredential, _dwWrappedDescriptorCount);
if (FAILED(hr))
{
// If initialization failed, clean everything up.
for (lcv = 0; lcv < _dwCredentialCount; lcv++)
{
if (_rgpCredentials[lcv] != NULL)
{
// Release the pointer to account for the local reference.
_rgpCredentials[lcv]->Release();
_rgpCredentials[lcv] = NULL;
}
}
}
pCredential->Release();
} // (End if _pWrappedProvider->GetCredentialAt succeeded.)
} // (End if allocating _rgpCredentials[lcv] succeeded.)
else
{
hr = E_OUTOFMEMORY;
}
} // (End of _rgpCredentials allocation loop.)
} // (End if for allocating _rgpCredentials succeeded.)
else
{
hr = E_OUTOFMEMORY;
}
} // (End if _pWrappedProvider->GetCredentialCount succeeded.)
} // (End if GetFieldDescriptorCount succeeded.)
}
if (FAILED(hr))
{
// Clean up.
if (_rgpCredentials != NULL)
{
delete _rgpCredentials;
_rgpCredentials = NULL;
}
}
else
{
*pdwCount = _dwCredentialCount;
*pdwDefault = dwDefault;
*pbAutoLogonWithDefault = bAutoLogonWithDefault;
}
return hr;
}
// Returns the credential at the index specified by dwIndex. This function is called by
// logonUI to enumerate the tiles.
HRESULT CSampleProvider::GetCredentialAt(
__in DWORD dwIndex,
__in ICredentialProviderCredential** ppcpc
)
{
HRESULT hr;
// Validate parameters.
if ((dwIndex < _dwCredentialCount) &&
(ppcpc != NULL) &&
(_rgpCredentials != NULL) &&
(_rgpCredentials[dwIndex] != NULL))
{
hr = _rgpCredentials[dwIndex]->QueryInterface(IID_ICredentialProviderCredential, reinterpret_cast<void**>(ppcpc));
}
else
{
hr = E_INVALIDARG;
}
return hr;
}
// Boilerplate code to create our provider.
HRESULT CSample_CreateInstance(__in REFIID riid, __deref_out void** ppv)
{
HRESULT hr;
CSampleProvider* pProvider = new CSampleProvider();
if (pProvider)
{
hr = pProvider->QueryInterface(riid, ppv);
pProvider->Release();
}
else
{
hr = E_OUTOFMEMORY;
}
return hr;
}