Added New API which use Auth URL with internal header 04/303004/2 accepted/tizen_7.0_unified accepted/tizen/7.0/unified/20231220.124857 accepted/tizen/7.0/unified/20231220.134943 accepted/tizen/7.0/unified/20231220.170049
authorSundaram Bhupathi <s.bhupathi@samsung.com>
Mon, 18 Dec 2023 13:06:09 +0000 (18:36 +0530)
committerSundaram Bhupathi <s.bhupathi@samsung.com>
Mon, 18 Dec 2023 13:07:15 +0000 (18:37 +0530)
Change-Id: I316ea2c7251fdf16d78c04401ee98734d6b9c580
Signed-off-by: Sundaram Bhupathi <s.bhupathi@samsung.com>
internal/oauth2_experimental.h [new file with mode: 0755]
src/CMakeLists.txt
src/oauth2_manager.c
src/oauth2_private.h

diff --git a/internal/oauth2_experimental.h b/internal/oauth2_experimental.h
new file mode 100755 (executable)
index 0000000..3991199
--- /dev/null
@@ -0,0 +1,93 @@
+/*\r
+ * Copyright (c) 2014 - 2023 Samsung Electronics Co., Ltd. All rights reserved.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ */\r
+\r
+#ifndef OAUTH2_EXPERIMENTAL_H_\r
+#define OAUTH2_EXPERIMENTAL_H_\r
+\r
+#include <stdbool.h>\r
+\r
+#include <oauth2_request.h>\r
+#include <oauth2_response.h>\r
+#include <oauth2_error.h>\r
+#include <oauth2_types.h>\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+/**\r
+ * @file oauth2_experimental.h\r
+ * @brief The experimental OAuth 2.0 handler APIs.\r
+ * @details The Application must use either:\r
+ * oauth2_manager_request_token\r
+ * or\r
+ * oauth2_manager_request_authorization_grant and then oauth2_manager_request_access_token.\r
+ * One instance handles only one pending request at any given time. If requested again before completion it throws TIZEN_ERROR_ALREADY_IN_PROGRESS.\r
+ * Application  must have internet in order to use these APIs.\r
+ */\r
+\r
+\r
+/**\r
+ * @brief Called when the oauth2_manager_request_token_auth_url() response comes.\r
+ * @since_tizen 9.0\r
+ * @remarks The @a response must be released using oauth2_response_destroy().\r
+ *\r
+ * @param[in] response The response\r
+ * @param[in] user_data The user data passed from the callback function\r
+ *\r
+ * @pre oauth2_manager_request_token_auth_url() must be called to get this callback invoked.\r
+ * @see oauth2_manager_request_token_auth_url()\r
+ */\r
+typedef void (*oauth2_token_auth_url_cb)(oauth2_response_h response, void *user_data);\r
+\r
+/**\r
+ * @brief Requests OAuth 2.0 access token with authorization URL.\r
+ * @details The response is delivered via oauth2_token_auth_url_cb().\r
+ * @since_tizen 9.0\r
+ * @privlevel public\r
+ * @privilege %http://tizen.org/privilege/internet\r
+ * @remarks The "internet" privilege is required to call this API. Note, only one pending request is allowed.\r
+ *\r
+ * @param[in] handle   The oauth2 manager handle.\r
+ * @param[in] request  The request handle.\r
+ * @param[in] callback The callback to receive response.\r
+ * @param[in] response_url  The response URL after successful Google Sign-in which contains the token.\r
+ * @param[in] user_data  The user data to be passed to the callback function.\r
+ *\r
+ * @return     @c 0 on success,\r
+ *             otherwise a negative error value\r
+ * @retval     #OAUTH2_ERROR_NONE               Successful\r
+ * @retval     #OAUTH2_ERROR_NOT_SUPPORTED Not supported.\r
+ * @retval     #OAUTH2_ERROR_PERMISSION_DENIED Permission denied.\r
+ * @retval     #OAUTH2_ERROR_INVALID_PARAMETER Invalid input parameter(s) passed.\r
+ * @retval     #OAUTH2_ERROR_OUT_OF_MEMORY Out of memory.\r
+ * @retval     #OAUTH2_ERROR_ALREADY_IN_PROGRESS The previous request is already in progress.\r
+ * @retval     #OAUTH2_ERROR_PARSE_FAILED Parsing failed.\r
+ * @retval     #OAUTH2_ERROR_NETWORK_ERROR Network Error.\r
+ * @retval     #OAUTH2_ERROR_UNKNOWN Unknown system error.\r
+ *\r
+ * @see oauth2_token_auth_url_cb()\r
+ * @see oauth2_manager_create()\r
+ * @see oauth2_request_create()\r
+ */\r
+OAUTH2_API int oauth2_manager_request_token_auth_url(oauth2_manager_h handle, oauth2_request_h request, oauth2_token_auth_url_cb callback, char *response_url, void *user_data);\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* OAUTH2_EXPERIMENTAL_H_ */\r
index 4390bbcfff09122b71d9f48eb64b887c0f20a70c..c903b126f4cf5cbe0509359f36e2790d822236e7 100644 (file)
@@ -15,6 +15,7 @@ SET(SRCS
 )
 
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/internal)
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
 
 INCLUDE(FindPkgConfig)
index 92c48b72aa9cce13cc031fc5d8816491ecd83c44..1a4f54d9852f6edc6c1445f2873930634feb2ab0 100755 (executable)
@@ -21,6 +21,7 @@
 #include <system_info.h>
 
 #include "oauth2_manager.h"
+#include "oauth2_experimental.h"
 #include "oauth2_util.h"
 #include "oauth2_private.h"
 
@@ -161,6 +162,7 @@ static void
 __reset_all_cb(oauth2_manager_s *mgr_handle)
 {
        mgr_handle->token_cb = NULL;
+       mgr_handle->token_auth_url_cb = NULL;
        mgr_handle->grant_cb = NULL;
        mgr_handle->access_token_cb = NULL;
        mgr_handle->refresh_cb = NULL;
@@ -318,6 +320,14 @@ __send_response_to_caller(oauth2_manager_s *mgr_handle)
                                (mgr_handle->response), mgr_handle->user_data);
                } else
                        (mgr_handle->token_cb)(NULL, mgr_handle->user_data);
+       } else if (mgr_handle->token_auth_url_cb) {
+               if (mgr_handle->response) {
+                       OAUTH2_LOG_I("__send_response_to_caller calling \
+                               application cb");
+                       (mgr_handle->token_auth_url_cb)((oauth2_response_h)
+                               (mgr_handle->response), mgr_handle->user_data);
+               } else
+                       (mgr_handle->token_auth_url_cb)(NULL, mgr_handle->user_data);
        } else if (mgr_handle->grant_cb) {
                if (mgr_handle->response) {
                        (mgr_handle->grant_cb)((oauth2_response_h)
@@ -341,9 +351,10 @@ __send_response_to_caller(oauth2_manager_s *mgr_handle)
        }
        __reset_all_cb(mgr_handle);
 
-       eext_object_event_callback_del(mgr_handle->login_win,
-               EEXT_CALLBACK_BACK, __handle_back_key);
-
+       if(!mgr_handle->login_win) {
+               eext_object_event_callback_del(mgr_handle->login_win,
+                       EEXT_CALLBACK_BACK, __handle_back_key);
+       }
        OAUTH2_LOG_I("__send_response_to_caller end");
        return;
 }
@@ -1380,6 +1391,120 @@ oauth2_manager_request_token(oauth2_manager_h handle, oauth2_request_h request,
        /*LCOV_EXCL_STOP*/
 }
 
+OAUTH2_API int
+oauth2_manager_request_token_auth_url(oauth2_manager_h handle, oauth2_request_h request,
+       oauth2_token_auth_url_cb callback, char *response_url, void *user_data)
+{
+       OAUTH2_RETURN_VAL(__is_feature_supported(), {}, OAUTH2_ERROR_NOT_SUPPORTED,
+               "oauth2 feature not supported");
+
+       OAUTH2_RETURN_VAL(callback, {}, OAUTH2_ERROR_INVALID_PARAMETER,
+               "callback is Null");
+
+       OAUTH2_RETURN_VAL(handle, {}, OAUTH2_ERROR_INVALID_PARAMETER,
+               "handle is Null");
+
+       OAUTH2_RETURN_VAL(request, {}, OAUTH2_ERROR_INVALID_PARAMETER,
+               "request is Null");
+       OAUTH2_RETURN_VAL(response_url, {}, OAUTH2_ERROR_INVALID_PARAMETER,
+               "Response URL is Null");
+       /*LCOV_EXCL_START*/
+
+       OAUTH2_RETURN_VAL(__check_permission(), {}, OAUTH2_ERROR_PERMISSION_DENIED,
+               "permission denied, no internet privilege");
+
+       oauth2_manager_s *mgr_impl = (oauth2_manager_s *)handle;
+       OAUTH2_RETURN_VAL(!mgr_impl->is_active, {},
+               OAUTH2_ERROR_ALREADY_IN_PROGRESS, "Already in progress");
+
+       mgr_impl->request = (oauth2_request_s *)request;
+
+       __reset_all_cb(mgr_impl);
+
+       char *redirect_uri = NULL;
+       bundle_get_str(mgr_impl->request->request_data,
+               OAUTH2_PARAMETER_KEY_REDIRECT_URI, &redirect_uri);
+       if (!redirect_uri) {
+               OAUTH2_LOG_E("Missing mandatory field [%s]",
+                       OAUTH2_PARAMETER_KEY_REDIRECT_URI);
+               return OAUTH2_ERROR_INVALID_PARAMETER;
+       }
+
+       char *grant_type_str = NULL;
+       bundle_get_str(mgr_impl->request->request_data,
+               OAUTH2_PARAMETER_KEY_GRANT_TYPE, &grant_type_str);
+       char *grant_type = __get_grant_type(grant_type_str);
+
+       char *response_type_str = NULL;
+       bundle_get_str(mgr_impl->request->request_data,
+               OAUTH2_PARAMETER_KEY_RESPONSE_TYPE, &response_type_str);
+       char *response_type = __get_response_type(response_type_str);
+
+       if (!grant_type && !response_type) {
+               OAUTH2_LOG_E("[%s] or [%s] must be specified",
+                       OAUTH2_PARAMETER_KEY_GRANT_TYPE,
+                       OAUTH2_PARAMETER_KEY_RESPONSE_TYPE);
+               return OAUTH2_ERROR_INVALID_PARAMETER;
+       }
+
+       mgr_impl->token_auth_url_cb = callback;
+       mgr_impl->user_data = user_data;
+
+       mgr_impl->is_active = TRUE;
+
+       /* For authorization code and implicit, response_type is mentioned */
+       if (response_type) {
+               if (!strcmp(response_type,
+                       OAUTH2_PARAMETER_VAL_RESPONSE_TYPE_CODE) ||
+                       !strcmp(response_type,
+                       OAUTH2_PARAMETER_VAL_RESPONSE_TYPE_TOKEN)) {
+                       mgr_impl->request_func = REQUEST_ACCESS_TOKEN;
+                       _on_auth_grant_received(mgr_impl, response_url);
+
+                       return OAUTH2_ERROR_NONE;
+               }
+       }
+       /*
+        * For resource owner pwd and client credentials, grant_type
+        * is mentioned
+        */
+       else {
+               /*
+                * Here authorization grant handling is out of oauth 2.0 RFC, so
+                * unless a custom auth grant handler is set, we will proceed
+                * with access token request directly
+                */
+               if (!strcmp(grant_type,
+                       OAUTH2_PARAMETER_VAL_GRANT_TYPE_PASSWORD) ||
+                       !strcmp(grant_type,
+                       OAUTH2_PARAMETER_VAL_GRANT_TYPE_CLIENT_CREDENTIALS)) {
+                       /* For pwd: grant_type, username, password, scope */
+                       if (!strcmp(grant_type,
+                               OAUTH2_PARAMETER_VAL_GRANT_TYPE_PASSWORD)) {
+                               _request_access_token_for_grant_type_pwd(
+                                       mgr_impl);
+                       } else {
+                               /*For client cred: grant_type, scope */
+                               __request_access_token_for_client_cred(
+                                       mgr_impl);
+                       }
+
+                       /* _request_access_token() */
+                       return OAUTH2_ERROR_NONE;
+               } else {
+                       /*
+                        * TODO:
+                        * TBD, extension grant_type / response_type support
+                        */
+                       return OAUTH2_ERROR_NOT_SUPPORTED;
+               }
+       }
+
+       /* TODO: TBD, extension grant_type / response_type support */
+       return OAUTH2_ERROR_NOT_SUPPORTED;
+       /*LCOV_EXCL_STOP*/
+}
+
 OAUTH2_API int
 oauth2_manager_request_authorization_grant(oauth2_manager_h handle,
        oauth2_request_h request, oauth2_auth_grant_cb callback,
index 11c436db128825f3da3a4eda17afec00510894f3..6c0caffa0e776e6e47e82afe7fd5253b42500c8f 100755 (executable)
@@ -32,6 +32,7 @@
 
 #include <json-glib/json-glib.h>
 #include "oauth2_manager.h"
+#include "oauth2_experimental.h"
 
 #define OAUTH2_PARAMETER_VAL_GRANT_TYPE_AUTH_CODE      "authorization_code"
 
@@ -127,6 +128,7 @@ typedef struct _oauth2_manager_s {
 
 /* callbacks */
        oauth2_token_cb token_cb;
+       oauth2_token_auth_url_cb token_auth_url_cb;
        oauth2_auth_grant_cb grant_cb;
        oauth2_access_token_cb access_token_cb;
        oauth2_refresh_token_cb refresh_cb;