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.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
22 namespace Tizen.Account.OAuth2
25 /// The ImplicitGrantAuthorizer is used to obtain access tokens using Implicit Grant flow as described at https://tools.ietf.org/html/rfc6749#section-4.2
27 /// <since_tizen> 3 </since_tizen>
28 public class ImplicitGrantAuthorizer : Authorizer
33 /// <since_tizen> 3 </since_tizen>
34 public ImplicitGrantAuthorizer()
40 /// Access token can be retreived implicitly using <see cref="ClientCredentialsAuthorizer.AuthorizeAsync"/> in this flow.
42 /// <since_tizen> 3 </since_tizen>
43 /// <exception cref="InvalidOperationException">Thrown when the operation is not supported</exception>
44 public Task<TokenResponse> GetAccessTokenAsync(TokenRequest request)
46 Log.Error(ErrorFactory.LogTag, "Obtain token directly from authorization grant ");
47 throw new InvalidOperationException();
51 /// Refreshing access token is not supported in this flow.
53 /// <since_tizen> 3 </since_tizen>
54 /// <exception cref="InvalidOperationException">Thrown when the operation is not supported</exception>
55 public override Task<TokenResponse> RefreshAccessTokenAsync(RefreshTokenRequest request)
57 Log.Error(ErrorFactory.LogTag, "Refesh token is not supported in Implicit Grant flow");
58 throw new InvalidOperationException();
61 private TokenResponse GetAuthorizationResponse(IntPtr requestHandle)
63 IntPtr error = IntPtr.Zero;
64 TokenResponse response = null;
65 int ret = (int)OAuth2Error.None;
66 Interop.Manager.Oauth2AuthGrantCallback authGrantCb = (IntPtr responseHandle, IntPtr usrData) =>
68 if (responseHandle == IntPtr.Zero)
70 Log.Error(ErrorFactory.LogTag, "Error occured");
71 throw (new ArgumentNullException());
74 Interop.Response.GetError(responseHandle, out error);
75 if (error != IntPtr.Zero)
77 Log.Error(ErrorFactory.LogTag, "Server Error occured");
82 ret = Interop.Response.GetAccessToken(responseHandle, out accessToken);
83 if (ret != (int)OAuth2Error.None)
85 Log.Error(ErrorFactory.LogTag, "Interop failed");
86 throw ErrorFactory.GetException(ret);
90 ret = Interop.Response.GetTokenType(responseHandle, out tokenType);
91 if (ret != (int)OAuth2Error.None)
93 Log.Error(ErrorFactory.LogTag, "Interop failed");
94 throw ErrorFactory.GetException(ret);
98 ret = Interop.Response.GetExpiresIn(responseHandle, out expiresIn);
99 if (ret != (int)OAuth2Error.None)
101 Log.Error(ErrorFactory.LogTag, "Interop failed");
102 throw ErrorFactory.GetException(ret);
106 ret = Interop.Response.GetScope(responseHandle, out scope);
107 if (ret != (int)OAuth2Error.None)
109 Log.Error(ErrorFactory.LogTag, "Interop failed");
110 throw ErrorFactory.GetException(ret);
114 ret = Interop.Response.GetState(responseHandle, out state);
115 if (ret != (int)OAuth2Error.None)
117 Log.Error(ErrorFactory.LogTag, "Interop failed");
118 throw ErrorFactory.GetException(ret);
121 IEnumerable<string> scopes = (scope == IntPtr.Zero) ? null : Marshal.PtrToStringAnsi(scope)?.Split(' ');
123 var token = new AccessToken() { Token = Marshal.PtrToStringAnsi(accessToken), ExpiresIn = expiresIn, Scope = scopes, TokenType = Marshal.PtrToStringAnsi(tokenType) };
124 response = new TokenResponse(responseHandle) { AccessToken = token, State = Marshal.PtrToStringAnsi(state), RefreshToken = null };
128 ret = Interop.Manager.RequestAuthorizationGrant(_managerHandle, requestHandle, authGrantCb, IntPtr.Zero);
129 Interop.Request.Destroy(requestHandle);
130 if (ret != (int)OAuth2Error.None || error != IntPtr.Zero)
132 if (error != IntPtr.Zero)
134 throw ErrorFactory.GetException(error);
138 Log.Error(ErrorFactory.LogTag, "Interop failed");
139 throw ErrorFactory.GetException(ret);
146 // Fill device request handle for Authorization code grant
147 private IntPtr GetRequestHandle(ImplicitGrantAuthorizationRequest request)
151 Log.Error(ErrorFactory.LogTag, "Invalid request or request is null");
152 throw ErrorFactory.GetException((int)OAuth2Error.InvalidParameter);
155 IntPtr requestHandle;
156 int ret = Interop.Request.Create(out requestHandle);
157 if (ret != (int)OAuth2Error.None)
159 Log.Error(ErrorFactory.LogTag, "Interop failed");
160 throw ErrorFactory.GetException(ret);
163 ret = Interop.Request.SetAuthEndPointUrl(requestHandle, request.AuthorizationEndpoint.ToString());
164 if (ret != (int)OAuth2Error.None)
166 Log.Error(ErrorFactory.LogTag, "Interop failed");
167 throw ErrorFactory.GetException(ret);
170 ret = Interop.Request.SetResponseType(requestHandle, Interop.ResponseType.Token);
171 if (ret != (int)OAuth2Error.None)
173 Log.Error(ErrorFactory.LogTag, "Interop failed");
174 throw ErrorFactory.GetException(ret);
177 ret = Interop.Request.SetClientId(requestHandle, request.ClientSecrets.Id);
178 if (ret != (int)OAuth2Error.None)
180 Log.Error(ErrorFactory.LogTag, "Interop failed");
181 throw ErrorFactory.GetException(ret);
184 if (request.RedirectionEndPoint != null)
186 ret = Interop.Request.SetRedirectionUrl(requestHandle, request.RedirectionEndPoint.ToString());
187 if (ret != (int)OAuth2Error.None)
189 Log.Error(ErrorFactory.LogTag, "Interop failed");
190 throw ErrorFactory.GetException(ret);
194 if (request.Scopes != null)
196 string scope = string.Join(" ", request.Scopes);
197 ret = Interop.Request.SetScope(requestHandle, scope);
198 if (ret != (int)OAuth2Error.None)
200 Log.Error(ErrorFactory.LogTag, "Interop failed");
201 throw ErrorFactory.GetException(ret);
205 if (request.State != null)
207 ret = Interop.Request.SetState(requestHandle, request.State);
208 if (ret != (int)OAuth2Error.None)
210 Log.Error(ErrorFactory.LogTag, "Interop failed");
211 throw ErrorFactory.GetException(ret);
215 if (request.CustomData != null)
217 foreach (var item in request.CustomData)
219 ret = Interop.Request.AddCustomData(requestHandle, item.Key, item.Value);
220 if (ret != (int)OAuth2Error.None)
222 Log.Error(ErrorFactory.LogTag, "Interop failed");
223 throw ErrorFactory.GetException(ret);
228 return requestHandle;