Skip to content

Commit 3c39def

Browse files
committed
Add GA_login_user to handle all login types
1 parent ace0465 commit 3c39def

File tree

8 files changed

+105
-0
lines changed

8 files changed

+105
-0
lines changed

docs/source/gdk-json.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,46 @@ Connection parameter JSON
3232
"spv_enabled": false,
3333
}
3434
35+
.. _login-credentials:
36+
37+
Login Credentials JSON
38+
----------------------
39+
40+
To authenticate with a hardware wallet, pass empty JSON and provide :ref:`hw-device`.
41+
42+
To authenticate with a mnemonic and optional password:
43+
44+
.. code-block:: json
45+
46+
{
47+
"mnemonic": "moral lonely ability sail balance simple kid girl inhale master dismiss round about aerobic purpose shiver silly happy kitten track kind pattern nose noise",
48+
"password": ""
49+
}
50+
51+
To authenticate with a PIN:
52+
53+
.. code-block:: json
54+
55+
{
56+
"pin": "123456",
57+
"pin_data": {
58+
"encrypted_data": "0b39c1e90ca6adce9ff35d1780de74b91d46261a7cbf2b8d2fdc21528c068c8e2b26e3bf3f6a2a992e0e1ecfad0220343b9659495e7f4b21ff95c32cee1b2dd6b0f44b3828ccdc73d68d9e4142a25437b0c6b53a056e2415ca23442dd18d11fb5f62ef9155703c36a5b3e10b2d93973602cebb2369559612cb4267f4826028cea7b067d6ec3658cc72155a4b17b4ba277c143d40ce49c407102c62ca759d04e74dd0778ac514292be09f66449993c36b0bc0cb78f41368bc394d0cf444d452bea0e7df5766b92a3c3a3c57169c2529e9aa36e89b3f6dfcfddc6027f3aabd47dedbd9851729a3f6fba899842b1f5e949117c62e94f558da5ebd37feb4927209e2ead2d492c1d647049e8a1347c46c75411a14c5420ef6896cd0d0c6145af76668d9313f3e71e1970de58f674f3b387e4c74d24214fbc1ad7d30b3d2db3d6fb7d9e92dd1a9f836dad7c2713dc6ebfec62f",
59+
"pin_identifier": "38e2f188-b3a8-4d98-a7f9-6c348cb54cfe",
60+
"salt": "a99/9Qy6P7ON4Umk2FafVQ=="
61+
}
62+
}
63+
64+
See :ref:`pin-data` for the format of the `pin_data` element.
65+
66+
To authenticate a watch-only user:
67+
68+
.. code-block:: json
69+
70+
{
71+
"username": "my_watch_only_username",
72+
"password": "my_watch_only_password"
73+
}
74+
3575
.. _hw-device:
3676

3777
HW device JSON

include/gdk.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ GDK_API int GA_validate_asset_domain_name(struct GA_session* session, const GA_j
165165
GDK_API int GA_register_user(
166166
struct GA_session* session, const GA_json* hw_device, const char* mnemonic, struct GA_auth_handler** call);
167167

168+
/**
169+
* Authenticate a user.
170+
*
171+
* :param session: The session to use.
172+
* :param hw_device: Details about any :ref:`hw-device` being used to login.
173+
* :param details: The :ref:`login-credentials` for authenticating the user.
174+
* :param call: Destination for the resulting GA_auth_handler to perform the login.
175+
*| Returned GA_auth_handler should be freed using `GA_destroy_auth_handler`.
176+
*/
177+
GDK_API int GA_login_user(
178+
struct GA_session* session, const GA_json* hw_device, const GA_json* details, struct GA_auth_handler** call);
179+
168180
/**
169181
* Authenticate a user using a hardware wallet/HSM/TPM.
170182
*
@@ -174,6 +186,9 @@ GDK_API int GA_register_user(
174186
* :param password: The user's password to decrypt a 27 word mnemonic, or a blank string if none.
175187
* :param call: Destination for the resulting GA_auth_handler to perform the login.
176188
*| Returned GA_auth_handler should be freed using `GA_destroy_auth_handler`.
189+
*
190+
* .. note:: This call is deprecated and will be removed in a future release. Use
191+
*| `GA_login_user` to login.
177192
*/
178193
GDK_API int GA_login(struct GA_session* session, const GA_json* hw_device, const char* mnemonic, const char* password,
179194
struct GA_auth_handler** call);
@@ -186,6 +201,9 @@ GDK_API int GA_login(struct GA_session* session, const GA_json* hw_device, const
186201
* :param pin_data: The :ref:`pin-data` returned by `GA_set_pin`.
187202
* :param call: Destination for the resulting GA_auth_handler to perform the login with pin.
188203
*| Returned GA_auth_handler should be freed using `GA_destroy_auth_handler`.
204+
*
205+
* .. note:: This call is deprecated and will be removed in a future release. Use
206+
*| `GA_login_user` to login.
189207
*/
190208
GDK_API int GA_login_with_pin(
191209
struct GA_session* session, const char* pin, const GA_json* pin_data, struct GA_auth_handler** call);
@@ -214,6 +232,9 @@ GDK_API int GA_get_watch_only_username(struct GA_session* session, char** userna
214232
* :param session: The session to use.
215233
* :param username: The username.
216234
* :param password: The password.
235+
*
236+
* .. note:: This call is deprecated and will be removed in a future release. Use
237+
*| `GA_login_user` to login.
217238
*/
218239
GDK_API int GA_login_watch_only(struct GA_session* session, const char* username, const char* password);
219240

src/ffi_c.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ GDK_DEFINE_C_FUNCTION_4(GA_register_user, struct GA_session*, session, const GA_
222222
struct GA_auth_handler**, call,
223223
{ *call = auth_cast(new ga::sdk::register_call(*session, *json_cast(hw_device), mnemonic)); })
224224

225+
GDK_DEFINE_C_FUNCTION_4(GA_login_user, struct GA_session*, session, const GA_json*, hw_device, const GA_json*, details,
226+
struct GA_auth_handler**, call,
227+
{ *call = auth_cast(ga::sdk::get_login_call(*session, *json_cast(hw_device), *json_cast(details))); })
228+
225229
GDK_DEFINE_C_FUNCTION_5(GA_login, struct GA_session*, session, const GA_json*, hw_device, const char*, mnemonic,
226230
const char*, password, struct GA_auth_handler**, call,
227231
{ *call = auth_cast(new ga::sdk::login_call(*session, *json_cast(hw_device), mnemonic, password)); })

src/ga_auth_handlers.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,30 @@ namespace sdk {
493493
m_result = m_session.login_watch_only(username, password);
494494
} catch (const std::exception& ex) {
495495
set_error(res::id_username);
496+
return state_type::error;
496497
}
497498
return state_type::done;
498499
}
499500

501+
//
502+
// Return a suitable auth handler for all supported login types
503+
//
504+
auth_handler* get_login_call(
505+
session& session, const nlohmann::json& hw_device, const nlohmann::json& credential_data)
506+
{
507+
if (!hw_device.empty() || credential_data.contains("mnemonic")) {
508+
const auto mnemonic = json_get_value(credential_data, "mnemonic");
509+
const auto password = json_get_value(credential_data, "password");
510+
// FIXME: Allow a "bip39_passphrase" element to enable bip39 logins
511+
return new login_call(session, hw_device, mnemonic, password);
512+
} else if (credential_data.contains("pin")) {
513+
return new login_with_pin_call(session, credential_data.at("pin"), credential_data.at("pin_data"));
514+
} else {
515+
// Assume watch-only
516+
return new watch_only_login_call(session, credential_data);
517+
}
518+
}
519+
500520
//
501521
// Create subaccount
502522
//

src/ga_auth_handlers.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace sdk {
1616
std::string m_mnemonic;
1717
};
1818

19+
// Return an auth handler for logging in a user (caller must delete it)
20+
auth_handler* get_login_call(
21+
session& session, const nlohmann::json& hw_device, const nlohmann::json& credential_data);
22+
1923
class login_call : public auth_handler {
2024
public:
2125
login_call(session& session, const nlohmann::json& hw_device, const std::string& mnemonic,

src/swift/GreenAddress/Sources/GreenAddress/GreenAddress.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ public class Session {
243243
return TwoFactorCall(optr: optr!);
244244
}
245245

246+
public func loginUser(details: [String: Any], hw_device: [String: Any] = [:]) throws -> TwoFactorCall {
247+
var optr: OpaquePointer? = nil;
248+
var hw_device_json: OpaquePointer = try convertDictToJSON(dict: hw_device)
249+
var details_json: OpaquePointer = try convertDictToJSON(dict: details)
250+
try callWrapper(fun: GA_login_user(session, hw_device_json, details_json, &optr))
251+
defer {
252+
GA_destroy_json(hw_device_json)
253+
GA_destroy_json(details_json)
254+
}
255+
return TwoFactorCall(optr: optr!);
256+
}
257+
246258
public func login(mnemonic: String = "", password: String = "", hw_device: [String: Any] = [:]) throws -> TwoFactorCall {
247259
var optr: OpaquePointer? = nil;
248260
var hw_device_json: OpaquePointer = try convertDictToJSON(dict: hw_device)

src/swig_java/swig_gasdk.i

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ LOCALFUNC jbyteArray create_array(JNIEnv *jenv, const unsigned char* p, size_t l
527527
%returns_void__(GA_login_watch_only)
528528
%returns_struct(GA_login_with_pin, GA_auth_handler)
529529
%returns_struct(GA_login, GA_auth_handler)
530+
%returns_struct(GA_login_user, GA_auth_handler)
530531
%returns_void__(GA_register_network)
531532
%returns_struct(GA_register_user, GA_auth_handler)
532533
%returns_struct(GA_remove_account, GA_auth_handler)

src/swig_python/python_extra.py_in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ class Session(object):
138138
def register_user(self, hw_device, mnemonic):
139139
return Call(register_user(self.session_obj, self._to_json(hw_device), mnemonic))
140140

141+
def login_user(self, hw_device, details):
142+
return Call(login_user(self.session_obj, self._to_json(hw_device), self._to_json(details)))
143+
141144
def login_with_pin(self, pin, pin_data):
142145
return Call(login_with_pin(self.session_obj, pin, self._to_json(pin_data)))
143146

0 commit comments

Comments
 (0)