1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "modules/credentialmanager/CredentialsContainer.h"
7
8 #include "bindings/core/v8/Dictionary.h"
9 #include "bindings/core/v8/ScriptPromise.h"
10 #include "bindings/core/v8/ScriptPromiseResolver.h"
11 #include "core/dom/DOMException.h"
12 #include "core/dom/ExceptionCode.h"
13 #include "core/dom/ExecutionContext.h"
14 #include "modules/credentialmanager/Credential.h"
15 #include "modules/credentialmanager/CredentialManagerClient.h"
16 #include "modules/credentialmanager/LocalCredential.h"
17 #include "platform/weborigin/SecurityOrigin.h"
18 #include "public/platform/Platform.h"
19 #include "public/platform/WebCredential.h"
20 #include "public/platform/WebCredentialManagerClient.h"
21 #include "public/platform/WebCredentialManagerError.h"
22 #include "public/platform/WebLocalCredential.h"
23
24 namespace blink {
25
rejectDueToCredentialManagerError(PassRefPtr<ScriptPromiseResolver> resolver,WebCredentialManagerError * reason)26 static void rejectDueToCredentialManagerError(PassRefPtr<ScriptPromiseResolver> resolver, WebCredentialManagerError* reason)
27 {
28 switch (reason->errorType) {
29 case WebCredentialManagerError::ErrorTypeDisabled:
30 resolver->reject(DOMException::create(InvalidStateError, "The credential manager is disabled."));
31 break;
32 case WebCredentialManagerError::ErrorTypeUnknown:
33 default:
34 resolver->reject(DOMException::create(NotReadableError, "An unknown error occured while talking to the credential manager."));
35 break;
36 }
37 }
38
39 class NotificationCallbacks : public WebCredentialManagerClient::NotificationCallbacks {
40 WTF_MAKE_NONCOPYABLE(NotificationCallbacks);
41 public:
NotificationCallbacks(PassRefPtr<ScriptPromiseResolver> resolver)42 explicit NotificationCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_resolver(resolver) { }
~NotificationCallbacks()43 virtual ~NotificationCallbacks() { }
44
onSuccess()45 virtual void onSuccess() OVERRIDE
46 {
47 m_resolver->resolve();
48 }
49
onError(WebCredentialManagerError * reason)50 virtual void onError(WebCredentialManagerError* reason) OVERRIDE
51 {
52 rejectDueToCredentialManagerError(m_resolver, reason);
53 }
54
55 private:
56 const RefPtr<ScriptPromiseResolver> m_resolver;
57 };
58
59 class RequestCallbacks : public WebCredentialManagerClient::RequestCallbacks {
60 WTF_MAKE_NONCOPYABLE(RequestCallbacks);
61 public:
RequestCallbacks(PassRefPtr<ScriptPromiseResolver> resolver)62 explicit RequestCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_resolver(resolver) { }
~RequestCallbacks()63 virtual ~RequestCallbacks() { }
64
onSuccess(WebCredential * credential)65 virtual void onSuccess(WebCredential* credential) OVERRIDE
66 {
67 if (!credential) {
68 m_resolver->resolve();
69 return;
70 }
71
72 // FIXME: Split this into Local/Federated types. Right now it's hard-coded to be a LocalCredential. :(
73 m_resolver->resolve(LocalCredential::create(static_cast<WebLocalCredential*>(credential)));
74 }
75
onError(WebCredentialManagerError * reason)76 virtual void onError(WebCredentialManagerError* reason) OVERRIDE
77 {
78 rejectDueToCredentialManagerError(m_resolver, reason);
79 }
80
81 private:
82 const RefPtr<ScriptPromiseResolver> m_resolver;
83 };
84
85
create()86 CredentialsContainer* CredentialsContainer::create()
87 {
88 return new CredentialsContainer();
89 }
90
CredentialsContainer()91 CredentialsContainer::CredentialsContainer()
92 {
93 }
94
checkBoilerplate(PassRefPtr<ScriptPromiseResolver> resolver)95 static bool checkBoilerplate(PassRefPtr<ScriptPromiseResolver> resolver)
96 {
97 CredentialManagerClient* client = CredentialManagerClient::from(resolver->scriptState()->executionContext());
98 if (!client) {
99 resolver->reject(DOMException::create(InvalidStateError, "Could not establish connection to the credential manager."));
100 return false;
101 }
102
103 SecurityOrigin* securityOrigin = resolver->scriptState()->executionContext()->securityOrigin();
104 String errorMessage;
105 if (!securityOrigin->canAccessFeatureRequiringSecureOrigin(errorMessage)) {
106 resolver->reject(DOMException::create(SecurityError, errorMessage));
107 return false;
108 }
109
110 return true;
111 }
112
request(ScriptState * scriptState,const Dictionary &)113 ScriptPromise CredentialsContainer::request(ScriptState* scriptState, const Dictionary&)
114 {
115 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
116 ScriptPromise promise = resolver->promise();
117 if (!checkBoilerplate(resolver))
118 return promise;
119
120 WebVector<WebURL> tempVector;
121 CredentialManagerClient::from(scriptState->executionContext())->dispatchRequest(false, tempVector, new RequestCallbacks(resolver));
122 return promise;
123 }
124
notifySignedIn(ScriptState * scriptState,Credential * credential)125 ScriptPromise CredentialsContainer::notifySignedIn(ScriptState* scriptState, Credential* credential)
126 {
127 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
128 ScriptPromise promise = resolver->promise();
129 if (!checkBoilerplate(resolver))
130 return promise;
131
132 CredentialManagerClient::from(scriptState->executionContext())->dispatchSignedIn(WebCredential(credential->platformCredential()), new NotificationCallbacks(resolver));
133 return promise;
134 }
135
notifyFailedSignIn(ScriptState * scriptState,Credential * credential)136 ScriptPromise CredentialsContainer::notifyFailedSignIn(ScriptState* scriptState, Credential* credential)
137 {
138 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
139 ScriptPromise promise = resolver->promise();
140 if (!checkBoilerplate(resolver))
141 return promise;
142
143 CredentialManagerClient::from(scriptState->executionContext())->dispatchFailedSignIn(WebCredential(credential->platformCredential()), new NotificationCallbacks(resolver));
144 return promise;
145 }
146
notifySignedOut(ScriptState * scriptState)147 ScriptPromise CredentialsContainer::notifySignedOut(ScriptState* scriptState)
148 {
149 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
150 ScriptPromise promise = resolver->promise();
151 if (!checkBoilerplate(resolver))
152 return promise;
153
154 CredentialManagerClient::from(scriptState->executionContext())->dispatchSignedOut(new NotificationCallbacks(resolver));
155 return promise;
156 }
157
158 } // namespace blink
159