Add 3.0 APIs and sync APIs same as 2.4
[platform/core/convergence/service-adaptor.git] / client / sal_auth_provider.c
1 /*
2  * Auth Plugin Client
3  *
4  * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <glib.h>
23
24 #include <app.h>
25
26 #include "service_adaptor_errors.h"
27 #include "service_adaptor_internal.h"
28 #include "sal_service_provider.h"
29 #include "sal_auth_provider.h"
30
31 API int auth_provider_create(auth_provider_h *provider)
32 {
33         SAL_FN_CALL;
34
35         auth_provider_h auth_provider = (auth_provider_h) g_malloc0(sizeof(auth_provider_s));
36
37         auth_provider->oauth1_get_access_token = NULL;
38         auth_provider->oauth1_get_extra_data = NULL;
39
40         auth_provider->oauth2_get_access_token = NULL;
41         auth_provider->oauth2_get_extra_data = NULL;
42
43         *provider = auth_provider;
44
45         return SERVICE_ADAPTOR_ERROR_NONE;
46 }
47
48 API app_control_h auth_provider_message(auth_provider_h provider, const char *operation, void *user_data)
49 {
50         SAL_FN_CALL;
51
52         app_control_h reply = NULL;
53
54         RETV_IF(NULL == provider, reply);
55
56         int ret = SERVICE_ADAPTOR_ERROR_NONE;
57
58         if (0 == strcmp(operation, OAUTH1_0_GET_ACCESS_TOKEN_URI))
59         {
60                 app_control_create(&reply);
61
62                 char *access_token = NULL;
63                 ret = provider->oauth1_get_access_token(&access_token);
64
65                 if (SERVICE_ADAPTOR_ERROR_NONE != ret)
66                 {
67                         SAL_ERR("oauth1_get_access_token() Fail (%d)", ret);
68                         app_control_add_extra_data(reply, PLUGIN_RESULT_KEY, PLUGIN_RESULT_VALUE_FAILURE);
69                         return reply;
70                 }
71
72                 app_control_add_extra_data(reply, PLUGIN_RESULT_KEY, PLUGIN_RESULT_VALUE_SUCCESS);
73                 app_control_add_extra_data(reply, OAUTH1_0_ACCESS_TOKEN_KEY, access_token);
74         }
75
76         return reply;
77 }
78
79 API int auth_provider_add_extra_data(auth_provider_h provider, app_control_h reply)
80 {
81         SAL_FN_CALL;
82
83         RETV_IF(NULL == provider, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
84         RETV_IF(NULL == reply, SERVICE_ADAPTOR_ERROR_INVALID_PARAMETER);
85
86         app_control_add_extra_data(reply, PLUGIN_KEY_AUTH, PLUGIN_VALUE_TRUE);
87
88         if (NULL != provider->oauth1_get_access_token)
89         {
90                 app_control_add_extra_data(reply, OAUTH1_0_GET_ACCESS_TOKEN_URI, PLUGIN_VALUE_TRUE);
91         }
92
93         if (NULL != provider->oauth1_get_extra_data)
94         {
95                 app_control_add_extra_data(reply, OAUTH1_0_GET_EXTRA_DATA_URI, PLUGIN_VALUE_TRUE);
96         }
97
98         if (NULL != provider->oauth2_get_access_token)
99         {
100                 app_control_add_extra_data(reply, OAUTH2_0_GET_ACCESS_TOKEN_URI, PLUGIN_VALUE_TRUE);
101         }
102
103         if (NULL != provider->oauth2_get_extra_data)
104         {
105                 app_control_add_extra_data(reply, OAUTH2_0_GET_EXTRA_DATA_URI, PLUGIN_VALUE_TRUE);
106         }
107
108         return SERVICE_ADAPTOR_ERROR_NONE;
109 }