From 89c2cc1016c431a49e19da8db9b84ea83d887ab9 Mon Sep 17 00:00:00 2001 From: Sundaram Bhupathi Date: Fri, 1 Dec 2023 18:07:02 +0530 Subject: [PATCH] Added new API which use Auth URL Change-Id: I4717deb403f1e92c542166fb338815b01049d691 --- include/oauth2_manager.h | 45 ++++++++++++++++ src/oauth2_manager.c | 130 +++++++++++++++++++++++++++++++++++++++++++++-- src/oauth2_private.h | 1 + 3 files changed, 173 insertions(+), 3 deletions(-) diff --git a/include/oauth2_manager.h b/include/oauth2_manager.h index a702f5d..735fed6 100755 --- a/include/oauth2_manager.h +++ b/include/oauth2_manager.h @@ -127,6 +127,51 @@ typedef void (*oauth2_token_cb)(oauth2_response_h response, void *user_data); OAUTH2_API int oauth2_manager_request_token(oauth2_manager_h handle, oauth2_request_h request, oauth2_token_cb callback, void *user_data); /** + * @brief Called when the oauth2_manager_request_token_auth_url() response comes. + * @since_tizen 9.0 + * @remarks The @a response must be released using oauth2_response_destroy(). + * + * @param[in] response The response + * @param[in] user_data The user data passed from the callback function + * + * @pre oauth2_manager_request_token_auth_url() must be called to get this callback invoked. + * @see oauth2_manager_request_token_auth_url() + */ +typedef void (*oauth2_token_auth_url_cb)(oauth2_response_h response, void *user_data); + +/** + * @brief Requests OAuth 2.0 access token with authorization URL. + * @details The response is delivered via oauth2_token_auth_url_cb(). + * @since_tizen 9.0 + * @privlevel public + * @privilege %http://tizen.org/privilege/internet + * @remarks The "internet" privilege is required to call this API. Note, only one pending request is allowed. + * + * @param[in] handle The oauth2 manager handle. + * @param[in] request The request handle. + * @param[in] callback The callback to receive response. + * @param[in] response_url The response URL after successful Google Sign-in which contains the token. + * @param[in] user_data The user data to be passed to the callback function. + * + * @return @c 0 on success, + * otherwise a negative error value + * @retval #OAUTH2_ERROR_NONE Successful + * @retval #OAUTH2_ERROR_NOT_SUPPORTED Not supported. + * @retval #OAUTH2_ERROR_PERMISSION_DENIED Permission denied. + * @retval #OAUTH2_ERROR_INVALID_PARAMETER Invalid input parameter(s) passed. + * @retval #OAUTH2_ERROR_OUT_OF_MEMORY Out of memory. + * @retval #OAUTH2_ERROR_ALREADY_IN_PROGRESS The previous request is already in progress. + * @retval #OAUTH2_ERROR_PARSE_FAILED Parsing failed. + * @retval #OAUTH2_ERROR_NETWORK_ERROR Network Error. + * @retval #OAUTH2_ERROR_UNKNOWN Unknown system error. + * + * @see oauth2_token_auth_url_cb() + * @see oauth2_manager_create() + * @see oauth2_request_create() + */ +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); + +/** * @brief Called when oauth2_manager_request_authorization_grant() response comes. * @since_tizen @if MOBILE 2.4 @elseif WEARABLE 3.0 @endif * diff --git a/src/oauth2_manager.c b/src/oauth2_manager.c index 92c48b7..9617cfc 100755 --- a/src/oauth2_manager.c +++ b/src/oauth2_manager.c @@ -161,6 +161,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 +319,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 +350,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; } @@ -1381,6 +1391,120 @@ oauth2_manager_request_token(oauth2_manager_h handle, oauth2_request_h request, } 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, void *user_data) diff --git a/src/oauth2_private.h b/src/oauth2_private.h index 11c436d..9eb43f1 100755 --- a/src/oauth2_private.h +++ b/src/oauth2_private.h @@ -127,6 +127,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; -- 2.7.4