Skip to content

Commit

Permalink
Add GA_login_user to handle all login types
Browse files Browse the repository at this point in the history
  • Loading branch information
jgriffiths committed Apr 23, 2021
1 parent ace0465 commit 3c39def
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/source/gdk-json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,46 @@ Connection parameter JSON
"spv_enabled": false,
}
.. _login-credentials:

Login Credentials JSON
----------------------

To authenticate with a hardware wallet, pass empty JSON and provide :ref:`hw-device`.

To authenticate with a mnemonic and optional password:

.. code-block:: json
{
"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",
"password": ""
}
To authenticate with a PIN:

.. code-block:: json
{
"pin": "123456",
"pin_data": {
"encrypted_data": "0b39c1e90ca6adce9ff35d1780de74b91d46261a7cbf2b8d2fdc21528c068c8e2b26e3bf3f6a2a992e0e1ecfad0220343b9659495e7f4b21ff95c32cee1b2dd6b0f44b3828ccdc73d68d9e4142a25437b0c6b53a056e2415ca23442dd18d11fb5f62ef9155703c36a5b3e10b2d93973602cebb2369559612cb4267f4826028cea7b067d6ec3658cc72155a4b17b4ba277c143d40ce49c407102c62ca759d04e74dd0778ac514292be09f66449993c36b0bc0cb78f41368bc394d0cf444d452bea0e7df5766b92a3c3a3c57169c2529e9aa36e89b3f6dfcfddc6027f3aabd47dedbd9851729a3f6fba899842b1f5e949117c62e94f558da5ebd37feb4927209e2ead2d492c1d647049e8a1347c46c75411a14c5420ef6896cd0d0c6145af76668d9313f3e71e1970de58f674f3b387e4c74d24214fbc1ad7d30b3d2db3d6fb7d9e92dd1a9f836dad7c2713dc6ebfec62f",
"pin_identifier": "38e2f188-b3a8-4d98-a7f9-6c348cb54cfe",
"salt": "a99/9Qy6P7ON4Umk2FafVQ=="
}
}
See :ref:`pin-data` for the format of the `pin_data` element.

To authenticate a watch-only user:

.. code-block:: json
{
"username": "my_watch_only_username",
"password": "my_watch_only_password"
}
.. _hw-device:

HW device JSON
Expand Down
21 changes: 21 additions & 0 deletions include/gdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ GDK_API int GA_validate_asset_domain_name(struct GA_session* session, const GA_j
GDK_API int GA_register_user(
struct GA_session* session, const GA_json* hw_device, const char* mnemonic, struct GA_auth_handler** call);

/**
* Authenticate a user.
*
* :param session: The session to use.
* :param hw_device: Details about any :ref:`hw-device` being used to login.
* :param details: The :ref:`login-credentials` for authenticating the user.
* :param call: Destination for the resulting GA_auth_handler to perform the login.
*| Returned GA_auth_handler should be freed using `GA_destroy_auth_handler`.
*/
GDK_API int GA_login_user(
struct GA_session* session, const GA_json* hw_device, const GA_json* details, struct GA_auth_handler** call);

/**
* Authenticate a user using a hardware wallet/HSM/TPM.
*
Expand All @@ -174,6 +186,9 @@ GDK_API int GA_register_user(
* :param password: The user's password to decrypt a 27 word mnemonic, or a blank string if none.
* :param call: Destination for the resulting GA_auth_handler to perform the login.
*| Returned GA_auth_handler should be freed using `GA_destroy_auth_handler`.
*
* .. note:: This call is deprecated and will be removed in a future release. Use
*| `GA_login_user` to login.
*/
GDK_API int GA_login(struct GA_session* session, const GA_json* hw_device, const char* mnemonic, const char* password,
struct GA_auth_handler** call);
Expand All @@ -186,6 +201,9 @@ GDK_API int GA_login(struct GA_session* session, const GA_json* hw_device, const
* :param pin_data: The :ref:`pin-data` returned by `GA_set_pin`.
* :param call: Destination for the resulting GA_auth_handler to perform the login with pin.
*| Returned GA_auth_handler should be freed using `GA_destroy_auth_handler`.
*
* .. note:: This call is deprecated and will be removed in a future release. Use
*| `GA_login_user` to login.
*/
GDK_API int GA_login_with_pin(
struct GA_session* session, const char* pin, const GA_json* pin_data, struct GA_auth_handler** call);
Expand Down Expand Up @@ -214,6 +232,9 @@ GDK_API int GA_get_watch_only_username(struct GA_session* session, char** userna
* :param session: The session to use.
* :param username: The username.
* :param password: The password.
*
* .. note:: This call is deprecated and will be removed in a future release. Use
*| `GA_login_user` to login.
*/
GDK_API int GA_login_watch_only(struct GA_session* session, const char* username, const char* password);

Expand Down
4 changes: 4 additions & 0 deletions src/ffi_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ GDK_DEFINE_C_FUNCTION_4(GA_register_user, struct GA_session*, session, const GA_
struct GA_auth_handler**, call,
{ *call = auth_cast(new ga::sdk::register_call(*session, *json_cast(hw_device), mnemonic)); })

GDK_DEFINE_C_FUNCTION_4(GA_login_user, struct GA_session*, session, const GA_json*, hw_device, const GA_json*, details,
struct GA_auth_handler**, call,
{ *call = auth_cast(ga::sdk::get_login_call(*session, *json_cast(hw_device), *json_cast(details))); })

GDK_DEFINE_C_FUNCTION_5(GA_login, struct GA_session*, session, const GA_json*, hw_device, const char*, mnemonic,
const char*, password, struct GA_auth_handler**, call,
{ *call = auth_cast(new ga::sdk::login_call(*session, *json_cast(hw_device), mnemonic, password)); })
Expand Down
20 changes: 20 additions & 0 deletions src/ga_auth_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,30 @@ namespace sdk {
m_result = m_session.login_watch_only(username, password);
} catch (const std::exception& ex) {
set_error(res::id_username);
return state_type::error;
}
return state_type::done;
}

//
// Return a suitable auth handler for all supported login types
//
auth_handler* get_login_call(
session& session, const nlohmann::json& hw_device, const nlohmann::json& credential_data)
{
if (!hw_device.empty() || credential_data.contains("mnemonic")) {
const auto mnemonic = json_get_value(credential_data, "mnemonic");
const auto password = json_get_value(credential_data, "password");
// FIXME: Allow a "bip39_passphrase" element to enable bip39 logins
return new login_call(session, hw_device, mnemonic, password);
} else if (credential_data.contains("pin")) {
return new login_with_pin_call(session, credential_data.at("pin"), credential_data.at("pin_data"));
} else {
// Assume watch-only
return new watch_only_login_call(session, credential_data);
}
}

//
// Create subaccount
//
Expand Down
4 changes: 4 additions & 0 deletions src/ga_auth_handlers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ namespace sdk {
std::string m_mnemonic;
};

// Return an auth handler for logging in a user (caller must delete it)
auth_handler* get_login_call(
session& session, const nlohmann::json& hw_device, const nlohmann::json& credential_data);

class login_call : public auth_handler {
public:
login_call(session& session, const nlohmann::json& hw_device, const std::string& mnemonic,
Expand Down
12 changes: 12 additions & 0 deletions src/swift/GreenAddress/Sources/GreenAddress/GreenAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ public class Session {
return TwoFactorCall(optr: optr!);
}

public func loginUser(details: [String: Any], hw_device: [String: Any] = [:]) throws -> TwoFactorCall {
var optr: OpaquePointer? = nil;
var hw_device_json: OpaquePointer = try convertDictToJSON(dict: hw_device)
var details_json: OpaquePointer = try convertDictToJSON(dict: details)
try callWrapper(fun: GA_login_user(session, hw_device_json, details_json, &optr))
defer {
GA_destroy_json(hw_device_json)
GA_destroy_json(details_json)
}
return TwoFactorCall(optr: optr!);
}

public func login(mnemonic: String = "", password: String = "", hw_device: [String: Any] = [:]) throws -> TwoFactorCall {
var optr: OpaquePointer? = nil;
var hw_device_json: OpaquePointer = try convertDictToJSON(dict: hw_device)
Expand Down
1 change: 1 addition & 0 deletions src/swig_java/swig_gasdk.i
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ LOCALFUNC jbyteArray create_array(JNIEnv *jenv, const unsigned char* p, size_t l
%returns_void__(GA_login_watch_only)
%returns_struct(GA_login_with_pin, GA_auth_handler)
%returns_struct(GA_login, GA_auth_handler)
%returns_struct(GA_login_user, GA_auth_handler)
%returns_void__(GA_register_network)
%returns_struct(GA_register_user, GA_auth_handler)
%returns_struct(GA_remove_account, GA_auth_handler)
Expand Down
3 changes: 3 additions & 0 deletions src/swig_python/python_extra.py_in
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class Session(object):
def register_user(self, hw_device, mnemonic):
return Call(register_user(self.session_obj, self._to_json(hw_device), mnemonic))

def login_user(self, hw_device, details):
return Call(login_user(self.session_obj, self._to_json(hw_device), self._to_json(details)))

def login_with_pin(self, pin, pin_data):
return Call(login_with_pin(self.session_obj, pin, self._to_json(pin_data)))

Expand Down

0 comments on commit 3c39def

Please sign in to comment.