Source code

Revision control

Other Tools

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/. */
6
7
#ifndef mozilla_dom_AndroidWebAuthnTokenManager_h
8
#define mozilla_dom_AndroidWebAuthnTokenManager_h
9
10
#include "mozilla/dom/CryptoBuffer.h"
11
#include "mozilla/dom/U2FTokenTransport.h"
12
13
namespace mozilla {
14
namespace dom {
15
16
// Collected from
18
NS_NAMED_LITERAL_STRING(kSecurityError, "SECURITY_ERR");
19
NS_NAMED_LITERAL_STRING(kConstraintError, "CONSTRAINT_ERR");
20
NS_NAMED_LITERAL_STRING(kNotSupportedError, "NOT_SUPPORTED_ERR");
21
NS_NAMED_LITERAL_STRING(kInvalidStateError, "INVALID_STATE_ERR");
22
NS_NAMED_LITERAL_STRING(kNotAllowedError, "NOT_ALLOWED_ERR");
23
NS_NAMED_LITERAL_STRING(kAbortError, "ABORT_ERR");
24
NS_NAMED_LITERAL_STRING(kEncodingError, "ENCODING_ERR");
25
NS_NAMED_LITERAL_STRING(kDataError, "DATA_ERR");
26
NS_NAMED_LITERAL_STRING(kTimeoutError, "TIMEOUT_ERR");
27
NS_NAMED_LITERAL_STRING(kNetworkError, "NETWORK_ERR");
28
NS_NAMED_LITERAL_STRING(kUnknownError, "UNKNOWN_ERR");
29
30
class AndroidWebAuthnResult {
31
public:
32
explicit AndroidWebAuthnResult(const nsAString& aErrorCode)
33
: mErrorCode(aErrorCode) {}
34
35
explicit AndroidWebAuthnResult() {}
36
37
bool IsError() const { return NS_FAILED(GetError()); }
38
39
nsresult GetError() const {
40
if (mErrorCode.IsEmpty()) {
41
return NS_OK;
42
} else if (mErrorCode.Equals(kSecurityError)) {
43
return NS_ERROR_DOM_SECURITY_ERR;
44
} else if (mErrorCode.Equals(kConstraintError)) {
45
// TODO: The message is right, but it's not about indexeddb.
47
return NS_ERROR_DOM_INDEXEDDB_CONSTRAINT_ERR;
48
} else if (mErrorCode.Equals(kNotSupportedError)) {
49
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
50
} else if (mErrorCode.Equals(kInvalidStateError)) {
51
return NS_ERROR_DOM_INVALID_STATE_ERR;
52
} else if (mErrorCode.Equals(kNotAllowedError)) {
53
return NS_ERROR_DOM_NOT_ALLOWED_ERR;
54
} else if (mErrorCode.Equals(kEncodingError)) {
55
return NS_ERROR_DOM_ENCODING_NOT_SUPPORTED_ERR;
56
} else if (mErrorCode.Equals(kDataError)) {
57
return NS_ERROR_DOM_DATA_ERR;
58
} else if (mErrorCode.Equals(kTimeoutError)) {
59
return NS_ERROR_DOM_TIMEOUT_ERR;
60
} else if (mErrorCode.Equals(kNetworkError)) {
61
return NS_ERROR_DOM_NETWORK_ERR;
62
} else if (mErrorCode.Equals(kAbortError)) {
63
return NS_ERROR_DOM_ABORT_ERR;
64
} else if (mErrorCode.Equals(kUnknownError)) {
65
return NS_ERROR_DOM_UNKNOWN_ERR;
66
} else {
67
__android_log_print(ANDROID_LOG_ERROR, "Gecko",
68
"RegisterAbort unknown code: %s",
69
NS_ConvertUTF16toUTF8(mErrorCode).get());
70
return NS_ERROR_DOM_UNKNOWN_ERR;
71
}
72
}
73
74
// Attestation-only
75
CryptoBuffer mAttObj;
76
77
// Attestations and assertions
78
CryptoBuffer mKeyHandle;
79
nsCString mClientDataJSON;
80
81
// Assertions-only
82
CryptoBuffer mAuthData;
83
CryptoBuffer mSignature;
84
CryptoBuffer mUserHandle;
85
86
private:
87
const nsString mErrorCode;
88
};
89
90
/*
91
* WebAuthnAndroidTokenManager is a token implementation communicating with
92
* Android Fido2 APIs.
93
*/
94
class AndroidWebAuthnTokenManager final : public U2FTokenTransport {
95
public:
96
explicit AndroidWebAuthnTokenManager();
97
~AndroidWebAuthnTokenManager() {}
98
99
RefPtr<U2FRegisterPromise> Register(const WebAuthnMakeCredentialInfo& aInfo,
100
bool aForceNoneAttestation) override;
101
102
RefPtr<U2FSignPromise> Sign(const WebAuthnGetAssertionInfo& aInfo) override;
103
104
void Cancel() override;
105
106
void Drop() override;
107
108
void HandleRegisterResult(const AndroidWebAuthnResult& aResult);
109
110
void HandleSignResult(const AndroidWebAuthnResult& aResult);
111
112
static AndroidWebAuthnTokenManager* GetInstance();
113
114
private:
115
void ClearPromises() {
116
mRegisterPromise.RejectIfExists(NS_ERROR_DOM_UNKNOWN_ERR, __func__);
117
mSignPromise.RejectIfExists(NS_ERROR_DOM_UNKNOWN_ERR, __func__);
118
}
119
120
void AssertIsOnOwningThread() const;
121
122
MozPromiseHolder<U2FRegisterPromise> mRegisterPromise;
123
MozPromiseHolder<U2FSignPromise> mSignPromise;
124
};
125
126
} // namespace dom
127
} // namespace mozilla
128
129
#endif // mozilla_dom_AndroidWebAuthnTokenManager_h