2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Threading.Tasks;
19 using System.Collections.Generic;
21 namespace Tizen.Account.OAuth2
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
26 /// <since_tizen> 3 </since_tizen>
27 public class ClientCredentialsAuthorizer : Authorizer
32 /// <since_tizen> 3 </since_tizen>
33 public ClientCredentialsAuthorizer()
39 /// Authorization not supported through this API for this flow.
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)
45 Log.Error(ErrorFactory.LogTag, "Authorization is not supported in this flow");
46 throw new InvalidOperationException();
50 /// Refreshing access token is not supported in this flow.
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)
56 Log.Error(ErrorFactory.LogTag, "Refreshing access token is not supported in this flow");
57 throw new InvalidOperationException();
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
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)
72 IntPtr requestHandle = GetRequestHandle(request as ClientCredentialsTokenRequest);
73 return await Task.Run(() => GetAccessToken(requestHandle));
76 // Fill device request handle for access token
77 private IntPtr GetRequestHandle(ClientCredentialsTokenRequest request)
81 Log.Error(ErrorFactory.LogTag, "Invalid request or request is null");
82 throw ErrorFactory.GetException((int)OAuth2Error.InvalidParameter);
86 int ret = Interop.Request.Create(out requestHandle);
87 if (ret != (int)OAuth2Error.None)
89 Log.Error(ErrorFactory.LogTag, "Interop failed");
90 throw ErrorFactory.GetException(ret);
93 ret = Interop.Request.SetTokenEndPointUrl(requestHandle, request.TokenEndpoint.ToString());
94 if (ret != (int)OAuth2Error.None)
96 Log.Error(ErrorFactory.LogTag, "Interop failed");
97 throw ErrorFactory.GetException(ret);
100 ret = Interop.Request.SetRedirectionUrl(requestHandle, request.RedirectionEndPoint.ToString());
101 if (ret != (int)OAuth2Error.None)
103 Log.Error(ErrorFactory.LogTag, "Interop failed");
104 throw ErrorFactory.GetException(ret);
107 ret = Interop.Request.SetGrantType(requestHandle, Interop.GrantType.ClientCredentials);
108 if (ret != (int)OAuth2Error.None)
110 Log.Error(ErrorFactory.LogTag, "Interop failed");
111 throw ErrorFactory.GetException(ret);
114 if (request.ClientSecrets.Id != null)
116 ret = Interop.Request.SetClientId(requestHandle, request.ClientSecrets.Id);
117 if (ret != (int)OAuth2Error.None)
119 Log.Error(ErrorFactory.LogTag, "Interop failed");
120 throw ErrorFactory.GetException(ret);
124 if (request.ClientSecrets.Secret != null)
126 ret = Interop.Request.SetClientSecret(requestHandle, request.ClientSecrets.Secret);
127 if (ret != (int)OAuth2Error.None)
129 Log.Error(ErrorFactory.LogTag, "Interop failed");
130 throw ErrorFactory.GetException(ret);
134 if (request.Scopes != null)
136 string scope = string.Join(" ", request.Scopes);
137 ret = Interop.Request.SetScope(requestHandle, scope);
138 if (ret != (int)OAuth2Error.None)
140 Log.Error(ErrorFactory.LogTag, "Interop failed");
141 throw ErrorFactory.GetException(ret);
145 if (request.CustomData != null)
147 foreach (var item in request.CustomData)
149 ret = Interop.Request.AddCustomData(requestHandle, item.Key, item.Value);
150 if (ret != (int)OAuth2Error.None)
152 Log.Error(ErrorFactory.LogTag, "Interop failed");
153 throw ErrorFactory.GetException(ret);
158 ret = Interop.Request.SetClientAuthenticationType(requestHandle, (int) request.AuthenticationScheme);
159 if (ret != (int)OAuth2Error.None)
161 Log.Error(ErrorFactory.LogTag, "Interop failed");
162 throw ErrorFactory.GetException(ret);
165 return requestHandle;