1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "IdentityCredentialRequestManager.h"
8 #include "mozilla/dom/IdentityCredentialBinding.h"
9 #include "mozilla/dom/WindowGlobalParent.h"
10 #include "nsContentUtils.h"
11 #include "nsNetUtil.h"
12 #include "mozilla/BasePrincipal.h"
16 NS_IMPL_ISUPPORTS0(IdentityCredentialRequestManager
);
18 StaticRefPtr
<IdentityCredentialRequestManager
>
19 IdentityCredentialRequestManager::sSingleton
;
22 IdentityCredentialRequestManager
*
23 IdentityCredentialRequestManager::GetInstance() {
25 sSingleton
= new IdentityCredentialRequestManager();
26 ClearOnShutdown(&sSingleton
);
31 nsresult
IdentityCredentialRequestManager::StorePendingRequest(
32 const nsCOMPtr
<nsIPrincipal
>& aRPPrincipal
,
33 const dom::IdentityCredentialRequestOptions
& aRequest
,
35 dom::IdentityCredential::GetIPCIdentityCredentialPromise::Private
>&
37 const RefPtr
<dom::CanonicalBrowsingContext
>& aBrowsingContext
) {
38 MOZ_ASSERT(aRPPrincipal
);
40 if (!aRequest
.mProviders
.WasPassed()) {
41 return NS_ERROR_DOM_INVALID_ACCESS_ERR
;
44 for (const auto& provider
: aRequest
.mProviders
.Value()) {
45 if (!provider
.mLoginURL
.WasPassed()) {
48 nsCOMPtr
<nsIURI
> idpOriginURI
;
49 nsAutoCString idpOriginString
;
50 if (provider
.mOrigin
.WasPassed()) {
51 idpOriginString
= provider
.mOrigin
.Value();
53 // Infer the origin from the loginURL if one wasn't provided
54 idpOriginString
= provider
.mLoginURL
.Value();
56 nsresult rv
= NS_NewURI(getter_AddRefs(idpOriginURI
), idpOriginString
);
57 if (NS_WARN_IF(NS_FAILED(rv
))) {
58 return NS_ERROR_DOM_BAD_URI
;
60 nsCOMPtr
<nsIPrincipal
> idpPrincipal
= BasePrincipal::CreateContentPrincipal(
61 idpOriginURI
, aRPPrincipal
->OriginAttributesRef());
63 NS_ENSURE_TRUE(idpPrincipal
, NS_ERROR_FAILURE
);
64 nsTArray
<PendingRequestEntry
>& list
=
65 mPendingRequests
.LookupOrInsert(idpPrincipal
);
66 list
.AppendElement(PendingRequestEntry(aRPPrincipal
, aRequest
, aPromise
,
72 void IdentityCredentialRequestManager::NotifyOfStoredCredential(
73 const nsCOMPtr
<nsIPrincipal
>& aIDPPrincipal
,
74 const dom::IPCIdentityCredential
& aCredential
) {
75 MOZ_ASSERT(aIDPPrincipal
);
76 auto listLookup
= mPendingRequests
.Lookup(aIDPPrincipal
);
78 for (auto& entry
: listLookup
.Data()) {
79 if (!entry
.mBrowsingContext
) {
82 // We must (asynchronously) test if this credential should be sent down
84 dom::IdentityCredential::AllowedToCollectCredential(
85 entry
.mRPPrincipal
, entry
.mBrowsingContext
, entry
.mRequestOptions
,
88 GetCurrentSerialEventTarget(), __func__
,
89 [aCredential
, entry
](bool effectiveCredential
) {
90 if (effectiveCredential
) {
91 entry
.mPromise
->Resolve(aCredential
, __func__
);
99 } // namespace mozilla