Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Account.OAuth2 / Tizen.Account.OAuth2 / ClientCredentialsAuthorizer.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Threading.Tasks;
19 using System.Collections.Generic;
20
21 namespace Tizen.Account.OAuth2
22 {
23     /// <summary>
24     /// The ClientCredentialsAuthorizer is used to obtain access tokens using Client Credentials Grant flow as described at https://tools.ietf.org/html/rfc6749#section-4.4
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class ClientCredentialsAuthorizer : Authorizer
28     {
29         /// <summary>
30         /// The constructor
31         /// </summary>
32         /// <since_tizen> 3 </since_tizen>
33         public ClientCredentialsAuthorizer()
34         {
35
36         }
37
38         /// <summary>
39         /// Authorization not supported through this API for this flow.
40         /// </summary>
41         /// <since_tizen> 3 </since_tizen>
42         /// <exception cref="InvalidOperationException">Thrown when the operation is not supported</exception>
43         public override Task<AuthorizationResponse> AuthorizeAsync(AuthorizationRequest request)
44         {
45             Log.Error(ErrorFactory.LogTag, "Authorization is not supported in this flow");
46             throw new InvalidOperationException();
47         }
48
49         /// <summary>
50         /// Refreshing access token is not supported in this flow.
51         /// </summary>
52         /// <since_tizen> 3 </since_tizen>
53         /// <exception cref="InvalidOperationException">Thrown when the operation is not supported</exception>
54         public override Task<TokenResponse> RefreshAccessTokenAsync(RefreshTokenRequest request)
55         {
56             Log.Error(ErrorFactory.LogTag, "Refreshing access token is not supported in this flow");
57             throw new InvalidOperationException();
58         }
59
60         /// <summary>
61         /// Retrieves access token using client credentials.
62         /// The authroization request parameters should be as defined in https://tools.ietf.org/html/rfc6749#section-4.4.2
63         /// </summary>
64         /// <since_tizen> 3 </since_tizen>
65         /// <param name="request">The token request <see cref="ClientCredentialsTokenRequest"/></param>
66         /// <returns>The response containing access token.</returns>
67         /// <privilege>http://tizen.org/privilege/internet</privilege>
68         /// <exception cref="ArgumentException">Thrown when method failed due to invalid argumets</exception>
69         /// <exception cref="OAuth2Exception">Thrown when method fails due to server error</exception>
70         public override async Task<TokenResponse> GetAccessTokenAsync(TokenRequest request)
71         {
72             IntPtr requestHandle = GetRequestHandle(request as ClientCredentialsTokenRequest);
73             return await Task.Run(() => GetAccessToken(requestHandle));
74         }
75
76         // Fill device request handle for access token
77         private IntPtr GetRequestHandle(ClientCredentialsTokenRequest request)
78         {
79             if (request == null)
80             {
81                 Log.Error(ErrorFactory.LogTag, "Invalid request or request is null");
82                 throw ErrorFactory.GetException((int)OAuth2Error.InvalidParameter);
83             }
84
85             IntPtr requestHandle;
86             int ret = Interop.Request.Create(out requestHandle);
87             if (ret != (int)OAuth2Error.None)
88             {
89                 Log.Error(ErrorFactory.LogTag, "Interop failed");
90                 throw ErrorFactory.GetException(ret);
91             }
92
93             ret = Interop.Request.SetTokenEndPointUrl(requestHandle, request.TokenEndpoint.ToString());
94             if (ret != (int)OAuth2Error.None)
95             {
96                 Log.Error(ErrorFactory.LogTag, "Interop failed");
97                 throw ErrorFactory.GetException(ret);
98             }
99
100             ret = Interop.Request.SetRedirectionUrl(requestHandle, request.RedirectionEndPoint.ToString());
101             if (ret != (int)OAuth2Error.None)
102             {
103                 Log.Error(ErrorFactory.LogTag, "Interop failed");
104                 throw ErrorFactory.GetException(ret);
105             }
106
107             ret = Interop.Request.SetGrantType(requestHandle, Interop.GrantType.ClientCredentials);
108             if (ret != (int)OAuth2Error.None)
109             {
110                 Log.Error(ErrorFactory.LogTag, "Interop failed");
111                 throw ErrorFactory.GetException(ret);
112             }
113
114             if (request.ClientSecrets.Id != null)
115             {
116                 ret = Interop.Request.SetClientId(requestHandle, request.ClientSecrets.Id);
117                 if (ret != (int)OAuth2Error.None)
118                 {
119                     Log.Error(ErrorFactory.LogTag, "Interop failed");
120                     throw ErrorFactory.GetException(ret);
121                 }
122             }
123
124             if (request.ClientSecrets.Secret != null)
125             {
126                 ret = Interop.Request.SetClientSecret(requestHandle, request.ClientSecrets.Secret);
127                 if (ret != (int)OAuth2Error.None)
128                 {
129                     Log.Error(ErrorFactory.LogTag, "Interop failed");
130                     throw ErrorFactory.GetException(ret);
131                 }
132             }
133
134             if (request.Scopes != null)
135             {
136                 string scope = string.Join(" ", request.Scopes);
137                 ret = Interop.Request.SetScope(requestHandle, scope);
138                 if (ret != (int)OAuth2Error.None)
139                 {
140                     Log.Error(ErrorFactory.LogTag, "Interop failed");
141                     throw ErrorFactory.GetException(ret);
142                 }
143             }
144
145             if (request.CustomData != null)
146             {
147                 foreach (var item in request.CustomData)
148                 {
149                     ret = Interop.Request.AddCustomData(requestHandle, item.Key, item.Value);
150                     if (ret != (int)OAuth2Error.None)
151                     {
152                         Log.Error(ErrorFactory.LogTag, "Interop failed");
153                         throw ErrorFactory.GetException(ret);
154                     }
155                 }
156             }
157
158             ret = Interop.Request.SetClientAuthenticationType(requestHandle, (int) request.AuthenticationScheme);
159             if (ret != (int)OAuth2Error.None)
160             {
161                 Log.Error(ErrorFactory.LogTag, "Interop failed");
162                 throw ErrorFactory.GetException(ret);
163             }
164
165             return requestHandle;
166         }
167     }
168 }