Skip to content

Commit

Permalink
cloud: expose oc_cloud_access_* functions to public API
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed Mar 22, 2023
1 parent db0d204 commit 8575b83
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 115 deletions.
27 changes: 16 additions & 11 deletions api/cloud/oc_cloud_access.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#ifdef OC_CLOUD

#include "oc_cloud_access.h"
#include "oc_cloud_access_internal.h"
#include "oc_cloud_internal.h"
#include "oc_core_res.h"
Expand Down Expand Up @@ -85,9 +86,9 @@ cloud_tls_add_peer(const oc_endpoint_t *endpoint, int selected_identity_cred_id)

/* Internal APIs for accessing the OCF Cloud */
bool
cloud_access_register(oc_cloud_access_conf_t conf, const char *auth_provider,
const char *auth_code, const char *uid,
const char *access_token)
oc_cloud_access_register(oc_cloud_access_conf_t conf, const char *auth_provider,
const char *auth_code, const char *uid,
const char *access_token)
{
if (conf.endpoint == NULL || conf.handler == NULL ||
((auth_provider == NULL || auth_code == NULL) && access_token == NULL)) {
Expand Down Expand Up @@ -171,8 +172,8 @@ cloud_access_deregister_query(const char *uid, const char *access_token,
}

bool
cloud_access_deregister(oc_cloud_access_conf_t conf, const char *uid,
const char *access_token)
oc_cloud_access_deregister(oc_cloud_access_conf_t conf, const char *uid,
const char *access_token)
{
if (conf.endpoint == NULL || conf.handler == NULL) {
OC_ERR("Error of input parameters");
Expand Down Expand Up @@ -248,22 +249,23 @@ cloud_access_login_out(oc_cloud_access_conf_t conf, const char *uid,
}

bool
cloud_access_login(oc_cloud_access_conf_t conf, const char *uid,
const char *access_token)
oc_cloud_access_login(oc_cloud_access_conf_t conf, const char *uid,
const char *access_token)
{
return cloud_access_login_out(conf, uid, access_token, /*is_sign_in*/ true);
}

bool
cloud_access_logout(oc_cloud_access_conf_t conf, const char *uid,
const char *access_token)
oc_cloud_access_logout(oc_cloud_access_conf_t conf, const char *uid,
const char *access_token)
{
return cloud_access_login_out(conf, uid, access_token, /*is_sign_in*/ false);
}

bool
cloud_access_refresh_access_token(oc_cloud_access_conf_t conf, const char *uid,
const char *refresh_token)
oc_cloud_access_refresh_access_token(oc_cloud_access_conf_t conf,
const char *auth_provider, const char *uid,
const char *refresh_token)
{
if (conf.endpoint == NULL || conf.handler == NULL || uid == NULL ||
refresh_token == NULL) {
Expand Down Expand Up @@ -296,6 +298,9 @@ cloud_access_refresh_access_token(oc_cloud_access_conf_t conf, const char *uid,
oc_rep_start_root_object();
oc_rep_set_text_string(root, uid, uid);
oc_rep_set_text_string(root, di, uuid);
if (auth_provider != NULL) {
oc_rep_set_text_string(root, authprovider, auth_provider);
}
oc_rep_set_text_string(root, granttype, OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN);
oc_rep_set_text_string(root, refreshtoken, refresh_token);
oc_rep_end_root_object();
Expand Down
74 changes: 0 additions & 74 deletions api/cloud/oc_cloud_access_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,6 @@
extern "C" {
#endif

// Common configuration arguments for cloud_access functions
typedef struct oc_cloud_access_conf_t
{
oc_endpoint_t *endpoint; ///< cloud endpoint
size_t device; ///< index of the device
int selected_identity_cred_id; ///< selected identity certficate id
oc_response_handler_t handler; ///< response callback
void *user_data; ///< data passed to response callback
uint16_t timeout; ///< timeout for response
} oc_cloud_access_conf_t;

/**
* @brief Send request to register device to cloud.
*
* @param conf cloud access configuration
* @param auth_provider authorization provider
* @param auth_code authorization code
* @param uid user id
* @param access_token access token
* @return true on success
* false otherwise
*/
bool cloud_access_register(oc_cloud_access_conf_t conf,
const char *auth_provider, const char *auth_code,
const char *uid, const char *access_token);

/**
* @brief Generate URI query for deregister request.
*
Expand All @@ -65,54 +39,6 @@ bool cloud_access_register(oc_cloud_access_conf_t conf,
oc_string_t cloud_access_deregister_query(const char *uid,
const char *access_token,
size_t device);
/**
* @brief Send request to deregister device from cloud.
*
* The device must be registered and logged in for this call to succeed.
*
* @param conf cloud access configuration
* @param uid user id
* @param access_token access token
* @return true on success
* false otherwise
*/
bool cloud_access_deregister(oc_cloud_access_conf_t conf, const char *uid,
const char *access_token);
/**
* @brief Send request to sign in the device to the cloud.
*
* @param conf cloud access configuration
* @param uid user id
* @param access_token access token
* @return true on success
* false otherwise
*/
bool cloud_access_login(oc_cloud_access_conf_t conf, const char *uid,
const char *access_token);
/**
* @brief Send request to sign out the device to the cloud.
*
* @param conf cloud access configuration
* @param uid user id
* @param access_token access token
* @return true on success
* false otherwise
*/
bool cloud_access_logout(oc_cloud_access_conf_t conf, const char *uid,
const char *access_token);
/**
* @brief Send request to refresh the device access token to the cloud.
*
* @param conf cloud access configuration
* @param uid user id
* @param refresh_token refresh token
* @return true on success
* false otherwise
*/
bool cloud_access_refresh_access_token(oc_cloud_access_conf_t conf,
const char *uid,
const char *refresh_token);

#ifdef __cplusplus
}
#endif
Expand Down
21 changes: 11 additions & 10 deletions api/cloud/oc_cloud_apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "oc_api.h"
#include "oc_client_state.h"
#include "oc_cloud.h"
#include "oc_cloud_access_internal.h"
#include "oc_cloud_access.h"
#include "oc_cloud_context_internal.h"
#include "oc_cloud_deregister_internal.h"
#include "oc_cloud_internal.h"
Expand Down Expand Up @@ -121,9 +121,9 @@ cloud_register(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
.user_data = p,
.timeout = timeout,
};
if (cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL,
oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
if (oc_cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL,
oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
ctx->store.cps = OC_CPS_REGISTERING;
return 0;
}
Expand Down Expand Up @@ -180,8 +180,8 @@ cloud_login(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
}
conf.endpoint = ctx->cloud_ep;

if (cloud_access_login(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
if (oc_cloud_access_login(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
return 0;
}

Expand Down Expand Up @@ -266,8 +266,8 @@ cloud_logout(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
goto error;
}
conf.endpoint = ctx->cloud_ep;
if (cloud_access_logout(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
if (oc_cloud_access_logout(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
return 0;
}

Expand Down Expand Up @@ -325,8 +325,9 @@ cloud_refresh_token(oc_cloud_context_t *ctx, oc_cloud_cb_t cb, void *data,
goto error;
}
conf.endpoint = ctx->cloud_ep;
if (cloud_access_refresh_access_token(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.refresh_token))) {
if (oc_cloud_access_refresh_access_token(
conf, oc_string(ctx->store.auth_provider), oc_string(ctx->store.uid),
oc_string(ctx->store.refresh_token))) {
return 0;
}

Expand Down
12 changes: 11 additions & 1 deletion api/cloud/oc_cloud_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ cloud_context_iterate(cloud_context_iterator_cb_t cb, void *user_data)

void
cloud_context_clear(oc_cloud_context_t *ctx)
{
oc_cloud_context_clear(ctx, true);
}

void
oc_cloud_context_clear(oc_cloud_context_t *ctx, bool dump_async)
{
assert(ctx != NULL);

Expand All @@ -142,7 +148,11 @@ cloud_context_clear(oc_cloud_context_t *ctx)
ctx->store.cps = 0;
ctx->selected_identity_cred_id = -1;
ctx->keepalive.ping_timeout = 4;
cloud_store_dump_async(&ctx->store);
if (dump_async) {
cloud_store_dump_async(&ctx->store);
} else {
cloud_store_dump(&ctx->store);
}
}

size_t
Expand Down
3 changes: 2 additions & 1 deletion api/cloud/oc_cloud_deregister.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "oc_api.h"
#include "oc_cloud_internal.h"
#include "oc_cloud_access.h"
#include "oc_cloud_access_internal.h"
#include "oc_cloud_context_internal.h"
#include "oc_cloud_deregister_internal.h"
Expand Down Expand Up @@ -186,7 +187,7 @@ cloud_deregister_by_request(cloud_api_param_t *p, uint16_t timeout,
}
conf.endpoint = ctx->cloud_ep;

if (cloud_access_deregister(
if (oc_cloud_access_deregister(
conf, oc_string(ctx->store.uid),
useAccessToken ? oc_string(ctx->store.access_token) : NULL)) {
return 0;
Expand Down
17 changes: 9 additions & 8 deletions api/cloud/oc_cloud_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#ifdef OC_CLOUD

#include "oc_api.h"
#include "oc_cloud_access_internal.h"
#include "oc_cloud_access.h"
#include "oc_cloud_context_internal.h"
#include "oc_cloud_internal.h"
#include "oc_cloud_manager_internal.h"
Expand Down Expand Up @@ -422,9 +422,9 @@ cloud_manager_register_async(void *data)
goto retry;
}
conf.endpoint = ctx->cloud_ep;
if (!cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL,
oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
if (!oc_cloud_access_register(conf, oc_string(ctx->store.auth_provider), NULL,
oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
OC_ERR("failed to sent register request to cloud");
goto retry;
}
Expand Down Expand Up @@ -639,8 +639,8 @@ cloud_manager_login_async(void *data)
goto retry;
}
conf.endpoint = ctx->cloud_ep;
if (!cloud_access_login(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
if (!oc_cloud_access_login(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.access_token))) {
OC_ERR("failed to sent sign in request to cloud");
goto retry;
}
Expand Down Expand Up @@ -835,8 +835,9 @@ cloud_manager_refresh_token_async(void *data)
}
conf.endpoint = ctx->cloud_ep;

if (!cloud_access_refresh_access_token(conf, oc_string(ctx->store.uid),
oc_string(ctx->store.refresh_token))) {
if (!oc_cloud_access_refresh_access_token(
conf, oc_string(ctx->store.auth_provider), oc_string(ctx->store.uid),
oc_string(ctx->store.refresh_token))) {
goto retry;
}

Expand Down
3 changes: 3 additions & 0 deletions api/cloud/oc_cloud_rd.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ cloud_rd_reset_context(oc_cloud_context_t *ctx)
void
oc_cloud_delete_resource(oc_resource_t *res)
{
if (res == NULL) {
return;
}
oc_cloud_context_t *ctx = oc_cloud_get_context(res->device);
if (ctx == NULL) {
return;
Expand Down
22 changes: 12 additions & 10 deletions api/cloud/unittest/cloud_access_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <gtest/gtest.h>

#include "oc_api.h"
#include "oc_cloud_access_internal.h"
#include "oc_cloud_access.h"
#include "oc_cloud_internal.h"
#include "oc_endpoint.h"

Expand Down Expand Up @@ -79,8 +79,8 @@ TEST_F(TestCloudAccess, cloud_access_register_p)
/*.user_data = */ nullptr,
/*.timeout=*/0,
};
bool ret = cloud_access_register(conf, "auth_provider", "auth_code", "uid",
"access_token");
bool ret = oc_cloud_access_register(conf, "auth_provider", "auth_code", "uid",
"access_token");

// Then
EXPECT_TRUE(ret);
Expand All @@ -97,7 +97,7 @@ TEST_F(TestCloudAccess, cloud_access_register_f)
/*.user_data = */ nullptr,
/*.timeout=*/0,
};
bool ret = cloud_access_register(conf, nullptr, nullptr, nullptr, nullptr);
bool ret = oc_cloud_access_register(conf, nullptr, nullptr, nullptr, nullptr);

// Then
EXPECT_FALSE(ret);
Expand All @@ -114,7 +114,7 @@ TEST_F(TestCloudAccess, cloud_access_login_p)
/*.user_data = */ nullptr,
/*.timeout=*/0,
};
bool ret = cloud_access_login(conf, "uid", "access_token");
bool ret = oc_cloud_access_login(conf, "uid", "access_token");

// Then
EXPECT_TRUE(ret);
Expand All @@ -131,7 +131,7 @@ TEST_F(TestCloudAccess, cloud_access_login_f)
/*.user_data = */ nullptr,
/*.timeout=*/0,
};
bool ret = cloud_access_login(conf, nullptr, nullptr);
bool ret = oc_cloud_access_login(conf, nullptr, nullptr);

// Then
EXPECT_FALSE(ret);
Expand All @@ -148,7 +148,7 @@ TEST_F(TestCloudAccess, cloud_access_logout_p)
/*.user_data = */ nullptr,
/*.timeout=*/0,
};
bool ret = cloud_access_logout(conf, "uid", "access_token");
bool ret = oc_cloud_access_logout(conf, "uid", "access_token");

// Then
EXPECT_TRUE(ret);
Expand All @@ -165,7 +165,7 @@ TEST_F(TestCloudAccess, cloud_access_logout_f)
/*.user_data = */ nullptr,
/*.timeout=*/0,
};
bool ret = cloud_access_logout(conf, nullptr, nullptr);
bool ret = oc_cloud_access_logout(conf, nullptr, nullptr);

// Then
EXPECT_FALSE(ret);
Expand All @@ -182,7 +182,8 @@ TEST_F(TestCloudAccess, cloud_access_refresh_access_token_p)
/*.user_data = */ nullptr,
/*.timeout=*/0,
};
bool ret = cloud_access_refresh_access_token(conf, "uid", "refresh_token");
bool ret =
oc_cloud_access_refresh_access_token(conf, "apn", "uid", "refresh_token");

// Then
EXPECT_TRUE(ret);
Expand All @@ -199,7 +200,8 @@ TEST_F(TestCloudAccess, cloud_access_refresh_access_token_f)
/*.user_data = */ nullptr,
/*.timeout=*/0,
};
bool ret = cloud_access_refresh_access_token(conf, nullptr, nullptr);
bool ret =
oc_cloud_access_refresh_access_token(conf, nullptr, nullptr, nullptr);

// Then
EXPECT_FALSE(ret);
Expand Down
Loading

0 comments on commit 8575b83

Please sign in to comment.